[英]how to use objects of the widgets in QT
在我的應用程序中,我有2個名為widget
和form
widget
。 但是,如果我嘗試在小部件form
頭文件中創建小widget
小widget
的指針對象,則會出現類似“表單未命名類型”的錯誤。 請參閱下面我的二手代碼:
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
#include "widget.h"
Widget::Widget(QWidget *parent) :QWidget(parent)
{
setupUi(this);
}
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
表格
#include "form.h"
#include "widget.h"
Form::Form(QWidget *parent) :QWidget(parent)
{
setupUi(this);
}
形式
#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);
};
我做錯了什么?
您應該在widget.h中放置一個類Form
的前向聲明,而不是#include
includeform.h。 問題是您包含form.h,其中包括widget.h,該窗口試圖包含form.h,但由於包含保護而不能。 因此,在widget.h中,類Form
是未定義的,盡管它看起來是要定義的用戶。
問題是widget.h
包含form.h
,而widget.h
包括widget.h
。 標頭保護#ifndef
( #ifndef
)導致第二個include被跳過。
為了在標頭中聲明指針變量,前向聲明就足夠了:
SomeClass.h
class Form; // forward declaration
class SomeClass {
public:
SomeClass();
// ...
private:
Form* form; // pointer to Form
};
SomeClass.cpp
SomeClass::SomeClass()
{
form = new Form();
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.