Search code examples
c++qtqprogressbar

QProgressDialog: How to adjust the size of the dialog to fit its contents?


I test with code below:

QProgressDialog* dialog = new QProgressDialog("Message", "Close", 0, 10);
dialog->setWindowTitle("Long Long Long Long Title");
dialog->setCancelButtonText("Long Long Long Click this button to cancel");
dialog->setWindowModality(Qt::ApplicationModal);
dialog->adjustSize();
dialog->setValue(5);

The title and the cancel button text are cut. I called adjustSize(), but it didn't work. How to adjust the size of the dialog to fit its contents?

enter image description here


Solution

  • You can use the following: use the QLayout...

    QProgressDialog* dialog = new QProgressDialog("Message", "Close", 0, 10);
    dialog->setWindowTitle("Long Long Long Long Title");
    dialog->setCancelButtonText("Long Long Long Click this button to cancel");
    dialog->setWindowModality(Qt::ApplicationModal);
    dialog->setValue(5);
    
    QVBoxLayout *layout = new QVBoxLayout;
    foreach (QObject *obj, dialog->children()) {
        QWidget *widget = qobject_cast<QWidget *>(obj);
        if (widget)
            layout->addWidget(widget);
    }
    dialog->setLayout(layout);