PDA

View Full Version : QPainter and how to access the parent variables.



Teuniz
7th March 2007, 11:09
I made a program with Qt3, it collects a continues datastream from the comport and
paints several curves on the screen (almost realtime like an osciloscope). I accomplished
this by painting directly on the widget without double buffering. Disadvantage was that
the screen wasn't refreshed when another window was placed on the screen. The big
advantage was that the programn was fast and didn't use much memory, this program
was able to run on older computers.

I rewrote this program for Qt4 and then the problems started, in Qt4 it's not possible
anymore to paint on a widget outside a paintevent (except for Linux/X11).
So I used another approach, I created a QPixmap (1200x800), do all the painting
on the pixmap and then add the pixmap to a QLabel 50 times a second. This approach
works only on fast computers (P4 2.6GHz), on older computers (P2 450MHz) this program
was unusable.

So, now I'm trying to rewrite the program again using the QPaintEvent.
The problem here is that I don't want to update the whole mainwindow but only the
painting area (1200x800) 50 times a second. That's why I created a widget that
inherits QWidget. I use this widget in my QMainwindow. Now I'm able to paint
during using the QPaintEvent but there is another problem. To paint usefull curves,
I need the data of almost all the variables and widgets from the parent (the
QMainwindow) but in the paintingwidget I can't access them. How can I accomplish this?



painterwidget.h



#ifndef UI_PAINWIDGET_H
#define UI_PAINTERWIDGET_H


#include <QWidget>
#include <QtGui>


class PainterWidget : public QWidget
{
Q_OBJECT

public:

PainterWidget(QWidget *parent = 0);

protected:

void paintEvent(QPaintEvent *event);
};

#endif


painterwidget.cpp



#include "painterwidget.h"



PainterWidget::PainterWidget(QWidget *parent) : QWidget(parent)
{
setFixedSize(1200, 800);

setAttribute(Qt::WA_OpaquePaintEvent);
}



void PainterWidget::paintEvent(QPaintEvent *)
{
QPainter painter(this);

painter.setPen(Qt::red);
painter.drawLine etc. etc.
here comes all the stuf to paint the curves
}

wysota
7th March 2007, 11:27
Why update 50 times per second? Why not update only when something actually changes?

You can access the parent though parent() and parentWidget(). Remember to cast the result to the proper class pointer (your QMainWindow descendant).

Teuniz
7th March 2007, 11:42
Why update 50 times per second? Why not update only when something actually changes?

There is a continu datastream arriving at the comport which need to be visualized, actually
the data is coming from a 4 channel analog/digital-converter which generates 100 samples
per second. So with 1200 pixels on the X-axe one trace takes 12 seconds (I paint one pixel
per sample).
Tnx for the tip, I'm going to try that.

Regards.

Teuniz
7th March 2007, 12:23
"mainwindow.h:140: error: ISO C++ forbids declaration of ‘PainterWidget’ with no type"

I can't include mainwindow.h in painterwidget.h because in mainwindow.h I have already
included painterwidget.h. If I don't include painterwidget.h in mainwindow.h I can not create
a class PainterWiget. If I can't include mainwindow.h in painter.h, how do I cast the parent
widget to the class used in mainwindow.cpp?

I guess I got lost in C++... :(

jpn
7th March 2007, 12:35
I guess I got lost in C++... :(
Use forward declaration in headers. Further explanation: Circular dependency (http://en.wikipedia.org/wiki/Circular_dependency)

wysota
7th March 2007, 12:43
There is a continu datastream arriving at the comport which need to be visualized, actually
the data is coming from a 4 channel analog/digital-converter which generates 100 samples
per second.
But the human eye treats ~16fps as a smooth movement, so there is no point in refreshing the graph too often. I don't think a delay of several miliseconds will make you a difference in considering the data "real time".

Teuniz
7th March 2007, 14:06
I read the article on Wikipedia and now it works :)
I'm using a refreshrate of 16 fps as well ;)

Tnx wysota and jpn!