2013-11-23 60 views
-1

我的设置如下:创建QT UI的多个选项卡和初始组合框

main.cpp 
projectQt.cpp 
projectQt.h 
tab1.h 
tab1.cpp 
tab2.h 
tab2.cpp 
projectQt.ui 
ui_projectQt.h 

我想创建一个UI文件的项目。在Ui文件中,我有三个组合框和两个应用按钮,并且在tab1.cpp和tab2.cpp中,我想添加组合框的初始项目。如果用户单击其中一个应用按钮,请运行“OnBtnApplyClicked”方法。

但是我得到这个错误:

错误是

projectQt.obj : error LNK2019: unresolved external symbol "public: __thiscall Ctab1::~Ctab1(void)" ([email protected]@[email protected]) referenced in function "public: __thiscall ProjectQt::ProjectQt(class QWidget *)" ([email protected]@[email protected]@@@Z) 
C:\output\Deneme\Qt\Win32\Debug\\projectQt.exe : fatal error LNK1120: 1 unresolved externals 

的main.cpp

#include "projectQt.h" 

    #include <QtWidgets/QApplication> 

    int main(int argc, char *argv[]) 
    { 
     QApplication a(argc, argv); 
     ProjectQt w; 
     w.show(); 
     return a.exec(); 
    } 

projectQt.ui文件:

<?xml version="1.0" encoding="UTF-8"?> 
<ui version="4.0"> 
<class>ProjectQtClass</class> 
<widget class="QMainWindow" name="ProjectQtClass"> 
    <property name="geometry"> 
    <rect> 
    <x>0</x> 
    <y>0</y> 
    <width>900</width> 
    <height>900</height> 
    </rect> 
    </property> 
    <property name="windowTitle"> 
    <string>ProjectQt</string> 
    </property> 
    <widget class="QWidget" name="centralWidget"> 
    <widget class="QTabWidget" name="tabWidget"> 
    <property name="geometry"> 
    <rect> 
     <x>10</x> 
     <y>40</y> 
     <width>800</width> 
     <height>800</height> 
    </rect> 
    </property> 
    <property name="currentIndex"> 
    <number>1</number> 
    </property> 
    <widget class="QWidget" name="IDC_FORM_TAB_1"> 
    <attribute name="title"> 
     <string>M001</string> 
    </attribute> 
    <widget class="QLabel" name="IDC_LBL_1_0"> 
     <property name="geometry"> 
     <rect> 
     <x>10</x> 
     <y>40</y> 
     <width>124</width> 
     <height>13</height> 
     </rect> 
     </property> 
     <property name="text"> 
     <string>COMBO1</string> 
     </property> 
    </widget> 
    <widget class="QComboBox" name="IDC_CMB_1_0"> 
     <property name="geometry"> 
     <rect> 
     <x>120</x> 
     <y>40</y> 
     <width>69</width> 
     <height>22</height> 
     </rect> 
     </property> 
    </widget> 
    <widget class="QComboBox" name="IDC_CMB_1_1"> 
     <property name="geometry"> 
     <rect> 
     <x>120</x> 
     <y>90</y> 
     <width>69</width> 
     <height>22</height> 
     </rect> 
     </property> 
    </widget> 
    <widget class="QPushButton" name="IDC_BTN_1_Apply"> 
     <property name="geometry"> 
     <rect> 
     <x>180</x> 
     <y>174</y> 
     <width>75</width> 
     <height>23</height> 
     </rect> 
     </property> 
     <property name="text"> 
     <string>Apply</string> 
     </property> 
    </widget> 
    <widget class="QLabel" name="IDC_LBL_1_1"> 
     <property name="geometry"> 
     <rect> 
     <x>10</x> 
     <y>90</y> 
     <width>124</width> 
     <height>13</height> 
     </rect> 
     </property> 
     <property name="text"> 
     <string>COMBO2</string> 
     </property> 
    </widget> 
    </widget> 
    <widget class="QWidget" name="IDC_FORM_TAB_2"> 
    <attribute name="title"> 
     <string>M002</string> 
    </attribute> 
    <widget class="QLabel" name="IDC_LBL_2_0"> 
     <property name="geometry"> 
     <rect> 
     <x>0</x> 
     <y>30</y> 
     <width>104</width> 
     <height>13</height> 
     </rect> 
     </property> 
     <property name="text"> 
     <string>COMBO3</string> 
     </property> 
    </widget> 
    <widget class="QComboBox" name="IDC_CMB_2_0"> 
     <property name="geometry"> 
     <rect> 
     <x>100</x> 
     <y>30</y> 
     <width>69</width> 
     <height>22</height> 
     </rect> 
     </property> 
    </widget> 
    <widget class="QPushButton" name="IDC_BTN_2_Apply"> 
     <property name="geometry"> 
     <rect> 
     <x>100</x> 
     <y>70</y> 
     <width>75</width> 
     <height>23</height> 
     </rect> 
     </property> 
     <property name="text"> 
     <string>Apply</string> 
     </property> 
    </widget> 
    </widget> 
    </widget> 
    </widget> 
    <widget class="QMenuBar" name="menuBar"> 
    <property name="geometry"> 
    <rect> 
    <x>0</x> 
    <y>0</y> 
    <width>900</width> 
    <height>21</height> 
    </rect> 
    </property> 
    </widget> 
    <widget class="QToolBar" name="mainToolBar"> 
    <attribute name="toolBarArea"> 
    <enum>TopToolBarArea</enum> 
    </attribute> 
    <attribute name="toolBarBreak"> 
    <bool>false</bool> 
    </attribute> 
    </widget> 
    <widget class="QStatusBar" name="statusBar"/> 
</widget> 
<layoutdefault spacing="6" margin="11"/> 
<resources> 
    <include location="projectQt.qrc"/> 
</resources> 
<connections/> 
</ui> 

projectQt.cpp文件:

#include "projectQt.h" 
#include "tab1.h" 
#include "tab2.h" 
#include "ui_projectQt.h" 


ProjectQt::ProjectQt(QWidget *parent) 
    : QMainWindow(parent) 
{ 
    ui.setupUi(this); 

    Ctab1 ctab11; 
    Ctab1 ctab22; 
    ctab11.initGUI(); 
    ctab22.initGUI(); 

    connect(ui.IDC_BTN_1_Apply,SIGNAL(clicked()),this,SLOT(Ctab1::OnBtnApplyClicked())); 
    connect(ui.IDC_BTN_2_Apply,SIGNAL(clicked()),this,SLOT(Ctab2::OnBtnApplyClicked())); 

} 

ProjectQt::~ProjectQt() 
{ 

} 

projectQt.h文件:

#ifndef projectQt_H 
#define projectQt_H 

#include <QtWidgets/QMainWindow> 
#include "ui_projectQt.h" 
#include "tab1.h" 
#include "tab2.h" 

class ProjectQt : public QMainWindow 
{ 
    Q_OBJECT 

public: 
    ProjectQt(QWidget *parent = 0); 
    ~ProjectQt(); 

//private: 
    Ui::ProjectQtClass ui; 
}; 

#endif // projectQt_H 

tab1.h文件:

#pragma once 

#include <string> 
#include "projectQt.h" 
#include "ui_projectQt.h" 

class Ctab1 
{ 

public: 
    Ctab1(void); 
    ~Ctab1(void); 

public slots: 
    void setEvents(); 
    void initGUI(); 
    void OnBtnApplyClicked(); 

//private: 
    //Ui::ProjectQtClass *ui; 

}; 

tab1.cpp文件:

#include "projectQt.h" 
#include "tab1.h" 
#include <QTextStream> 
#include "ui_projectQt.h" 

Ctab1::Ctab1(void) 
{ 
} 

void Ctab1::initGUI(){ 
    Ui::ProjectQtClass uimain; 
    uimain.IDC_CMB_1_0->addItem("1100"); 
    uimain.IDC_CMB_1_0->addItem("1101"); 
    uimain.IDC_CMB_1_1->addItem("1200"); 
    uimain.IDC_CMB_1_1->addItem("1201"); 

} 

void Ctab1::OnBtnApplyClicked(){ 

} 

tab2.h文件:

#pragma once 

#include <string> 
#include "projectQt.h" 
#include "ui_projectQt.h" 

class Ctab2 
{ 

public: 
    Ctab2(void); 
    ~Ctab2(void); 

public slots: 
    void setEvents(); 
    void initGUI(); 
    void OnBtnApplyClicked(); 

//private: 
    //Ui::ProjectQtClass *ui; 

}; 

tab2.cpp文件:

#include "projectQt.h" 
#include "tab2.h" 
#include <QTextStream> 
#include "ui_projectQt.h" 

Ctab2::Ctab2(void) 
{ 
} 

void Ctab2::initGUI(){ 

    Ui::ProjectQtClass uimain; 
    uimain.IDC_CMB_2_0->addItem("2000"); 
    uimain.IDC_CMB_2_0->addItem("2001"); 

} 

void Ctab2::OnBtnApplyClicked(){ 

} 
+0

你不会指定正是你有什么样的错误。 – eraxillan

+0

我添加错误... – CiTi

+0

谢谢。现在,请查看下面的@RobbieE的答案,我认为这有助于解决问题。 – eraxillan

回答

2

每当你看到沿着“未定义参考......”行一个错误,这意味着它是一个连接错误。

这个特定的错误意味着在你的程序的某个地方,Ctab1类的析构函数被调用,但它不能在你的任何源文件中找到那个析构函数的实现,只有声明。

以下内容添加到您的tab1.cpp文件:

Ctab1::~Ctab1(){ 
    // any cleanup code placed here 
} 

它也似乎很清楚,你不完全了解的变量范围在C++中。 (ctab11ctab22只有ProjectQt

的构造,我建议您与您的项目在继续前走在C/C++的入门课程里面存在。

+0

look look of mine – CiTi

相关问题