2

I create a QProgressDialog, set a QProgressBar to it. Set the format

"Processing section %v of %m. Percentage completed: %p."

to the QProgressBar. But the text is cut, not the whole progress bar is displayed on the dialog.

enter image description here

How to adjust the width to show the whole progress bar?

demonplus
  • 5,613
  • 12
  • 49
  • 68
ldlchina
  • 927
  • 2
  • 12
  • 31

1 Answers1

2

Here is an example which uses QFontMetrics to get the width of the progress bar's text, and then adds 100px to this for the progress bar itself. When the dialog is shown, it is resized to this width.

auto dialog = new QProgressDialog();
dialog->setWindowTitle("Progress");
dialog->setLabelText("Test progress dialog");

auto bar = new QProgressBar(dialog);
bar->setTextVisible(true);
bar->setValue(50);
bar->setFormat("Processing section %v of %m. Percentage completed: %p");
dialog->setBar(bar);

// Use QFontMetrics to get the width of the bar text,
// and then add 100px for the progress bar itself. Set
// this to the initial dialog width.
int width = QFontMetrics(bar->font()).width(bar->text()) + 100;
dialog->resize(width, dialog->height());
dialog->show();

This allows you to avoid hardcoding a width, although it's a bit complicated. I think just setting a reasonable minimum width like below is probably a better/simpler solution:

dialog->setMinimumWidth(300);
ajshort
  • 3,684
  • 5
  • 29
  • 43