QMessageBoxのウィンドウサイズをカスタマイズする方法
12836 ワード
QMessageBoxのウィンドウサイズをカスタマイズする方法
1.考え方
クラス継承QMessageBoxを新しく作成し、resizeEvent(QResizeEvent*event)関数を再ロードします.
2.実現
//============================================
//Brief: QMessageBox
//Email: [email protected]
//Date:
//============================================
#ifndef AUTOMESSAGEBOX_H
#define AUTOMESSAGEBOX_H
#include
#include
#include
class CAutoMessageBox:public QMessageBox
{
Q_OBJECT
public:
explicit CAutoMessageBox(QWidget*parent=0);
~CAutoMessageBox(){}
public:
void AutoSetSize(int width,int high);
protected:
void resizeEvent(QResizeEvent*event);
private:
int m_width;
int m_high;
};
#endif // AUTOMESSAGEBOX_H
#include "automessagebox.h"
CAutoMessageBox::CAutoMessageBox(QWidget *parent):QMessageBox(parent)
,m_width(0)
,m_high(0)
{
}
void CAutoMessageBox::AutoSetSize(int width, int high)
{
m_width = width;
m_high = high;
}
void CAutoMessageBox::resizeEvent(QResizeEvent *event)
{
setFixedSize(m_width,m_high);
}
3.テスト
#ifndef WIDGET_H
#define WIDGET_H
#include
namespace Ui {
class Widget;
}
class Widget : public QWidget
{
Q_OBJECT
public:
explicit Widget(QWidget *parent = 0);
~Widget();
private slots:
void on_pushButton_clicked();
private:
Ui::Widget *ui;
};
#include "widget.h"
#include "ui_widget.h"
#include "automessagebox.h"
Widget::Widget(QWidget *parent) :
QWidget(parent),
ui(new Ui::Widget)
{
ui->setupUi(this);
}
Widget::~Widget()
{
delete ui;
}
void Widget::on_pushButton_clicked()
{
CAutoMessageBox msg;
msg.AutoSetSize(768,512);
msg.setText(tr("hello world,hello QT..."));
msg.setWindowTitle(tr("INFO"));
msg.setStyleSheet("font:14px;background-color:rgb(255,224,220)");
msg.setIcon(QMessageBox::Critical);
msg.addButton(tr("YES"),QMessageBox::ActionRole);
msg.addButton(tr("NO"),QMessageBox::AcceptRole);
msg.exec();
}
#endif // WIDGET_H