本文介绍了如何调整QMessageBox的大小?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个QMessageBox,我希望它更大.这是一个简单的QMessageBox,带有两个标准按钮,即确定"和取消".问题在于,对于我的应用程序而言,它很小.代码显示如下:

I have a QMessageBox which I'd like it to be bigger. It's a simple QMessageBox with two standard buttons, Ok and Cancel. The problem is that it is very small for my application's purposes. Code shows like this:

QMessageBox msg;
msg.setText("Whatever");
msg.setStandardButtons(QMessageBox::Ok | QMessageBox::Cancel);
msg.setSizePolicy(QSizePolicy::Expanding,QSizePolicy::Expanding);

int ret = msg.exec();
switch (ret) {
  case QMessageBox::Ok:
      ui->textEdit->clear();
      break;
  case QMessageBox::Cancel:
      break;}

我尝试了几种增加尺寸的方法:

I tried several ways to increase the size:

msg.setSizePolicy(QSizePolicy::Expanding,QSizePolicy::Expanding);

msg.setSizePolicy(QSizePolicy::Maximum,QSizePolicy::Maximum);

msg.setFixedHeight(600);
msg.setFixedWidth(600);

我什至清理并重建了它,它编译了所有内容,但没有任何效果...

I even cleared and rebuilt, and it compiles everything but nothing take effect...

您对如何手动"设置QMessageBox大小有任何想法吗?谢谢.

Do you have any idea on how to set QMessageBox size "by hand"? Thanks.

推荐答案

您可以编辑标签的CSS:

You can edit the css of the label:

msg.setStyleSheet("QLabel{min-width: 700px;}");

您可以类似地编辑按钮的css以添加边距或将其增大.

You can similarly edit the css of the buttons to add a margin or make them bigger.

例如:

msg.setStyleSheet("QLabel{min-width:500 px; font-size: 24px;} QPushButton{ width:250px; font-size: 18px; }");

还有一个提到的技巧:

QSpacerItem* horizontalSpacer = new QSpacerItem(800, 0, QSizePolicy::Minimum, QSizePolicy::Expanding);
QGridLayout* layout = (QGridLayout*)msg.layout();
layout->addItem(horizontalSpacer, layout->rowCount(), 0, 1, layout->columnCount());

但这似乎并不适合所有人.

But this doesn't seem to work for everyone.

这篇关于如何调整QMessageBox的大小?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-26 21:17