为多个选项卡和初始组合框创建QT Ui

时间:2013-11-23 15:42:57

标签: qt user-interface tabs connect

我的设置如下:

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

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

但是我收到了这个错误:

错误是

projectQt.obj : error LNK2019: unresolved external symbol "public: __thiscall Ctab1::~Ctab1(void)" (??1Ctab1@@QAE@XZ) referenced in function "public: __thiscall ProjectQt::ProjectQt(class QWidget *)" (??0ProjectQt@@QAE@PAVQWidget@@@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(){

}

1 个答案:

答案 0 :(得分:2)

每当你看到“未定义引用...”的错误时,就意味着它是一个链接器错误。

这个特殊的错误意味着你的程序中的某个地方正在调用Ctab1类的析构函数,但是它无法在你的任何源文件中找到该析构函数的实现,只有声明。

将以下内容添加到tab1.cpp文件中:

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

很明显,您还没有完全理解C ++中的变量范围。 (ctab11ctab22仅存在于ProjectQt

的构造函数中

我建议您在继续学习项目之前先参加C / C ++的入门课程。