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);