Difference between revisions of "Qt/Widgets/Timer"

From ProgrammingExamples
< Qt
Jump to: navigation, search
(Do not subclass QThread.)
(Undo revision 5098 by Daviddoria (Talk) Oops, changed wrong page.)
 
Line 21: Line 21:
 
<source lang="cpp">
 
<source lang="cpp">
 
#include <QtGui>
 
#include <QtGui>
#include <QImage>
+
//#include <qtimer.h>
 +
#include <QTimer>
  
 
#include "form.h"
 
#include "form.h"
#include "MyClass.h"
 
 
 
#include <iostream>
 
#include <iostream>
  
Form::Form(QWidget *parent)
+
MainWindow::MainWindow()
    : QWidget(parent)
+
 
{
 
{
   setupUi(this);
+
   ui.setupUi(this);
 
}
 
}
  
void Form::on_btnOpen_clicked()
+
void MainWindow::on_btnStart_clicked()
 
{
 
{
   QThread* thread = new QThread;
+
   std::cout << "button clicked." << std::endl;
 
+
 
   MyClass* myClass = new MyClass;
+
   //QTimer* timer = new QTimer(this);
  myClass->moveToThread(thread);
+
   connect(&timer, SIGNAL(timeout()), this, SLOT(TimerEvent()));
   connect(thread, SIGNAL(started()), myClass, SLOT(start()));
+
   this->timer.start(1000);
   connect(myClass, SIGNAL(finished()), thread, SLOT(quit()));
+
 
   thread->start();
+
  //QProgressDialog dialog;
   //a->wait();
+
  this->dialog.setMaximum(0);
 +
   this->dialog.setMinimum(0);
 +
   this->dialog.exec();
 
    
 
    
  std::cout << "exit." << std::endl;
 
 
}
 
}
  
 
+
void MainWindow::TimerEvent()
 +
{
 +
  std::cout << "Timer expired." << std::endl;
 +
  this->dialog.cancel();
 +
  this->timer.stop();
 +
}
 
</source>
 
</source>
  
 
==form.h==
 
==form.h==
 
<source lang="cpp">
 
<source lang="cpp">
#ifndef FORM_H
+
#ifndef BUTTONFORM_H
#define FORM_H
+
#define BUTTONFORM_H
  
#include "ui_form.h"
+
#include "ui_main.h"
  
class Form : public QWidget, private Ui::Form
+
//class QTimer;
{
+
#include <QTimer>
Q_OBJECT
+
//class QProgressDialog;
 +
#include <QProgressDialog>
  
public slots:
+
//class MainWindow : public QWidget
 
+
class MainWindow : public QMainWindow
  void on_btnOpen_clicked();
+
{
 +
    Q_OBJECT
  
 
public:
 
public:
    Form(QWidget *parent = 0);
+
  MainWindow();
 +
 
 +
  public slots:
 +
  void on_btnStart_clicked(); 
 +
  void TimerEvent();
  
 +
private:
 +
    Ui::MainWindow ui;
 +
    //QTimer* timer = new QTimer(this);
 +
    QTimer timer;
 +
    QProgressDialog dialog;
 
};
 
};
  

Latest revision as of 11:12, 16 December 2011

Timer.cpp

#include <QProgressDialog>
#include <QApplication>
 
#include "form.h"
 
int main( int argc, char **argv )
{
  QApplication app(argc, argv);
  MainWindow window;
 
  window.show();
 
  return app.exec();
}

form.cpp

#include <QtGui>
//#include <qtimer.h>
#include <QTimer>
 
#include "form.h"
#include <iostream>
 
MainWindow::MainWindow()
{
  ui.setupUi(this);
}
 
void MainWindow::on_btnStart_clicked()
{
  std::cout << "button clicked." << std::endl;
 
  //QTimer* timer = new QTimer(this);
  connect(&timer, SIGNAL(timeout()), this, SLOT(TimerEvent()));
  this->timer.start(1000);
 
  //QProgressDialog dialog;
  this->dialog.setMaximum(0);
  this->dialog.setMinimum(0);
  this->dialog.exec();
 
}
 
void MainWindow::TimerEvent()
{
  std::cout << "Timer expired." << std::endl; 
  this->dialog.cancel();
  this->timer.stop();
}

form.h

#ifndef BUTTONFORM_H
#define BUTTONFORM_H
 
#include "ui_main.h"
 
//class QTimer;
#include <QTimer>
//class QProgressDialog;
#include <QProgressDialog>
 
//class MainWindow : public QWidget
class MainWindow : public QMainWindow
{
    Q_OBJECT
 
public:
  MainWindow();
 
  public slots:
  void on_btnStart_clicked();  
  void TimerEvent();
 
private:
    Ui::MainWindow ui;
    //QTimer* timer = new QTimer(this);
    QTimer timer;
    QProgressDialog dialog;
};
 
#endif

main.ui

<?xml version="1.0" encoding="UTF-8"?>
<ui version="4.0">
 <class>MainWindow</class>
 <widget class="QMainWindow" name="MainWindow">
  <property name="geometry">
   <rect>
    <x>0</x>
    <y>0</y>
    <width>205</width>
    <height>169</height>
   </rect>
  </property>
  <property name="windowTitle">
   <string>MainWindow</string>
  </property>
  <widget class="QWidget" name="centralwidget">
   <widget class="QPushButton" name="btnStart">
    <property name="geometry">
     <rect>
      <x>60</x>
      <y>90</y>
      <width>91</width>
      <height>28</height>
     </rect>
    </property>
    <property name="text">
     <string>Start</string>
    </property>
   </widget>
  </widget>
 </widget>
 <resources/>
 <connections/>
</ui>

CMakeLists.txt

cmake_minimum_required(VERSION 2.6)
 
PROJECT(Timer)
 
FIND_PACKAGE(Qt4 REQUIRED)
INCLUDE(${QT_USE_FILE})
 
QT4_WRAP_UI(UISrcs main.ui)
QT4_WRAP_CPP(MOCSrcs form.h)
 
include_directories(${include_directories} ${CMAKE_CURRENT_SOURCE_DIR} ${CMAKE_CURRENT_BINARY_DIR})
 
ADD_EXECUTABLE(Timer Timer.cpp form.h form.cpp ${UISrcs} ${MOCSrcs})
TARGET_LINK_LIBRARIES(Timer ${QT_LIBRARIES})