Can QMessageBox::about adjust size to title length?

前提是你 提交于 2019-12-10 15:30:09

问题


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 dialog 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.


回答1:


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)".




回答2:


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



来源:https://stackoverflow.com/questions/5582896/can-qmessageboxabout-adjust-size-to-title-length

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!