7
#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
4

4 回答 4

1

我尝试QMessageBox使用 qt help 中提到的构造函数进行创建,qmessagebox.cpp但它也对我不起作用。出于某种原因QMessageBox,调整大小以适应窗口标题不起作用。但是,您可以MessageBox通过创建自己的子类来调整大小QMessageBox

请看下面的例子;

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);
            }
        }
    };
于 2015-07-13T10:01:12.523 回答
1

似乎 QMessageBox 在使用 exec() 启动时会检查正文的长度并自动调整大小,而忽略了标题文本可能更长的事实。虽然不理想,但您可以在之后使用 QTimer 更改它,如下所示:

QMessageBox* box = new QMessageBox;
box->setText("short text");
box->setWindowTitle("looooooooooooooooong text");

QTimer* pTimer = new QTimer;
pTimer->setSingleShot(true);
QObject::connect(pTimer, &QTimer::timeout, [=](){
   box->setMinimumWidth(400);
   pTimer->deleteLater();
});
pTimer->start(0);
box->exec();

由于这发生在消息框启动后,因此大小的变化是可见的。

更好的解决方案是创建自己的 MessageBox,从 QDialog 派生。毕竟,QMessageBox 类只是一个方便的小部件。

于 2015-07-13T10:11:16.913 回答
1

由于exec()show()都根据框文本的内容覆盖您的最小尺寸,简单的解决方案是不使用exec() 在显示框后设置最小尺寸。在任何情况下,你都不应该exec()在对话框中使用。

注意:窗口标题是不可移植的。如果没有窗口标题,您的 UI 必须仍然有意义。例如,在 OS X 上,消息框窗口标题是不可见的。

#include <QApplication>
#include <QMessageBox>
#include <QDebug>

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

  QMessageBox box;
  box.setText("short text");
  box.setWindowTitle("looooooooooooooooong text");
  box.show();
  box.setMinimumSize(qMax(box.minimumWidth(), 800), box.minimumHeight());

  return app.exec();
}
于 2015-07-13T16:39:29.713 回答
0

由于消息框宽度调整为文本的宽度,一个简单的答案似乎是用空格填充文本字符串:

QString title = "This is a looooooooooooooooooooooooong title";
QString txt   = "Short Text";
QMessageBox box;
box.setWindowTitle(title);
int titleLen = title.length();
int txtLen   = txt.length();
if (txtLen < titleLen)
{
    int diff = titleLen - txtLen;
    txt.resize(titleLen + (diff * 2),' '); // diff * 2 is extra padding;
}
box.setText(txt);
box.exec();

你可以看到我因为可变宽度字体而混淆了填充长度。但是,嘿,kludge 对我有用。

于 2021-07-14T17:18:25.103 回答