简体   繁体   English

QWidget从自定义QWidget继承

[英]QWidget inherit from custom QWidget

在Qt C ++,是否有可能创建一个自定义QWidget ,然后再用这个自定义QWidget所有QWidget (即从自定义继承所有QWidget项目)?

Maybe I have misunderstood the question, but you can just create your custom QWidget , then use it everywhere. 也许我误解了这个问题,但是您只能创建自定义QWidget ,然后在任何地方使用它。

class derivedQWidget : public QWidget
{
  Q_OBJECT

  derivedQWidget();
  virtual ~derivedQWidget();
}

class myWidget : public derivedQWidget 
{
  ...
}

class myWidget2 : public derivedQWidget 
{
  ...
}

If the question is: Can we reimplement QWidget ?, no you can't. 如果问题是:我们可以重新实现QWidget吗,不,您不能。

i have solved in this mode: 我已经在这种模式下解决了:

the first class, Widget.h : 第一类Widget.h

#ifndef WIDGET_H
#define WIDGET_H

#include <QPushButton>
#include <QMouseEvent>
#include <QWidget>

namespace Ui {
class Widget;
}

class Widget : public QWidget
{
    Q_OBJECT

public:
    Widget(QWidget *parent = 0);
    virtual ~Widget();

    QPushButton *getBtn() const;
    void setBtn(QPushButton *value);

protected:
    void mousePressEvent(QMouseEvent *evt);
    void mouseMoveEvent(QMouseEvent *evt);

private:
    Ui::Widget *ui;
    QPushButton *btn;
    QPoint oldPos;
};

and the second class widExt.h , that inherit from Widget: 第二个类widExt.h ,它继承自Widget:

#ifndef WIDEXT_H
#define WIDEXT_H

#include "widget.h"

namespace Ui {
    class widExt;
}

class widExt : public Widget
{
public:
    widExt();


private slots:
    void on_dial_2_actionTriggered(int action);

private:
    Ui::widExt *ui;
};

#endif // WIDEXT_H

with the relative widExt.cpp : 与相对widExt.cpp

#include "widext.h"
#include "ui_widext.h"

widExt::widExt() : ui(new Ui::widExt)
{
    ui->setupUi(this);
}

void widExt::on_dial_2_actionTriggered(int action)
{

}

in this mode, i inherit all from the first class and i can customize other classes independently. 在这种模式下,我从第一类继承所有内容,并且可以独立自定义其他类。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM