Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
643 views
in Technique[技术] by (71.8m points)

c++ - How can I resize 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...

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

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

You can edit the css of the label:

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

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

For example:

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

There is also a trick mentioned:

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...