c – QMessageBox未显示整个标题

#include <QtGui>

int main(int argc, char** argv) 
{
    QApplication app(argc, argv);

    // first
    QMessageBox box(0);
    box.setText("short text");
    box.setWindowTitle("looooooooooooooooong text");
    box.setMinimumSize(800, 0);

    box.exec();


    // second
    //QMessageBox::warning(0, "looooooooooooooooong text", "short text");

    return app.exec();
}

这两种方法都会生成此消息框,其中标题未正确显示.我试图通过它没有帮助调整对话框小部件的大小.如何强制QMessageBox显示整个标题?

作为一种解决方法,我可以为标题文本添加尾随空格,但我认为应该有更好的解决方案.

aminasya@aminasya-desktop:~/qt$qmake --version
QMake version 2.01a
Using Qt version 4.8.6 in /usr/lib/x86_64-linux-gnu
最佳答案
我尝试使用qmessagebox.cpp中提到的qt帮助的构造函数创建QMessageBox,但它对我也没有用.
由于某种原因,QMessageBox调整大小以适合窗口标题不起作用.但是,您可以通过创建自己的QMessageBox子类来调整MessageBox大小.

请看下面的例子;

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

转载注明原文:c – QMessageBox未显示整个标题 - 代码日志