PDA

View Full Version : How to inherit Qt designer create a ui and class?



tiaoweiliao
22nd February 2017, 08:05
hi , all.

i use Qtcreator creator Qt designer ui and class .

i try use the ui pointer in my child class.

parent class code , in widget.h :


namespace Ui {
class Widget;
}
class Widget : public QWidget
{
Q_OBJECT
public:
explicit Widget(QWidget *parent = 0);
~Widget();
Ui::Widget *ui; //ui is public
private slots:
void on_pushButton_clicked();
};


child class code , in widget_child.h :


class Widget_Child : public Widget
{
public:
WidgetB_Child(QWidget * parent = 0);
};


child class code , in widget_child.cpp :


Widget_Child::Widget_Child(QWidget * parent) :
Widget(parent)
{
ui->pushButton->setText("Widget_Child");
}


a compiler error:
E:\QtCode\Test\widget_child.cpp:6: error: request for member 'pushButton' in '((Widget_Child*)this)->Widget_Child::<anonymous>.Widget::ui', which is of pointer type 'Ui::Widget*' (maybe you meant to use '->' ?)
ui.pushButton->setText("Widget_Child");
^

i use QtCreator 4.0 Qt5.4.2 , Win7 64bit

Added after 18 minutes:

i add #include<ui_widget.h> is no problem.

but , I don't know public:Ui::Widget *ui; whether the effect was encapsulation ?

anda_skoa
22nd February 2017, 08:49
You should read the compiler error.

The code you posted is the solution to the problem reported by it.

Cheers,
_