我是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位)
答案 0 :(得分:2)
你可以在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 © Me, 1848–2018. All rights reserved.</p>"
"<p style='text-align: center;'> </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 © Me, 1848–2018. All rights reserved.</p>"
"<p style='text-align: center;'> </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();