Results 1 to 6 of 6

Thread: How to use QGLWidget as the viewport for QGraphicsView?

  1. #1
    Join Date
    Sep 2010
    Posts
    654
    Thanks
    56
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Windows

    Default How to use QGLWidget as the viewport for QGraphicsView?

    I'm trying to use a QGLWIDGET to test if my app. runs faster.
    I see how the svgviewer example is faster when using it.
    I have used a similar code :
    Qt Code:
    1. glwidget = new QGLWidget(QGLFormat(QGL::SampleBuffers));
    2. G_view->setViewport(glwidget);
    To copy to clipboard, switch view to plain text mode 

    This is my paintEvent code for a QGraphicsView based widget, where I apply the transform and finally call to the internal paintevent

    Qt Code:
    1. void A_Gview2D::paintEvent(QPaintEvent *event) {
    2. QPainter painter (this->viewport());
    3. .....
    4. this->QGraphicsView::paintEvent(event);
    5. }
    To copy to clipboard, switch view to plain text mode 
    I see a 'white' empty space and I have the errors:
    A paint device can only be painted by one painter at a time.
    QPainter::setRenderHint: Painter must be active to set rendering hints
    QPainter::setWorldTransform: Painter not active
    etc.

    It seems that it is not so easy to apply. Any help ?
    Thanks.

  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: How to use QGLWidget as the viewport for QGraphicsView?

    What is it exactly that you are trying to do? Why are you reimplementing paintEvent() here?
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  3. #3
    Join Date
    Sep 2010
    Posts
    654
    Thanks
    56
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: How to use QGLWidget as the viewport for QGraphicsView?

    Thanks wy.
    Well , I have reimp. the paintEvent to have control over the paint events.
    (I have a different way to paint things depend on some vars. Sometimes I use a pre-saved pixmap.)
    Thanks.

  4. #4
    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: How to use QGLWidget as the viewport for QGraphicsView?

    What kind of control? The code you posted shouldn't be giving you the effects you have, at least not for one of recent versions of Qt. Are you sure this part is to blame?
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  5. #5
    Join Date
    Sep 2010
    Posts
    654
    Thanks
    56
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: How to use QGLWidget as the viewport for QGraphicsView?

    Excuse me , I dont understand the 'Are you sure... '

    The errors are emitted when I try to create a painter.
    When the program reaches and try to execute the next line is when the errors are showed.
    Qt Code:
    1. QPainter painter (this->viewport());
    To copy to clipboard, switch view to plain text mode 
    QPainter::begin: A paint device can only be painted by one painter at a time.
    QPainter::setRenderHint: Painter must be active to set rendering hints
    QPainter::setRenderHint: Painter must be active to set rendering hints
    QPainter::setWorldTransform: Painter not active
    QPainter::worldTransform: Painter not active


    Maybe I cant get the painter with this 'easy' code...
    Is there another and correct way to have the painter in case of use a QGLwidget ?
    Thanks.

  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: How to use QGLWidget as the viewport for QGraphicsView?

    The error manifests itself with the painter constructor call. But it doesn't mean the fault is actually there, it could be elsewhere. That's my point when asking whether you are sure the error is really caused by this statement.

    Qt Code:
    1. #include <QtGui>
    2. #include <QGLWidget>
    3.  
    4. class View : public QGraphicsView {
    5. public:
    6. View() {
    7. setViewport(new QGLWidget);
    8. }
    9. protected:
    10. void paintEvent(QPaintEvent *pe) {
    11. // QPainter p(viewport());
    12. QGraphicsView::paintEvent(pe);
    13. }
    14.  
    15. };
    16.  
    17. int main(int argc, char **argv) {
    18. QApplication app(argc, argv);
    19. View v;
    20. QGraphicsScene scene(QRect(0,0,400,300));
    21. v.setScene(&scene);
    22. scene.addRect(QRect(200,200, 100, 50));
    23. v.show();
    24. return app.exec();
    25. }
    To copy to clipboard, switch view to plain text mode 
    Uncommenting painter construction indeed gives the error you describe.

    However if you first call the base class implementation and then construct a painter, it works.

    Qt Code:
    1. void paintEvent(QPaintEvent *pe) {
    2. QGraphicsView::paintEvent(pe);
    3. QPainter p(viewport());
    4. }
    To copy to clipboard, switch view to plain text mode 
    If you do that, be sure to have a look at the "overpainting" Qt example otherwise you won't see much on your view apart a white background.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


Similar Threads

  1. QGraphicsView inside of QAbstractItemView viewport
    By Paladin12 in forum Qt Programming
    Replies: 5
    Last Post: 24th January 2016, 14:15
  2. QGLWidget render or QGraphicsView with GL viewport render
    By QTInfinity in forum Qt Programming
    Replies: 2
    Last Post: 28th November 2011, 11:34
  3. Replies: 0
    Last Post: 8th April 2010, 16:06
  4. Replies: 0
    Last Post: 29th September 2009, 02:28
  5. Replies: 0
    Last Post: 17th August 2008, 17:31

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.