【问题标题】:Subclassing QMessageBox Static API子类化 QMessageBox 静态 API
【发布时间】:2017-02-28 18:24:10
【问题描述】:

我创建了一个名为CustomQMessageBoxQMessageBox 子类,我希望在子类的构造函数中执行某些操作。这是我的课:

标题:

#ifndef CUSTOMQMESSAGEBOX_H
#define CUSTOMQMESSAGEBOX_H
#include <QMessageBox>

class CustomQMessageBox : public QMessageBox
{
public:
    CustomQMessageBox();
};

#endif // CUSTOMQMESSAGEBOX_H

Cpp:

#include "customqmessagebox.h"

CustomQMessageBox::CustomQMessageBox()
{
    qDebug() << "yo";
}

当我创建CustomQMessageBox 的实例时,构造函数被调用,qDebug 表示yo。但是,如果我调用静态 API 方法,则不会调用构造函数。

//constructor is called
CustomQMessageBox cbox;

//constructor is not called
CustomQMessageBox::question(this, "Title", "Question?", QMessageBox::Yes | QMessageBox::No);

为什么我使用QMessageBox的静态方法时没有调用构造函数?

感谢您的宝贵时间。

【问题讨论】:

  • 静态方法不会实例化它们关联的类对象。除非你做了一个单例什么的。
  • @vincent 所以我的子类根本不能使用静态方法?
  • 您可以,但您将无法访问与实例化类关联的变量,除非它们也是静态的。你想做什么?
  • @vincent 啊。我问是因为我的程序中已经有几十个静态的QMessageBox 调用。我想要一种简单的方法来以相同的方式对所有它们进行样式化,并且认为子类化将是可行的方法......猜不......
  • 子类化是要走的路。我知道你的痛苦,我会给你一个例子。

标签: c++ qt qmessagebox


【解决方案1】:

这是我经常使用的一个类,它是从 QMessageBox 派生的。您可以使用构造函数来启动自定义按钮。如果你需要更具体的东西,你可以从这个类派生。

如果你想得到一个按钮角色,你可以调用 messageBox->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()

【讨论】:

  • 感谢您的回答!我喜欢你的方法,但我还不确定这是否是我要走的路。这不会返回被选中的QMessageBox::StandardButton 吗?
  • 如果你调用 msg->standardButton(QAbstractButton* btn),你可以。因此,您可以从类中检索特定按钮。
  • 我想我更重要的一点是,如果我仍然必须经历和编辑每个静态调用、命名按钮、调用 exec、调用 msg->standardButton,这种方法并不能真正让事情变得更容易等。那时,我最好只实例化类并使用属性 API。我很欣赏静态调用的简单性,我只想改变颜色和东西:P
  • 很公平。你总是可以写一些函数来做更多的事情。
猜你喜欢
  • 1970-01-01
  • 2012-09-07
  • 2011-05-27
  • 1970-01-01
  • 2010-10-10
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多