我创建了一个名为QMessageBox
的{{1}}的子类,我希望在子类的构造函数中执行某些操作。这是我的班级:
部首:
CustomQMessageBox
.cpp的:
#ifndef CUSTOMQMESSAGEBOX_H
#define CUSTOMQMESSAGEBOX_H
#include <QMessageBox>
class CustomQMessageBox : public QMessageBox
{
public:
CustomQMessageBox();
};
#endif // CUSTOMQMESSAGEBOX_H
当我创建#include "customqmessagebox.h"
CustomQMessageBox::CustomQMessageBox()
{
qDebug() << "yo";
}
的实例时,会调用构造函数并CustomQMessageBox
说qDebug
。但是,如果我调用静态API方法,则不会调用构造函数。
yo
当我使用//constructor is called
CustomQMessageBox cbox;
//constructor is not called
CustomQMessageBox::question(this, "Title", "Question?", QMessageBox::Yes | QMessageBox::No);
的静态方法时,为什么不调用构造函数?
感谢您的时间。
答案 0 :(得分:-1)
这是我经常使用的一个类,它来自QMessageBox。您可以使用构造函数来启动自定义按钮。如果你需要更具体的东西,你可以从这个课程中获得。
如果您想获得按钮角色,可以调用messageBox-&gt; buttonRole(QAbstractButton * buttonInQuestion);
头。
#ifndef MESSAGEBOX_H
#define MESSAGEBOX_H
#include <QMessageBox>
#include <QPushButton>
class MessageBox : public QMessageBox
{
Q_OBJECT
public:
MessageBox(QWidget* parent, const QString& title, const QString& text,
QMessageBox::Icon icon = QMessageBox::NoIcon,
const QString& btnText1 = "", QMessageBox::ButtonRole btnRole1 = QMessageBox::AcceptRole,
const QString& btnText2 = "", QMessageBox::ButtonRole btnRole2 = QMessageBox::InvalidRole,
const QString& btnText3 = "", QMessageBox::ButtonRole btnRole3 = QMessageBox::InvalidRole);
virtual ~MessageBox();
virtual QPushButton* addButton(const QString& btnText, QMessageBox::ButtonRole btnRole);
QPushButton* button(const unsigned int& index) const;
bool clicked_button(const unsigned int& index) const;
private:
QPushButton *_button1;
QPushButton *_button2;
QPushButton *_button3;
};
#endif // MESSAGEBOX_H
类
#include "messagebox.h"
MessageBox::MessageBox(QWidget* parent, const QString& title, const QString& text,
QMessageBox::Icon icon,
const QString& btnText1, QMessageBox::ButtonRole btnRole1,
const QString& btnText2, QMessageBox::ButtonRole btnRole2,
const QString& btnText3, QMessageBox::ButtonRole btnRole3)
: QMessageBox(icon, title, text,QMessageBox::NoButton, parent),
_button1(0), _button2(0), _button3(0)
{
_button1 = addButton(btnText1, btnRole1);
_button2 = addButton(btnText2, btnRole2);
_button3 = addButton(btnText3, btnRole3);
}
MessageBox::~MessageBox() {
}
QPushButton* MessageBox::addButton(const QString& btnText, QMessageBox::ButtonRole btnRole) {
if (btnRole != QMessageBox::InvalidRole) {
if (btnText.compare("") != 0) {
return QMessageBox::addButton(btnText, btnRole);
} else {
QString stdText("?");
switch (btnRole) {
case QMessageBox::AcceptRole: stdText = tr("&OK"); break;
case QMessageBox::RejectRole: stdText = tr("&Cancel"); break;
case QMessageBox::YesRole: stdText = tr("&Yes"); break;
case QMessageBox::NoRole: stdText = tr("&No"); break;
default: break;
}
return QMessageBox::addButton(stdText, btnRole);
}
}
return 0;
}
QPushButton* MessageBox::button(const unsigned int& index) const {
switch(index) {
case 0: return _button1;
case 1: return _button2;
case 2: return _button3;
default: return 0;
}
}
bool MessageBox::clicked_button(const unsigned int& index) const {
return this->clickedButton() == button(index);
}
示例用法...
MessageBox msg(0, tr("Add Icon"), tr("Icon list already contains this icon!"), QMessageBox::Warning);
msg.exec()