Difference between revisions of "Qt/Widgets/Thread"

From ProgrammingExamples
< Qt
Jump to: navigation, search
(Thread.cxx)
 
(6 intermediate revisions by 4 users not shown)
Line 1: Line 1:
instructions on rival crockpot http://members.multimania.co.uk/emfunbillre/grayhound-bus.com-720994.html grayhound  www.myspce http://members.multimania.co.uk/emfunbillre/rival-mfg-co-933750.html rival mfg co  rival mfg co http://members.multimania.co.uk/emfunbillre/ comcast  18qt http://members.multimania.co.uk/emfunbillre/walgreens-.com-948437.html administrator@walgreens.com  ww.comcast.com http://members.multimania.co.uk/emfunbillre/ww.comcast.com.welcome-746511.html welcome to comcast.com  vodafone.co.nz/welcome http://members.multimania.co.uk/emfunbillre/myspce.copm-719985.html myspce.copm  hotmail france http://members.multimania.co.uk/emfunbillre/357-magnum-revolvers-750593.html 357 revolvers  grayhound com http://members.multimania.co.uk/emfunbillre/18qtcom-757280.html 18qtcom  www harborfreight com http://members.multimania.co.uk/emfunbillre/18qt.com-video-783609.html 18qt video  grayhound bus http://members.multimania.co.uk/emfunbillre/harborfreight.com-716114.html harborfreight
+
==form.h==
 +
<source lang="cpp">
 +
#ifndef FORM_H
 +
#define FORM_H
 +
 
 +
#include "ui_form.h"
 +
 
 +
class Form : public QWidget, private Ui::Form
 +
{
 +
Q_OBJECT
 +
 
 +
public slots:
 +
 
 +
  void on_btnOpen_clicked();
 +
 
 +
public:
 +
    Form(QWidget *parent = 0);
 +
 
 +
};
 +
 
 +
#endif
 +
 
 +
</source>
 +
 
 +
==form.cpp==
 +
<source lang="cpp">
 +
#include <QtGui>
 +
#include <QImage>
 +
 
 +
#include "form.h"
 +
#include "MyClass.h"
 +
 
 +
#include <iostream>
 +
 
 +
Form::Form(QWidget *parent)
 +
    : QWidget(parent)
 +
{
 +
  setupUi(this);
 +
}
 +
 
 +
void Form::on_btnOpen_clicked()
 +
{
 +
  QThread* thread = new QThread;
 +
 
 +
  MyClass* myClass = new MyClass;
 +
  myClass->moveToThread(thread);
 +
  connect(thread, SIGNAL(started()), myClass, SLOT(start()));
 +
  connect(myClass, SIGNAL(finished()), thread, SLOT(quit()));
 +
  thread->start();
 +
  //a->wait();
 +
 
 +
  std::cout << "exit." << std::endl;
 +
}
 +
 
 +
</source>
 +
 
 +
==Thread.cxx==
 +
<source lang="cpp">
 +
#include <QApplication>
 +
#include <QObject>
 +
#include <QThread>
 +
 
 +
#include <iostream>
 +
 
 +
#include "form.h"
 +
 
 +
int main(int argc, char*argv[])
 +
{
 +
  QApplication app(argc, argv);
 +
 
 +
  Form form;
 +
 
 +
  form.show();
 +
 
 +
  return app.exec();
 +
}
 +
</source>
  
 
==CMakeLists.txt==
 
==CMakeLists.txt==

Latest revision as of 11:13, 16 December 2011

form.h

#ifndef FORM_H
#define FORM_H
 
#include "ui_form.h"
 
class Form : public QWidget, private Ui::Form
{
Q_OBJECT
 
public slots:
 
  void on_btnOpen_clicked();
 
public:
    Form(QWidget *parent = 0);
 
};
 
#endif

form.cpp

#include <QtGui>
#include <QImage>
 
#include "form.h"
#include "MyClass.h"
 
#include <iostream>
 
Form::Form(QWidget *parent)
    : QWidget(parent)
{
  setupUi(this);
}
 
void Form::on_btnOpen_clicked()
{
  QThread* thread = new QThread;
 
  MyClass* myClass = new MyClass;
  myClass->moveToThread(thread);
  connect(thread, SIGNAL(started()), myClass, SLOT(start()));
  connect(myClass, SIGNAL(finished()), thread, SLOT(quit()));
  thread->start();
  //a->wait();
 
  std::cout << "exit." << std::endl;
}

Thread.cxx

#include <QApplication>
#include <QObject>
#include <QThread>
 
#include <iostream>
 
#include "form.h"
 
int main(int argc, char*argv[])
{
  QApplication app(argc, argv);
 
  Form form;
 
  form.show();
 
  return app.exec();
}

CMakeLists.txt

cmake_minimum_required(VERSION 2.6)
 
PROJECT(Thread)
 
FIND_PACKAGE(Qt4 REQUIRED)
INCLUDE(${QT_USE_FILE})
 
include_directories(${include_directories} ${CMAKE_CURRENT_SOURCE_DIR} ${CMAKE_CURRENT_BINARY_DIR})
 
ADD_EXECUTABLE(Thread Thread.cpp)# ${MOCSrcs} ${UISrcs})
TARGET_LINK_LIBRARIES(Thread ${QT_LIBRARIES})