[英]how to use objects of the widgets in QT
In my application I'm having 2 widgets named as widget
and form
. 在我的应用程序中,我有2个名为
widget
和form
widget
。 But if i try to create a pointer object of widget widget
in widget form
header file , it is giving the error like "Form does not name a type". 但是,如果我尝试在小部件
form
头文件中创建小widget
小widget
的指针对象,则会出现类似“表单未命名类型”的错误。 Refer my used code below: 请参阅下面我的二手代码:
main.cpp main.cpp
#include <QtGui/QApplication>
#include "widget.h"
#include "form.h"
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
Widget *w = new Widget();
w->show();
return a.exec();
}
widget.cpp widget.cpp
#include "widget.h"
Widget::Widget(QWidget *parent) :QWidget(parent)
{
setupUi(this);
}
widget.h widget.h
#ifndef WIDGET_H
#define WIDGET_H
#include "ui_widget.h"
#include "form.h"
class Widget : public QWidget, private Ui::Widget
{
Q_OBJECT
public:
explicit Widget(QWidget *parent = 0);
Form *f ;//i try to create pointer object for Form
};
#endif // WIDGET_H
form.cpp 表格
#include "form.h"
#include "widget.h"
Form::Form(QWidget *parent) :QWidget(parent)
{
setupUi(this);
}
form.h 形式
#ifndef FORM_H
#define FORM_H
#include "ui_form.h"
#include "widget.h"
class Form : public QWidget, private Ui::Form
{
Q_OBJECT
public:
explicit Form(QWidget *parent = 0);
};
What I'm doing wrong? 我做错了什么?
You should put a forward declaration of class Form
in widget.h instead of #include
ing form.h. 您应该在widget.h中放置一个类
Form
的前向声明,而不是#include
includeform.h。 The problem is that you include form.h, which includes widget.h, which tries to include form.h, but can't because of the include guard. 问题是您包含form.h,其中包括widget.h,该窗口试图包含form.h,但由于包含保护而不能。 Therefore, in widget.h, class
Form
is undefined, although it looks to the user to be defined. 因此,在widget.h中,类
Form
是未定义的,尽管它看起来是要定义的用户。
The problem is that widget.h
includes form.h
, which includes widget.h
. 问题是
widget.h
包含form.h
,而widget.h
包括widget.h
。 The header guards ( #ifndef
) cause the second include to be skipped. 标头保护
#ifndef
( #ifndef
)导致第二个include被跳过。
For declaring a pointer variable in a header a forward declaration will suffice: 为了在标头中声明指针变量,前向声明就足够了:
SomeClass.h SomeClass.h
class Form; // forward declaration
class SomeClass {
public:
SomeClass();
// ...
private:
Form* form; // pointer to Form
};
SomeClass.cpp SomeClass.cpp
SomeClass::SomeClass()
{
form = new Form();
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.