当您打开具有详细文本设置的QMessageBox时,它有“显示详细信息”按钮。我希望在默认情况下显示详细信息,而不是用户必须单击Show .按钮第一。

发布于 2016-03-18 12:15:31
从来源的快速浏览中我可以看出,没有一种简单的方法可以直接打开细节文本,或者访问“显示详细信息.”按钮。我能找到的最好的方法是:
ActionRole,因为这对应于“显示详细信息.”按钮。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();
}发布于 2021-04-22 09:26:21
至少在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();https://stackoverflow.com/questions/36083551
复制相似问题