gpt4 book ai didi

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

转载 作者:太空狗 更新时间:2023-10-29 21:33:59 25 4
gpt4 key购买 nike

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

Example About QMessageBox

图标和文本可以相对于彼此定位吗?具体来说:图标可以在文本上居中吗?

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

enter image description here

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

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

enter image description here

关于c++ - 图标可以在 QMessageBox 中以文本为中心吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48734194/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com