PDA

View Full Version : initial comboboxes at Qt using Ui



citix
24th November 2013, 13:18
I am new at Qt. I searched for some hours now but I'm not able to find a solution.

My setup is as follows:


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



I want to create a project with Ui file. At the Ui file, i have three comboBox and two Apply button and at the tab1.cpp and tab2.cpp, I want to add initial Items of the comboboxes. If the user click the apply buttons, run the "OnBtnApplyClicked" methods. I have error of the project. please help me.

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 file:




<?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 file:



#include "projectQt.h"
#include "tab1.h"
#include "tab2.h"
#include "ui_projectQt.h"
Ctab1 *ctab1=new Ctab1;
Ctab2 *ctab2=new Ctab2;
ProjectQt::ProjectQt(QWidget *parent)
: QMainWindow(parent),
ui(new Ui::ProjectQtClass)
{
ui->setupUi(this);

ctab1->initGUI();
ctab2->initGUI();
ui->IDC_CMB_2_0->addItem("123");

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 file:





#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();

Ui::ProjectQtClass *ui;

};

#endif // projectQt_H





tab1.h file:



#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();

};

tab1.cpp file:






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

Ctab1::Ctab1(void)
{

}

Ctab1::~Ctab1()

{

}

void Ctab1::initGUI(){

ui->IDC_CMB_1_1->addItem("adsa");

}


void Ctab1::OnBtnApplyClicked(){

}

 

 





tab2.h file:



#pragma once
#include <string>
#include "projectQt.h"
#include "ui_projectQt.h"
class Ctab2
{

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

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

};

tab2.cpp file:




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

Ctab2::Ctab2(void)
{

}

Ctab2::~Ctab2()

{

}

void Ctab2::initGUI(){

ui->IDC_CMB_2_0->addItem("adsaxxx");

}


void Ctab2::OnBtnApplyClicked(){

}

 



I cant declare ui identifier for Tab1 and Tab2. How can i declare it for Tab1.cpp and Tab2.cpp files. If i declare ui, I will solve the problem (I hope :D).

anda_skoa
25th November 2013, 18:33
Your connect() lines look wrong.
The SLOT() macro takes only the slot name and its argument types, not a class prefix. And the receiver object should probably not be "this" but the respective tab object, no?

Cheers,
_