How can I resize QMessageBox?

前端 未结 5 657
没有蜡笔的小新
没有蜡笔的小新 2020-12-15 06:11

I have a QMessageBox which I\'d like it to be bigger. It\'s a simple QMessageBox with two standard buttons, Ok and Cancel. The problem is that it i

5条回答
  •  萌比男神i
    2020-12-15 07:10

    I wanted my QMessageBox width to adapt in proportion to the length of the text content with a certain amount of buffer to avoid line wrap. After surveying numerous forums and threads including this one, I came up with:

    int x_offset = (2.0 * MainWindow::geometry().x());
    int y_offset = (0.5 * MainWindow::geometry().y());
    msgBox.setText(vers_msg.data());
    QSpacerItem* horizontalSpacer = new QSpacerItem 
        (8 * vers_msg.size(), 0,
        QSizePolicy::Minimum, QSizePolicy::Expanding);
    QGridLayout* layout = (QGridLayout*)msgBox.layout();
    layout->addItem(horizontalSpacer, layout->rowCount(),
        0, 1, layout->columnCount());
    msgBox.setGeometry(
        MainWindow::geometry().x() + x_offset,
        MainWindow::geometry().y() + y_offset,
        msgBox.geometry().width(),
        msgBox.geometry().height());
    

    Adjust the hard numbers in x_offset, y_offset and horizontalSpacer to suit your situation. I was hoping it would be easier than this but at least this works.

提交回复
热议问题