QProgressDialog:如何调整对话框的大小以适应其内容?

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

我用下面的代码测试:

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

标题和取消按钮文字被剪切。我调用了 adjustSize(),但没有用。如何调整对话框的大小以适应其内容?

您可以使用以下方法:使用 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);