图标可以在 QMessageBox 中以文本为中心吗?

Can an icon be centered over text in a QMessageBox?

我是 Qt 的新手,通过在其中构建应用程序,我很享受深入学习的乐趣。我制作了下面的 QMessageBox 作为对 About MyGreatApp 菜单项的响应,您可以看到它既有图标又有文本。

图标和文字可以相互定位吗?具体来说:图标可以在文字上居中吗?

void MainWindow::about()
{
    QMessageBox msgAbout;
    msgAbout.setInformativeText("<span style='text-align: center'><p><b><font size = 20>My Great Application</font><p><font size = 10>Version 100.1</font><p><font size = 10>by Me</font><p>Copyright © Me, 1848–2018. All rights reserved.</span><span style='text-align: left'><p>Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.</span><p>");
    QPixmap pixAbout;
    pixAbout.load(":/Info.png");
    msgAbout.setIconPixmap(pixAbout);
    msgAbout.setStandardButtons(QMessageBox::Ok);
    msgAbout.setDefaultButton(QMessageBox::Ok);
    msgAbout.exec();
}

macOS 10.13.3
Qt Creator 4.5 基于 Qt 5.10.0(Clang 7.0 (Apple),64 位)

你可以在 html 中设置它,但是你必须传递 qresource 中的图像路径,为此你必须使用 qrc::

QMessageBox msgAbout;
const QString message = "<p style='text-align: center;'><img src='qrc://Info.png' alt='' width='42' height='42'></p>"
                        "<p style='text-align: center;'><strong>My Great Application</strong></p>"
                        "<p style='text-align: center;'>Version 100.1</p>"
                        "<p style='text-align: center;'>by Me</p>"
                        "<p style='text-align: center;'>Copyright &copy; Me, 1848&ndash;2018. All rights reserved.</p>"
                        "<p style='text-align: center;'>&nbsp;</p>"
                        "<p>Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.</p>";

msgAbout.setInformativeText(message);
msgAbout.setStandardButtons(QMessageBox::Ok);
msgAbout.setDefaultButton(QMessageBox::Ok);
msgAbout.exec();

为了更好看,我们可以在右边给它一个边距:

QMessageBox msgAbout;
const QString message = "<p style='text-align: center;'><img src='qrc:/Info.png' alt="" width='42' height='42' /></p>"
                        "<p style='text-align: center;'><strong>My Great Application</strong></p>"
                        "<p style='text-align: center;'>Version 100.1</p>"
                        "<p style='text-align: center;'>by Me</p>"
                        "<p style='text-align: center;'>Copyright &copy; Me, 1848&ndash;2018. All rights reserved.</p>"
                        "<p style='text-align: center;'>&nbsp;</p>"
                        "<p>Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.</p>";

msgAbout.setInformativeText(message);
msgAbout.setStandardButtons(QMessageBox::Ok);
msgAbout.setDefaultButton(QMessageBox::Ok);

QGridLayout *lay = msgAbout.findChild<QGridLayout *>();
QMargins margins = lay->contentsMargins();
margins.setRight(40);
lay->setContentsMargins(margins);
msgAbout.exec();