类和继承qt
Classes and inheritance qt
我有两个类:
.cpp
MainWindow::MainWindow(QWidget *parent):
QMainWindow(parent) {
}
...
Inheritor:Inheritor(QWidget *parent):
MainWindow(parent) {
...
}
.h
class MainWindow : public QMainWindow
{
Q_OBJECT
QWidget *centralWidget;
public:
MainWindow (QWidget* parent=0);
QwtPlot *funPlot;
}
class Inheritor : public MainWindow
{
Q_OBJECT
QWidget *centralWidget;
public:
Inheritor (QWidget* parent=0);
...
}
这是正确的认识吗?我有一个分割错误。我想它就在这里。我使用在MainWindow类中声明的可变funPlot。也许我的遗产是错误的。你能帮我吗?
void Inheritor::setCheckBox() {
first_b = new QCheckBox("option 1");
second_b = new QCheckBox("option 2");
third_b = new QCheckBox("option 3");
fourth_b = new QCheckBox("option 4");
QPushButton *drawButton = new QPushButton("Draw");
QVBoxLayout *rightLayout = new QVBoxLayout;
rightLayout->addWidget(first_b);
rightLayout->addWidget(second_b);
rightLayout->addWidget(third_b);
rightLayout->addWidget(fourth_b);
rightLayout->addWidget(drawButton);
rightLayout->addStretch();
QHBoxLayout *mainLayout = new QHBoxLayout;
mainLayout->addWidget(funPlot);
mainLayout->addLayout(rightLayout);
QWidget *cent = new QWidget();
cent->setLayout(mainLayout);
setCentralWidget(cent);
resize(640,480);
connect(first_b, SIGNAL(clicked()), this, SLOT(addPoints()));
connect(second_b, SIGNAL(clicked()), this, SLOT(addPoints()));
connect(third_b, SIGNAL(clicked()), this, SLOT(addPoints()));
connect(fourth_b, SIGNAL(clicked()), this, SLOT(addPoints()));
drawButton->setCheckable(true);
connect(drawButton, SIGNAL(clicked()), this, SLOT(drawCurve()));
}
funPlot
指针未分配。
MainWindow::MainWindow(QWidget *parent):QMainWindow(parent)
{
funPlot = new QwtPlot( this );
}
如果没有分配,程序很可能会在使用funPlot
时发生seg故障(就像在Inheritor::setCheckBox
中使用mainLayout->addWidget(funPlot);
时一样)
相关文章:
- 如何在QT Creator上将QWidget声明为继承类的对象
- Qt Quick - 如何在 qml 接口中使用继承自 QQuickPaintedItem 的 c++ 类?
- 具有 QObject 继承的单例 - Qt
- 类 QLayout 和类 QMainWindow Qt c++ 之间的继承
- 如何在基类中使用来自qt中所有继承类的信号?
- Qt:如何访问继承的小部件?
- 如何列出从Qt中Qobject继承的类的所有成员变量
- QDialog 继承类的 Qt 调用成员函数
- QT:私人会员而不是继承?原因是什么?这是一个具体的概念吗?
- Qt 无法继承属性
- 如何在Qt Creator中从继承的类自动创建虚拟方法
- 在构造函数 QT 创建器(继承类)中传递默认参数
- Qt 中的 c++ 继承基础
- QT:继承导致模棱两可
- qt qsharedDatapointer多重继承钻石图案
- Qt 访问继承类的 D 指针
- 使用CMake构建Qt项目并从QMainWindow继承会导致未引用的vtable错误
- C++Qt继承QMessageBox以延迟用户输入,以防止意外操作
- Qt:继承的信号不能在子类中识别
- Qt继承和实例化问题