Qt/Widgets/ThreadClassMessageBox

From ProgrammingExamples
< Qt
Revision as of 21:30, 28 November 2010 by Daviddoria (Talk | contribs)

(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to: navigation, search

This example is currently broken. How do you produce a message box in the TestClass::MyFunction() ?

ThreadClassMessageBox.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();
}

TestClass.h

#ifndef TestClass_H
#define TestClass_H
 
class TestClass
{
public:
  void MyFunction();
 
};
 
#endif

TestClass.cpp

#include "TestClass.h"
#include <iostream>
 
void TestClass::MyFunction()
{
  std::cout << "MyFunction start." << std::endl;
 
  // ... I want to produce a message box here ...
 
  std::cout << "MyFunction end." << std::endl;
}

thread.h

#ifndef THREAD_H
#define THREAD_H
 
#include <QThread>
 
#include "TestClass.h"
 
class Thread : public QThread
{
Q_OBJECT
public:
  void run();
 
signals:
  void DisplayMessageBoxSignal();
 
public:
  TestClass MyTestClass;
};
 
#endif

thread.cpp

#include "thread.h"
 
#include <QMessageBox>
#include <iostream>
 
void Thread::run()
{
  MyTestClass.MyFunction();
 
  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
 
#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(ThreadClassMessageBox)
 
FIND_PACKAGE(Qt4 REQUIRED)
INCLUDE(${QT_USE_FILE})
 
QT4_WRAP_UI(UISrcs form.ui)
QT4_WRAP_CPP(MOCSrcs form.h thread.h)
 
include_directories(${include_directories} ${CMAKE_CURRENT_SOURCE_DIR} ${CMAKE_CURRENT_BINARY_DIR})
 
ADD_EXECUTABLE(ThreadClassMessageBox ThreadClassMessageBox.cpp form.cpp thread.cpp TestClass.cpp ${MOCSrcs} ${UISrcs})
TARGET_LINK_LIBRARIES(ThreadClassMessageBox ${QT_LIBRARIES})