如何调整 QMessageBox 按钮的大小以适应翻译后的文本?

How to make QMessageBox buttons resized to fit translated text?

我目前正在对应用程序进行本地化。一切都按照我的预期进行翻译,但是,QMessageBox 不会调整按钮的大小以适应文本。

这是我用来生成问题框的代码,QTranslator 包含在 MM_TR 定义的地方:

    #include <QMessageBox>

    void MainWindow::closeEvent( QCloseEvent * pEvent ) {
        QMessageBox::StandardButtons    iButtons        = QMessageBox::Save | QMessageBox::Discard | QMessageBox::Cancel;
        QMessageBox::StandardButton     iDefaultButton  = QMessageBox::Save;
        QMessageBox::StandardButton     iButton         = QMessageBox::question( this, QString::fromStdString( MM_TR( "ctxMessageBoxQuestion", "Save changes?" ) ), QString::fromStdString( MM_TR( "ctxMessageBoxQuestion", "Project has been modified, save changes?" ) ), iButtons, iDefaultButton );
    }

我在互联网上搜索过任何遇到同样问题的人,但到目前为止还没有找到任何结论。我已经尝试将大小策略设置为 MinimumMinimumExpanding 但这也不起作用。唯一有用的是设置样式表,我尝试使用以下代码:

        QMessageBox::StandardButtons    iButtons        = QMessageBox::Save | QMessageBox::Abort | QMessageBox::Cancel;
        QMessageBox msgClose( QMessageBox::Question, "Test", "Test button translation resizing.", iButtons );
        msgClose.setStyleSheet( "QPushButton {min-width:100;}" );

不过,我不认为正确的做法是根据出现的任何语言手动设置最小宽度,所以我不想那样做。这也改变了所有按钮,这不是我想要的。

我想知道此时是否唯一的选择是创建自定义对话框?

更新: 我的最终解决方案包括 cbuchart 的答案以及样式表填充设置:

    QMessageBox::StandardButtons    iButtons        = QMessageBox::Save | QMessageBox::Discard | QMessageBox::Cancel;
    QMessageBox msgClose( QMessageBox::Question, QString::fromStdString( MM_TR( "ctxMessageBoxQuestion", "Save changes?" ) ), QString::fromStdString( MM_TR( "ctxMessageBoxQuestion", "Project has been modified, save changes?" ) ), iButtons );
    msgClose.setStyleSheet( "QPushButton {padding: 3px;}" );
    msgClose.layout()->setSizeConstraint( QLayout::SizeConstraint::SetMinimumSize );
    QMessageBox::StandardButton     iButton = (QMessageBox::StandardButton)msgClose.exec();

这给了我这个:

需要注意的是,如果 padding 增加太多,它会开始覆盖文本 - 我不太明白 - 但 3px 似乎不错。

更新 2:

在尝试之后,我认为 QMessageBox 有一些固定的宽度,它链接到无法修改的消息框文本本身。如果消息框文本足够长,按钮会调整大小并适合按钮文本,因此按钮调整大小似乎与按钮文本本身无关。

我尝试使用 setMinimumWidthsetFixedWidth 进行调整,但框没有调整大小。根据此错误 QTBUG-7851 中的评论,我认为 QMessageBox 无法以编程方式调整大小。如果有人知道不包括制作自定义对话框的实际解决方案,那就太好了。

更新 3:

根据 cbuchart 的评论,我意识到有一个 .qss 样式表的 min-width 设置导致 QPushButtons 无法正确调整大小。

不需要使用样式表,尽管您仍然需要手动创建对象而不是使用 QMessageBox::question

您可以使用 QLayout::setSizeConstraint 将消息框布局更改为自动展开。这将强制对话框调整大小以适合其内容。

例子(也可以找here):

#include <QtWidgets/QApplication>
#include <qmessagebox.h>
#include <qlayout.h>

int main(int argc, char* argv[])
{
  QApplication a(argc, argv);

  QMessageBox::StandardButtons iButtons = QMessageBox::Save | QMessageBox::Abort | QMessageBox::Cancel;
  QMessageBox msgClose( QMessageBox::Question, "Test", "Test button translation resizing.", iButtons );
  msgClose.setButtonText(QMessageBox::Save, "Save: super mega long text for testing");
  msgClose.setButtonText(QMessageBox::Cancel, "Cancel: another super mega long text for testing");
  msgClose.layout()->setSizeConstraint(QLayout::SetMinimumSize); // QLayout::SetFixedSize also works
  msgClose.exec();

  return 0;
}