Difference between revisions of "Qt/Widgets/MessageBoxFromThread"
From ProgrammingExamples
< Qt
Daviddoria (Talk | contribs) (Created page with '==MessageBoxFromThread.cxx== <source lang="cpp"> #include <QThread> #include <QMessageBox> #include <QApplication> #include <iostream> class MyThread : public QThread { public…') |
Daviddoria (Talk | contribs) m |
||
| (4 intermediate revisions by the same user not shown) | |||
| Line 1: | Line 1: | ||
==MessageBoxFromThread.cxx== | ==MessageBoxFromThread.cxx== | ||
<source lang="cpp"> | <source lang="cpp"> | ||
| − | |||
| − | |||
#include <QApplication> | #include <QApplication> | ||
#include <iostream> | #include <iostream> | ||
| − | + | #include "form.h" | |
| + | |||
| + | int main(int argc, char*argv[]) | ||
{ | { | ||
| + | QApplication app(argc, argv); | ||
| + | Form form; | ||
| + | form.show(); | ||
| + | |||
| + | return app.exec(); | ||
| + | } | ||
| + | </source> | ||
| + | |||
| + | ==thread.h== | ||
| + | <source lang="cpp"> | ||
| + | #ifndef THREAD_H | ||
| + | #define THREAD_H | ||
| + | |||
| + | #include <QThread> | ||
| + | |||
| + | class Thread : public QThread | ||
| + | { | ||
| + | Q_OBJECT | ||
public: | public: | ||
| + | void run(); | ||
| + | |||
| + | signals: | ||
| + | void DisplayMessageBoxSignal(); | ||
| − | |||
}; | }; | ||
| − | void | + | #endif |
| + | </source> | ||
| + | |||
| + | ==thread.cpp== | ||
| + | <source lang="cpp"> | ||
| + | #include "thread.h" | ||
| + | |||
| + | #include <QMessageBox> | ||
| + | #include <iostream> | ||
| + | |||
| + | void Thread::run() | ||
| + | { | ||
| + | std::cout << "Thread::run()" << std::endl; | ||
| + | emit DisplayMessageBoxSignal(); | ||
| + | } | ||
| + | </source> | ||
| + | |||
| + | ==form.h== | ||
| + | <source lang="cpp"> | ||
| + | #ifndef BUTTONFORM_H | ||
| + | #define BUTTONFORM_H | ||
| + | |||
| + | #include <QWidget> | ||
| + | #include "ui_form.h" | ||
| + | #include "thread.h" | ||
| + | |||
| + | class Form : public QWidget, private Ui::Form | ||
| + | { | ||
| + | Q_OBJECT | ||
| + | public: | ||
| + | Form(QWidget *parent = 0); | ||
| + | |||
| + | public slots: | ||
| + | void pushButton_clicked(); | ||
| + | void DisplayMessageBoxSlot(); | ||
| + | |||
| + | private: | ||
| + | Thread MyThread; | ||
| + | }; | ||
| + | |||
| + | #endif | ||
| + | |||
| + | </source> | ||
| + | |||
| + | ==form.cpp== | ||
| + | <source lang="cpp"> | ||
| + | #include <QMessageBox> | ||
| + | |||
| + | #include "form.h" | ||
| + | #include "thread.h" | ||
| + | |||
| + | Form::Form(QWidget *parent) : QWidget(parent) | ||
| + | { | ||
| + | setupUi(this); | ||
| + | connect( this->pushButton, SIGNAL( clicked() ), this, SLOT(pushButton_clicked()) ); | ||
| + | connect( &(this->MyThread), SIGNAL( DisplayMessageBoxSignal() ), this, SLOT(DisplayMessageBoxSlot()) ); | ||
| + | } | ||
| + | |||
| + | void Form::pushButton_clicked() | ||
| + | { | ||
| + | MyThread.start(); | ||
| + | } | ||
| + | |||
| + | void Form::DisplayMessageBoxSlot() | ||
{ | { | ||
| − | |||
| − | |||
QMessageBox msgBox; | QMessageBox msgBox; | ||
msgBox.setText("Test Text"); | msgBox.setText("Test Text"); | ||
msgBox.exec(); | msgBox.exec(); | ||
} | } | ||
| − | + | </source> | |
| − | + | ||
| − | + | ||
| − | + | ||
| − | + | ==form.ui== | |
| − | + | <source lang="text"> | |
| + | <?xml version="1.0" encoding="UTF-8"?> | ||
| + | <ui version="4.0"> | ||
| + | <class>Form</class> | ||
| + | <widget class="QWidget" name="Form"> | ||
| + | <property name="geometry"> | ||
| + | <rect> | ||
| + | <x>0</x> | ||
| + | <y>0</y> | ||
| + | <width>400</width> | ||
| + | <height>300</height> | ||
| + | </rect> | ||
| + | </property> | ||
| + | <property name="windowTitle"> | ||
| + | <string>My Program</string> | ||
| + | </property> | ||
| + | <widget class="QPushButton" name="pushButton"> | ||
| + | <property name="geometry"> | ||
| + | <rect> | ||
| + | <x>130</x> | ||
| + | <y>100</y> | ||
| + | <width>91</width> | ||
| + | <height>28</height> | ||
| + | </rect> | ||
| + | </property> | ||
| + | <property name="text"> | ||
| + | <string>Click me</string> | ||
| + | </property> | ||
| + | </widget> | ||
| + | </widget> | ||
| + | <resources/> | ||
| + | <connections/> | ||
| + | </ui> | ||
| − | |||
| − | |||
</source> | </source> | ||
| Line 39: | Line 148: | ||
cmake_minimum_required(VERSION 2.6) | cmake_minimum_required(VERSION 2.6) | ||
| − | PROJECT( | + | PROJECT(MessageBoxFromThread) |
FIND_PACKAGE(Qt4 REQUIRED) | FIND_PACKAGE(Qt4 REQUIRED) | ||
INCLUDE(${QT_USE_FILE}) | INCLUDE(${QT_USE_FILE}) | ||
| + | |||
| + | QT4_WRAP_UI(UISrcs form.ui) | ||
| + | QT4_WRAP_CPP(MOCSrcs form.h) | ||
include_directories(${include_directories} ${CMAKE_CURRENT_SOURCE_DIR} ${CMAKE_CURRENT_BINARY_DIR}) | include_directories(${include_directories} ${CMAKE_CURRENT_SOURCE_DIR} ${CMAKE_CURRENT_BINARY_DIR}) | ||
| − | ADD_EXECUTABLE( | + | ADD_EXECUTABLE(MessageBoxFromThread MessageBoxFromThread.cpp form.cpp thread.cpp ${MOCSrcs} ${UISrcs}) |
| − | TARGET_LINK_LIBRARIES( | + | TARGET_LINK_LIBRARIES(MessageBoxFromThread ${QT_LIBRARIES}) |
</source> | </source> | ||
Latest revision as of 20:27, 28 November 2010
Contents
MessageBoxFromThread.cxx
#include <QApplication> #include <iostream> #include "form.h" int main(int argc, char*argv[]) { QApplication app(argc, argv); Form form; form.show(); return app.exec(); }
thread.h
#ifndef THREAD_H #define THREAD_H #include <QThread> class Thread : public QThread { Q_OBJECT public: void run(); signals: void DisplayMessageBoxSignal(); }; #endif
thread.cpp
#include "thread.h" #include <QMessageBox> #include <iostream> void Thread::run() { std::cout << "Thread::run()" << std::endl; emit DisplayMessageBoxSignal(); }
form.h
#ifndef BUTTONFORM_H #define BUTTONFORM_H #include <QWidget> #include "ui_form.h" #include "thread.h" class Form : public QWidget, private Ui::Form { Q_OBJECT public: Form(QWidget *parent = 0); public slots: void pushButton_clicked(); void DisplayMessageBoxSlot(); private: Thread MyThread; }; #endif
form.cpp
#include <QMessageBox> #include "form.h" #include "thread.h" Form::Form(QWidget *parent) : QWidget(parent) { setupUi(this); connect( this->pushButton, SIGNAL( clicked() ), this, SLOT(pushButton_clicked()) ); connect( &(this->MyThread), SIGNAL( DisplayMessageBoxSignal() ), this, SLOT(DisplayMessageBoxSlot()) ); } void Form::pushButton_clicked() { MyThread.start(); } void Form::DisplayMessageBoxSlot() { QMessageBox msgBox; msgBox.setText("Test Text"); msgBox.exec(); }
form.ui
<?xml version="1.0" encoding="UTF-8"?>
<ui version="4.0">
<class>Form</class>
<widget class="QWidget" name="Form">
<property name="geometry">
<rect>
<x>0</x>
<y>0</y>
<width>400</width>
<height>300</height>
</rect>
</property>
<property name="windowTitle">
<string>My Program</string>
</property>
<widget class="QPushButton" name="pushButton">
<property name="geometry">
<rect>
<x>130</x>
<y>100</y>
<width>91</width>
<height>28</height>
</rect>
</property>
<property name="text">
<string>Click me</string>
</property>
</widget>
</widget>
<resources/>
<connections/>
</ui>CMakeLists.txt
cmake_minimum_required(VERSION 2.6) PROJECT(MessageBoxFromThread) FIND_PACKAGE(Qt4 REQUIRED) INCLUDE(${QT_USE_FILE}) QT4_WRAP_UI(UISrcs form.ui) QT4_WRAP_CPP(MOCSrcs form.h) include_directories(${include_directories} ${CMAKE_CURRENT_SOURCE_DIR} ${CMAKE_CURRENT_BINARY_DIR}) ADD_EXECUTABLE(MessageBoxFromThread MessageBoxFromThread.cpp form.cpp thread.cpp ${MOCSrcs} ${UISrcs}) TARGET_LINK_LIBRARIES(MessageBoxFromThread ${QT_LIBRARIES})