修改QMessageBox按钮大小的代码
时间: 2023-12-04 19:38:50
浏览: 122
可以通过继承QMessageBox并重写showEvent方法来修改QMessageBox按钮的大小。以下是示例代码:
```cpp
#include <QLabel>
#include <QMessageBox>
#include <QWidget>
class MyMessageBox : public QMessageBox {
Q_OBJECT
public:
MyMessageBox(Icon icon, const Q[string](https://geek.csdn.net/edu/8802d631b97a4a6af1f4d0bbf8527465?dp_token=eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJpZCI6NDQ0MDg2MiwiZXhwIjoxNzA3MzcxOTM4LCJpYXQiOjE3MDY3NjcxMzgsInVzZXJuYW1lIjoid2VpeGluXzY4NjQ1NjQ1In0.RrTYEnMNYPC7AQdoij4SBb0kKEgHoyvF-bZOG2eGQvc&spm=1055.2569.3001.10083)& title, const Q[string](https://geek.csdn.net/edu/8802d631b97a4a6af1f4d0bbf8527465?dp_token=eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJpZCI6NDQ0MDg2MiwiZXhwIjoxNzA3MzcxOTM4LCJpYXQiOjE3MDY3NjcxMzgsInVzZXJuYW1lIjoid2VpeGluXzY4NjQ1NjQ1In0.RrTYEnMNYPC7AQdoij4SBb0kKEgHoyvF-bZOG2eGQvc&spm=1055.2569.3001.10083)& text, StandardButtons buttons, QWidget* parent = 0);
~MyMessageBox();
protected:
void showEvent(QShowEvent* event) override;
MyMessageBox::MyMessageBox(Icon icon, const Q[string](https://geek.csdn.net/edu/8802d631b97a4a6af1f4d0bbf8527465?dp_token=eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJpZCI6NDQ0MDg2MiwiZXhwIjoxNzA3MzcxOTM4LCJpYXQiOjE3MDY3NjcxMzgsInVzZXJuYW1lIjoid2VpeGluXzY4NjQ1NjQ1In0.RrTYEnMNYPC7AQdoij4SBb0kKEgHoyvF-bZOG2eGQvc&spm=1055.2569.3001.10083)& title, const Q[string](https://geek.csdn.net/edu/8802d631b97a4a6af1f4d0bbf8527465?dp_token=eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJpZCI6NDQ0MDg2MiwiZXhwIjoxNzA3MzcxOTM4LCJpYXQiOjE3MDY3NjcxMzgsInVzZXJuYW1lIjoid2VpeGluXzY4NjQ1NjQ1In0.RrTYEnMNYPC7AQdoij4SBb0kKEgHoyvF-bZOG2eGQvc&spm=1055.2569.3001.10083)& 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); // 设置按钮大小[策略](https://geek.csdn.net/educolumn/06da626ca8219c1b9b6fba41572b8cf2?spm=1055.2569.3001.10083)
```