Qt/Widgets/FileMenu
From ProgrammingExamples
main.cpp
#include <QApplication> #include "filemenu.h" int main(int argc, char *argv[]) { QApplication app(argc, argv); MyForm form; form.show(); return app.exec(); }
#include "filemenu.h" #include <iostream> MyForm::MyForm(QWidget *parent) { setupUi(this); connect( this->mnuTest, SIGNAL( triggered() ), this, SLOT(mnuTest_triggered()) ); } void MyForm::mnuTest_triggered() { std::cout << "Clicked." << std::endl; }
#ifndef FILEMENUFORM_H #define FILEMENUFORM_H #include "ui_form.h" class MyForm : public QMainWindow, private Ui::FileMenuForm { Q_OBJECT public: MyForm(QWidget *parent = 0); public slots: void mnuTest_triggered(); }; #endif
main.ui
<?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>Test</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>24</height>
</rect>
</property>
<widget class="QMenu" name="menuFile">
<property name="title">
<string>File</string>
</property>
<addaction name="mnuTest"/>
</widget>
<addaction name="menuFile"/>
</widget>
<widget class="QStatusBar" name="statusbar"/>
<action name="mnuTest">
<property name="text">
<string>Test</string>
</property>
</action>
</widget>
<resources/>
<connections/>
</ui>CMakeLists.txt
cmake_minimum_required(VERSION 2.6) PROJECT(FileMenu) FIND_PACKAGE(Qt4 REQUIRED) INCLUDE(${QT_USE_FILE}) QT4_WRAP_UI(UISrcs main.ui) QT4_WRAP_CPP(MOCSrcs filemenu.h) include_directories(${CMAKE_CURRENT_SOURCE_DIR} ${CMAKE_CURRENT_BINARY_DIR}) ADD_EXECUTABLE(FileMenu main.cpp filemenu.h filemenu.cpp ${MOCSrcs} ${UISrcs}) TARGET_LINK_LIBRARIES(FileMenu ${QT_LIBRARIES})