QMessageBox"显示详细信息"

UmN*_*obe 7 qt qmessagebox

当您QMessageBox使用详细文本集打开时,它具有显示详细信息按钮.我希望默认情况下显示详细信息,而不是用户必须先单击" 显示详细信息..."按钮.

qt_doc_example

ajs*_*ort 7

据我快速浏览源代码可以看出,没有简单的方法可以直接打开详细信息文本,或者确实访问“显示详细信息...”按钮。我能找到的最好方法是:

  1. 遍历消息框上的所有按钮。
  2. 提取具有角色的那个ActionRole,因为这对应于“显示详细信息...”按钮。
  3. click对此手动调用该方法。

此操作的代码示例:

#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();
}
Run Code Online (Sandbox Code Playgroud)