Difference between revisions of "Qt/Widgets/WidgetTemplated"
From ProgrammingExamples
< Qt
Daviddoria (Talk | contribs) (Created page with '==main.cpp== <source lang="cpp"> #include <QApplication> #include "filemenu.h" int main(int argc, char *argv[]) { QApplication app(argc, argv); MyForm form; form.s…') |
Daviddoria (Talk | contribs) |
||
Line 75: | Line 75: | ||
#include "ui_InsideForm.h" | #include "ui_InsideForm.h" | ||
− | class InsideFormObject : public QWidget, | + | class InsideFormObject : public QWidget, public Ui::InsideForm |
{ | { | ||
Q_OBJECT | Q_OBJECT | ||
Line 86: | Line 86: | ||
template <typename T> | template <typename T> | ||
− | class MyInsideForm : public InsideFormObject | + | class MyInsideForm : public InsideFormObject |
{ | { | ||
public: | public: | ||
Line 130: | Line 130: | ||
void MyInsideForm<T>::btnButton_clicked() | void MyInsideForm<T>::btnButton_clicked() | ||
{ | { | ||
− | |||
this->SetValue(2.1); | this->SetValue(2.1); | ||
− | std:: | + | std::stringstream ss; |
+ | ss << this->value; | ||
+ | this->lblLabel->setText(ss.str().c_str()); | ||
} | } | ||
+ | |||
</source> | </source> |
Latest revision as of 09:57, 10 February 2011
Contents
main.cpp
#include <QApplication> #include "filemenu.h" int main(int argc, char *argv[]) { QApplication app(argc, argv); MyForm form; form.show(); return app.exec(); }
#ifndef FILEMENUFORM_H #define FILEMENUFORM_H #include "ui_filemenu.h" class MyForm : public QMainWindow, private Ui::FileMenuForm { Q_OBJECT public: MyForm(QWidget *parent = 0); public slots: void mnuCreateIntForm_triggered(); void mnuCreateDoubleForm_triggered(); }; #endif
#include "filemenu.h" #include "InsideForm.h" #include <iostream> MyForm::MyForm(QWidget *parent) { setupUi(this); connect( this->mnuCreateIntForm, SIGNAL( triggered() ), this, SLOT(mnuCreateIntForm_triggered()) ); connect( this->mnuCreateDoubleForm, SIGNAL( triggered() ), this, SLOT(mnuCreateDoubleForm_triggered()) ); } void MyForm::mnuCreateIntForm_triggered() { std::cout << "Inner form." << std::endl; MyInsideForm<int>* insideForm = new MyInsideForm<int>(this); this->setCentralWidget(insideForm); } void MyForm::mnuCreateDoubleForm_triggered() { std::cout << "Inner form." << std::endl; MyInsideForm<double>* insideForm = new MyInsideForm<double>(this); this->setCentralWidget(insideForm); }
InsideForm.h
#ifndef INSIDEFORM_H #define INSIDEFORM_H #include "ui_InsideForm.h" class InsideFormObject : public QWidget, public Ui::InsideForm { Q_OBJECT public: InsideFormObject(QWidget *parent = 0); public slots: virtual void btnButton_clicked() = 0; }; template <typename T> class MyInsideForm : public InsideFormObject { public: void btnButton_clicked(); MyInsideForm(QWidget *parent = 0); void SetValue(T temp) {this->value = temp;} T value; }; #include "InsideForm.txx" #endif
InsideForm.cpp
#include "InsideForm.h" #include <iostream> InsideFormObject::InsideFormObject(QWidget *parent) { setupUi(this); connect( this->btnButton, SIGNAL( clicked() ), this, SLOT(btnButton_clicked()) ); }
InsideForm.txx
#include <iostream> template <typename T> MyInsideForm<T>::MyInsideForm(QWidget *parent) { } template <typename T> void MyInsideForm<T>::btnButton_clicked() { this->SetValue(2.1); std::stringstream ss; ss << this->value; this->lblLabel->setText(ss.str().c_str()); }
<?xml version="1.0" encoding="UTF-8"?> <ui version="4.0"> <class>FileMenuForm</class> <widget class="QMainWindow" name="FileMenuForm"> <property name="geometry"> <rect> <x>0</x> <y>0</y> <width>800</width> <height>600</height> </rect> </property> <property name="windowTitle"> <string>MainWindow</string> </property> <widget class="QWidget" name="centralwidget"/> <widget class="QMenuBar" name="menubar"> <property name="geometry"> <rect> <x>0</x> <y>0</y> <width>800</width> <height>25</height> </rect> </property> <widget class="QMenu" name="menuFile"> <property name="title"> <string>File</string> </property> <addaction name="mnuCreateIntForm"/> <addaction name="mnuCreateDoubleForm"/> </widget> <addaction name="menuFile"/> </widget> <widget class="QStatusBar" name="statusbar"/> <action name="mnuCreateIntForm"> <property name="text"> <string>Create Int Form</string> </property> </action> <action name="mnuDoubleForm"> <property name="text"> <string>CreateDoubleForm</string> </property> </action> <action name="mnuCreateDoubleForm"> <property name="text"> <string>Create Double Form</string> </property> </action> </widget> <resources/> <connections/> </ui>
InsideForm.ui
<?xml version="1.0" encoding="UTF-8"?> <ui version="4.0"> <class>InsideForm</class> <widget class="QWidget" name="InsideForm"> <property name="geometry"> <rect> <x>0</x> <y>0</y> <width>400</width> <height>300</height> </rect> </property> <property name="windowTitle"> <string>Form</string> </property> <widget class="QPushButton" name="btnButton"> <property name="geometry"> <rect> <x>150</x> <y>90</y> <width>92</width> <height>27</height> </rect> </property> <property name="text"> <string>Click me</string> </property> </widget> <widget class="QLabel" name="lblLabel"> <property name="geometry"> <rect> <x>180</x> <y>140</y> <width>59</width> <height>17</height> </rect> </property> <property name="text"> <string>TextLabel</string> </property> </widget> </widget> <resources/> <connections/> </ui>
CMakeLists.txt
cmake_minimum_required(VERSION 2.6) PROJECT(WidgetTemplated) FIND_PACKAGE(Qt4 REQUIRED) INCLUDE(${QT_USE_FILE}) QT4_WRAP_UI(UISrcs filemenu.ui InsideForm.ui) QT4_WRAP_CPP(MOCSrcs filemenu.h InsideForm.h) include_directories(${include_directories} ${CMAKE_CURRENT_SOURCE_DIR} ${CMAKE_CURRENT_BINARY_DIR}) ADD_EXECUTABLE(WidgetTemplated main.cpp filemenu.cpp InsideForm.cpp ${MOCSrcs} ${UISrcs}) TARGET_LINK_LIBRARIES(WidgetTemplated ${QT_LIBRARIES})