开发者

An "About" message box for a GUI with Qt

    QMessageBox::about( this, "About Application",
    "<h4>Application is a one-paragraph blurb</h4>\n\n"
"Copyright 1991-2003 Such-and-such. "
"For technical support, call 1234-56789 or see\n"
"<a href=\"http://www.suc开发者_如何转开发h-and-such.com\">http://www.such-and-such.com</a>" );

This code is creating the About message box which I wanted to have with two exceptions:

1) I would like to change the icon in the message box with an aaa.png file

2) And I would like to have the link clickable. It looks like hyperlink (it is blue and underlined) but mouse click does not work

Any ideas?


I think you should create a custom QWidget for your about widget. By this way, you can put on the widget all you want. By example, you can place QLabel using the openExternalLinks property for clickable link.

To display a custom image on the QWidget, this example may help.


For the icon, you need to just set the application icon. Something like this:

QApplication::setWindowIcon(QIcon(":/aaa.png")); // from a resource file

As for making the links clickable, I don't think it can be done with the QMessageBox::about API directly.


QMessageBox msgBox;
msgBox.setTextFormat(Qt::RichText); // this does the magic trick and allows you to click the link
msgBox.setText("Text<br /><a href=\"http://www.such-and-such.com\">http://www.such-and-such.com</a>");
msgBox.setIcon(yourIcon);
msgBox.exec();


For future reference, the docs state that the default type for textFormat is Qt::AutoText. The docs further state that Qt::AutoText is interpreted as Qt::RichText if Qt::mightBeRichText() returns true, otherwise as Qt::PlainText. Finally, mightBeRichText uses a fast and therefore simple heuristic. It mainly checks whether there is something that looks like a tag before the first line break. So, since you dont have a tag in your first line, it assumes that it is plain text. Set it to RichText explicitely with msgBox.setTextFormat(Qt::RichText); to make it act accordingly.


there's a message in the qtcenter about it: http://www.qtcentre.org/threads/17365-Clickable-URL-in-QMessageBox

Use http://doc.qt.nokia.com/latest/qlabel.html#setOpenExternalLinks


main.cpp

QApplication app(argc, argv);
app.setWindowIcon(QIcon(":/images/your_icon.png"));

mainwindow.cpp (into your slot if you have one)

void MainWindow::on_aboutAction_triggered()
{
    QMessageBox::about(0, "window title", "<a href='http://www.jeffersonpalheta.com'>jeffersonpalheta.com</a>");
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜