【问题标题】:QMessageBox "show details"QMessageBox“显示详细信息”
【发布时间】:2016-07-05 03:48:38
【问题描述】:

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

【问题讨论】:

    标签: qt qmessagebox


    【解决方案1】:

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

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

    【讨论】:

    • 你的例子对我不起作用。当我遍历按钮时,它只会找到标准按钮(例如,“是”和“否”)。在我浏览它们时,“显示详细信息”按钮不在其中。使用 Qt 4.7.4 版。
    • 不确定抱歉 - 从内存中我在 Qt 5.x 上测试过这个。也许尝试使用递归 findChildren 调用查看消息框的每个 QAbstractButton 子项?
    • 是的,我也曾尝试通过findChildren() 搜索“显示详细信息”按钮。我确实可以通过这种方式找到“显示详细信息”按钮,但在我找到它时它的作用是InvalidRole。无论如何,我在那个按钮上打电话给click(),只是想看看会发生什么。没有效果。
    • 你可以将最后一个 if 与循环融合:``` QAbstractButton *detailsButton = nullptr; foreach (QAbstractButton *button, messageBox.buttons()) { if (messageBox.buttonRole(button) == QMessageBox::ActionRole) { detailsButton = button;详细信息按钮->点击(); // 单击它以展开文本中断; } } ```
    【解决方案2】:

    这个函数会默认展开细节,同时也将文本框调整为更大的尺寸:

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

    【讨论】:

      【解决方案3】:

      至少在 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();
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2010-10-16
        • 1970-01-01
        • 2019-09-23
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多