开发者

Can QMessageBox::about adjust size to title length?

I wanted to create a simple About dialog, but noticed that the QMessageBox::about does not adjust its size to the length of the title (which is usually longer due to the larger font ...at least in my desktop environment), only to the content. Is there a way to make sure that the dial开发者_开发百科og is made big enough to show all of the title as well? I could of course add white space to the aboutText, but I am hoping for a less hackish solution.

Example:

QString titleText("Some title which is slightly longer");
QString aboutText("Short about text");
QMessageBox::about(this,titleText,aboutText);

Currently the above code only gives me "Some ..." as the title string. I have built the program in Eclipse on Ubuntu with Qt 4.7.


Use "setStyleSheet()" function of "QMessageBox". Here is an example.

background-color: QLinearGradient(x1: 0, y1: 0, x2: 0, y2: 1, stop: 0 #787878, stop: 0.5 #404040, stop: 0.6 #303030, stop: 0.8 #252525, stop: 1 #151515);
border: 2px solid #05b8cc;
border-radius: 8px;
color: white;
min-width: 300px;
min-height: 80px;

It will also affect the children of the "QMessageBox" whose stylesheets can be reverted by iterating through them. To access the children use "findChildren(QWidget)".


I believe QMessageBox does adjust size to fit the window title, but for some reason it doesn't work right on my system also, not sure if it's a bug or a feature, this is done in the qmessagabox.cpp QMessageBoxPrivate::updateSize() method.

Another thing I've noticed is that you're using an instance of the QMessageBox class to call about() method, which is static and you can execute it by using just the class name: QMessageBox::about(..).

What you could do to adjust the window size is creating your own subclass of the QMessageBox and adjusting min width of the window in the showEvent method, see the example below for details:

class MyMessageBox : public QMessageBox
{
public:
    explicit MyMessageBox(QWidget *parent = 0) : QMessageBox(parent) { }
    MyMessageBox(const QString &title, const QString &text, Icon icon,
                 int button0, int button1, int button2,
                 QWidget *parent = 0,
                 Qt::WindowFlags f = Qt::Dialog | Qt::MSWindowsFixedSizeDialogHint) :
        QMessageBox(title, text, icon, button0, button1, button2, parent, f) { }


    static void about(QString title, QString text)
    {
        MyMessageBox aboutBox(title, text, QMessageBox::Information, 0, 0, 0, NULL);

        aboutBox.setText(title);
        aboutBox.setText(text);
        QIcon icon = aboutBox.windowIcon();
        QSize size = icon.actualSize(QSize(64, 64));
        aboutBox.setIconPixmap(icon.pixmap(size));

        aboutBox.exec();
    }

    void showEvent(QShowEvent *event)
    {
        QMessageBox::showEvent(event);
        QWidget *textField = findChild<QWidget *>("qt_msgbox_label");
        if (textField != NULL)
        {
            // getting what ever my system has set for the window title font
            QFont font = QFont("Ubuntu Bold", 11);
            // you might want to make it more generic by detecting the actuall font
            // or using smth like this:
            //QFont font = QApplication::font("QWorkspaceTitleBar");

            QFontMetrics fm(font);
            int width = qMax(fm.width(windowTitle()) + 50, textField->minimumWidth());
            textField->setMinimumWidth(width);
        }
    }
};

here's how you can call it:

QString titleText("Some title which is slightly longer");
QString aboutText("Short about text");
MyMessageBox::about(titleText, aboutText);

hope this helps, regards

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜