Qt/Widgets/WidgetInsideMainWindow
From ProgrammingExamples
< Qt
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 mnuOpenInnerForm_triggered(); }; #endif
#include "filemenu.h" #include "InsideForm.h" #include <iostream> MyForm::MyForm(QWidget *parent) { setupUi(this); connect( this->mnuOpenInnerForm, SIGNAL( triggered() ), this, SLOT(mnuOpenInnerForm_triggered()) ); } void MyForm::mnuOpenInnerForm_triggered() { std::cout << "Inner form." << std::endl; MyInsideForm* insideForm = new MyInsideForm(this); this->setCentralWidget(insideForm); }
InsideForm.h
#ifndef INSIDEFORM_H #define INSIDEFORM_H #include "ui_InsideForm.h" class MyInsideForm : public QWidget, private Ui::InsideForm { Q_OBJECT public: MyInsideForm(QWidget *parent = 0); public slots: void btnButton_clicked(); }; #endif
InsideForm.cpp
#include "InsideForm.h" #include <iostream> MyInsideForm::MyInsideForm(QWidget *parent) { setupUi(this); connect( this->btnButton, SIGNAL( clicked() ), this, SLOT(btnButton_clicked()) ); } void MyInsideForm::btnButton_clicked() { this->lblLabel->setText("Clicked"); }
<?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="mnuOpenInnerForm"/>
</widget>
<addaction name="menuFile"/>
</widget>
<widget class="QStatusBar" name="statusbar"/>
<action name="mnuOpenInnerForm">
<property name="text">
<string>Create Int Form</string>
</property>
</action>
<action name="mnuDoubleForm">
<property name="text">
<string>CreateDoubleForm</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(WidgetInsideMainWindow) 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(WidgetInsideMainWindow main.cpp filemenu.cpp InsideForm.cpp ${MOCSrcs} ${UISrcs}) TARGET_LINK_LIBRARIES(WidgetInsideMainWindow ${QT_LIBRARIES})