qt qmessagebox设置大小
时间: 2024-03-10 17:40:21 浏览: 111
可以通过重写QMessageBox的showEvent()函数来设置QMessageBox的大小。具体实现方法如下:
```cpp
class MyMessageBox : public QMessageBox {
Q_OBJECT
public:
MyMessageBox(Icon icon, const QString& title, const QString& text, StandardButtons buttons, QWidget* parent = 0);
~MyMessageBox();
protected:
void showEvent(QShowEvent* event) override;
};
MyMessageBox::MyMessageBox(Icon icon, const QString& title, const QString& text, StandardButtons buttons, QWidget* parent)
: QMessageBox(icon, title, text, buttons, parent)
{
}
MyMessageBox::~MyMessageBox()
{
}
void MyMessageBox::showEvent(QShowEvent* event)
{
QMessageBox::showEvent(event);
this->setFixedSize(600, 400); // 设置QMessageBox的大小为600x400
}
```
在main.cpp中创建MyMessageBox对象时,可以设置其大小:
```cpp
MyMessageBox messageBox(QMessageBox::Question, "提示", "是否保存当前项目?", QMessageBox::Yes | QMessageBox::No);
messageBox.setFixedSize(600, 400); // 设置QMessageBox的大小为600x400
messageBox.exec();
```
阅读全文