简体   繁体   English

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

[英]Can an icon be centered over text in a QMessageBox?

I am new to Qt, and am having fun diving in by building up an application in it. 我是Qt的新手,通过在其中构建应用程序来享受乐趣。 I have produced the below QMessageBox as a response to the About MyGreatApp menu item, which you can see has both an icon and text. 我已经生成了以下QMessageBox作为对About MyGreatApp菜单项的响应,您可以看到它有图标和文本。

关于QMessageBox的示例

Can icon and text be positioned with respect to one another? 图标和文字可以相互定位吗? Specifically: can the icon be centered over the text? 具体来说:图标可以在文本中心吗?

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 macOS 10.13.3
Qt Creator 4.5 based on Qt 5.10.0 (Clang 7.0 (Apple), 64 bit) Qt Creator 4.5基于Qt 5.10.0(Clang 7.0(Apple),64位)

you can set it in the html but you must pass the path of the image that is in qresource , for it you must use qrc: : 你可以在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();

在此输入图像描述

For there to be a better look we can give it a margin on the right side: 为了更好看,我们可以在右侧给它一个余量:

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

在此输入图像描述

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM