简体   繁体   中英

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. 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的示例

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

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: :

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

在此输入图像描述

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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