QMessageBox“显示详细信息”

时间:2016-03-18 11:46:58

标签: qt qmessagebox

当您打开带有详细文字集的QMessageBox时,它会显示显示详细信息按钮。我希望默认情况下显示详细信息,而不是用户必须先点击显示详细信息... 按钮。

qt_doc_example

3 个答案:

答案 0 :(得分:3)

据我所知,通过the source快速查看,没有简单的方法可以直接打开详细信息文本,或者确实访问"显示详细信息..."按钮。我能找到的最佳方法是:

  1. 遍历消息框中的所有按钮。
  2. 提取具有角色ActionRole的那个,因为这对应于"显示详细信息..."按钮。
  3. 手动调用click方法。
  4. 此操作的代码示例:

    #include <QAbstractButton>
    #include <QApplication>
    #include <QMessageBox>
    
    int main(int argc, char *argv[]) {
        QApplication app(argc, argv);
    
        QMessageBox messageBox;
        messageBox.setText("Some text");
        messageBox.setDetailedText("More details go here");
    
        // Loop through all buttons, looking for one with the "ActionRole" button
        // role. This is the "Show Details..." button.
        QAbstractButton *detailsButton = NULL;
    
        foreach (QAbstractButton *button, messageBox.buttons()) {
            if (messageBox.buttonRole(button) == QMessageBox::ActionRole) {
                detailsButton = button;
                break;
            }
        }
    
        // If we have found the details button, then click it to expand the
        // details area.
        if (detailsButton) {
            detailsButton->click();
        }
    
        // Show the message box.
        messageBox.exec();
    
        return app.exec();
    }
    

答案 1 :(得分:0)

此功能将默认扩展细节,并将文本框的大小调整为更大的尺寸:

#include <QTextEdit>
#include <QMessageBox>
#include <QAbstractButton>

void showDetailsInQMessageBox(QMessageBox& messageBox)
{
    foreach (QAbstractButton *button, messageBox.buttons())
    {
        if (messageBox.buttonRole(button) == QMessageBox::ActionRole)
        {
            button->click();
            break;
        }
    }
    QList<QTextEdit*> textBoxes = messageBox.findChildren<QTextEdit*>();
    if(textBoxes.size())
        textBoxes[0]->setFixedSize(750, 250);
}

...  //somewhere else

QMessageBox box;
showDetailsInQMessageBox(box);

答案 2 :(得分:0)

至少在 Qt5 上:

    QMessageBox msgBox;
    msgBox.setText("Some text");
    msgBox.setDetailedText(text);

    // Search the "Show Details..." button
    foreach (QAbstractButton *button, msgBox.buttons())
    {
        if (msgBox.buttonRole(button) == QMessageBox::ActionRole)
        {
            button->click(); // click it to expand the text
            break;
        }
    }

    msgBox.exec();