Qt Graphical Objects & Multi-Thread
WidgetUpdate.cpp
Go to the documentation of this file.
1 
9 #include "WidgetUpdate.hpp"
10 #include <QPainter>
11 #include <QPaintEvent>
12 #include <QMouseEvent>
13 #include <QMainWindow>
14 #include <QApplication>
15 
16 // The main method of this thread.
17 void Updater::run() { // no exec() call = no event handling
18  //std::cout << "Other thread " << currentThreadId() << std::endl;
19  still_running = true;
20  while (still_running) {
21 #ifdef BUGGY
22  widget.updatePosition(1);
23  // !!!Feature appears when frequency is high (sleep < 10 ms)!!!
24 #else
26 #endif
27  msleep(5); // wait (parameter in milliseconds)
28  } // end of while (this thread should run)
29 } // end of void Updater::run() --------------------------------------
30 
31 // Draws the position pointed by the mouse.
32 // Parameter event The drawing event, giving the region
33 // which should be redrawn.
34 void UpdateWidget::paintEvent(QPaintEvent *event) {
35  static const int nb_points = 2;
36  QPainter painter(this);
37  const QRect& drawing_rect = event->rect();
38  const QPoint *points[nb_points] = {&aimed, &position};
39  const Qt::GlobalColor colors[nb_points] = {Qt::green, Qt::blue};
40 
41  painter.eraseRect(drawing_rect); // clean the region
42  for(int i = 0; i < nb_points; i++) { // for each point
43  painter.setPen(colors[i]); // set the border color
44  painter.setBrush(colors[i]); // set the fill color
45  painter.drawPie(region(*points[i]), 0, 360 * 16); // fill the disk
46  } // end of for (each point)
47 } // end of void UpdateWidget::paintEvent(QPaintEvent*) --------------
48 
49 // Moves the drawn position where the mouse is.
50 // Parameter event The mouse event, giving its position.
51 void UpdateWidget::mouseMoveEvent(QMouseEvent *event) {
52  if (event->buttons() & Qt::LeftButton) // if left button is pressed
53  modify( aimed, event->pos() ); // change position and drawing
54 } // end of void UpdateWidget::mouseMoveEvent(QMouseEvent*) ----------
55 
61 int main(int argc, char** argv) {
62  QApplication app(argc, argv); // start the Qt main loop
63  QMainWindow mainWindow;
64  UpdateWidget centralWidget(&mainWindow);
65  centralWidget.setMinimumSize(300,200);
66  centralWidget.init();
67  mainWindow.setCentralWidget(&centralWidget);
68  mainWindow.show();
69 
70  // stop the Qt main loop when last window (= main window) is closed
71  app.connect(&app, SIGNAL(lastWindowClosed()), &app, SLOT(quit()));
72  return app.exec(); // return Qt main loop's return value
73 } // end of int main(int, char**) ------------------------------------
bool still_running
Should the thread continue to run?
This widget draws two positions, one moved by drag and drop, the other moved by a separate thread tow...
void mouseMoveEvent(QMouseEvent *event)
Moves the drawn position where the mouse is, if left button is pressed.
This header file and its related source demonstrate that Qt graphical objects are not thread-safe! ...
void init()
Initialises both points.
void paintEvent(QPaintEvent *event)
Draws the position pointed by the mouse.
void updatePosRequest(const int dist)
Sends a signal to ask for an update of the widget.
void run()
The main method of this thread.
int main(int argc, char **argv)
This is the main function of this bug testing application.