Results 1 to 7 of 7

Thread: QPainter and how to access the parent variables.

  1. #1
    Join Date
    Aug 2006
    Location
    The Netherlands
    Posts
    64
    Thanks
    6
    Thanked 2 Times in 2 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default QPainter and how to access the parent variables.

    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

    Qt Code:
    1. #ifndef UI_PAINWIDGET_H
    2. #define UI_PAINTERWIDGET_H
    3.  
    4.  
    5. #include <QWidget>
    6. #include <QtGui>
    7.  
    8.  
    9. class PainterWidget : public QWidget
    10. {
    11. Q_OBJECT
    12.  
    13. public:
    14.  
    15. PainterWidget(QWidget *parent = 0);
    16.  
    17. protected:
    18.  
    19. void paintEvent(QPaintEvent *event);
    20. };
    21.  
    22. #endif
    To copy to clipboard, switch view to plain text mode 

    painterwidget.cpp

    Qt Code:
    1. #include "painterwidget.h"
    2.  
    3.  
    4.  
    5. PainterWidget::PainterWidget(QWidget *parent) : QWidget(parent)
    6. {
    7. setFixedSize(1200, 800);
    8.  
    9. setAttribute(Qt::WA_OpaquePaintEvent);
    10. }
    11.  
    12.  
    13.  
    14. void PainterWidget::paintEvent(QPaintEvent *)
    15. {
    16. QPainter painter(this);
    17.  
    18. painter.setPen(Qt::red);
    19. painter.drawLine etc. etc.
    20. here comes all the stuf to paint the curves
    21. }
    To copy to clipboard, switch view to plain text mode 

  2. #2
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: QPainter and how to access the parent variables.

    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).

  3. #3
    Join Date
    Aug 2006
    Location
    The Netherlands
    Posts
    64
    Thanks
    6
    Thanked 2 Times in 2 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QPainter and how to access the parent variables.

    Quote Originally Posted by wysota View Post
    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.

  4. #4
    Join Date
    Aug 2006
    Location
    The Netherlands
    Posts
    64
    Thanks
    6
    Thanked 2 Times in 2 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QPainter and how to access the parent variables.

    "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++...

  5. #5
    Join Date
    Feb 2006
    Location
    Oslo, Norway
    Posts
    6,264
    Thanks
    36
    Thanked 1,519 Times in 1,389 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows Symbian S60 Maemo/MeeGo

    Default Re: QPainter and how to access the parent variables.

    Quote Originally Posted by Teuniz View Post
    I guess I got lost in C++...
    Use forward declaration in headers. Further explanation: Circular dependency
    J-P Nurmi

  6. #6
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: QPainter and how to access the parent variables.

    Quote Originally Posted by Teuniz View Post
    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".

  7. #7
    Join Date
    Aug 2006
    Location
    The Netherlands
    Posts
    64
    Thanks
    6
    Thanked 2 Times in 2 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QPainter and how to access the parent variables.

    I read the article on Wikipedia and now it works
    I'm using a refreshrate of 16 fps as well

    Tnx wysota and jpn!

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.