修改QMessageBox按钮大小的代码
时间: 2023-12-04 18:38:50 浏览: 249
可以通过继承QMessageBox并重写showEvent方法来修改QMessageBox按钮的大小。以下是示例代码:
```cpp
#include <QLabel>
#include <QMessageBox>
#include <QWidget>
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);
QList<QPushButton*> btns = findChildren<QPushButton*>();
for (auto btn : btns) {
btn->setMinimumSize(100, 50); // 设置按钮最小尺寸
btn->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding); // 设置按钮大小策略
}
}
```
在使用MyMessageBox时,只需要像使用QMessageBox一样即可:
```cpp
MyMessageBox msgBox(QMessageBox::Information, "Title", "Message", QMessageBox::Ok | QMessageBox::Cancel);
msgBox.exec();
```
阅读全文