Results 1 to 17 of 17

Thread: QScrollView and repaint

  1. #1
    Join Date
    Jan 2006
    Posts
    667
    Thanks
    10
    Thanked 80 Times in 74 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default QScrollView and repaint

    Hi,

    I have reimplemented drawContents of QScrollView. I am drawing lots of stuff in this. When a window from some other application is dragged over to this and moved around, the drawing gets erased.

    How can I prevent this ?

    Thanks a lot

  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: QScrollView and repaint

    Maybe you reimplemented drawContents incorrectly? QScrollView should trigger a paint event (and eventually drawContents) after part of the widget gets obscured. Could you give more details, what exactly did you do?

  3. #3
    Join Date
    Jan 2006
    Posts
    667
    Thanks
    10
    Thanked 80 Times in 74 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QScrollView and repaint

    Here is my code :

    Qt Code:
    1. void Paper::drawContents(QPainter *p, int cx, int cy, int cw, int ch)
    2. {
    3. QPixmap pm(cw, ch);
    4. QPainter *pmp = new QPainter( &pm );
    5. pmp->setBrush( white );
    6. pmp->setPen( NoPen );
    7. pmp->drawRect( 0, 0, cw, ch);
    8. pmp->setPen( black );
    9. pmp->translate( -cx, -cy );
    10.  
    11. //My drawing code. Everything is done useing pmp
    12.  
    13. pmp->end();
    14. p->drawPixmap( cx, cy, pm );
    15. p->end();
    16. delete pmp;
    17. }
    To copy to clipboard, switch view to plain text mode 

    Thanks a lot

  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: QScrollView and repaint

    I think the translation might be messing the things up (I think the code is incorrect). Try getting rid of the pixmap at all and draw the whole viewport directly. If it helps, you can then optimise some things to get better results.

  5. #5
    Join Date
    Jan 2006
    Posts
    667
    Thanks
    10
    Thanked 80 Times in 74 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QScrollView and repaint

    Here is the new code :

    Qt Code:
    1. void Paper::drawContents(QPainter *pmp, int cx, int cy, int cw, int ch)
    2. {
    3. //QPixmap pm(cw, ch);
    4. //QPainter *pmp = new QPainter( &pm );
    5. pmp->setBrush( white );
    6. pmp->setPen( NoPen );
    7. pmp->drawRect( 0, 0, cw, ch);
    8. //pmp->setPen( black );
    9. //pmp->translate( -cx, -cy );
    10.  
    11. //My drawing code
    12. }
    To copy to clipboard, switch view to plain text mode 

    It did not solve the first problem. Also, now flickering is introduced.
    Does this has to do something with contentsHeight and contentsWidth ?

    Thanks a lot for your time

  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: QScrollView and repaint

    How about:

    Qt Code:
    1. void Paper::drawContents(QPainter *pmp, int cx, int cy, int cw, int ch)
    2. {
    3. pmp->setClipRect(cx,cy,cw,ch);
    4. pmp->setClipping(true);
    5. pmp->fill(Qt::white);
    6. //Your drawing code
    7. }
    To copy to clipboard, switch view to plain text mode 

  7. #7
    Join Date
    Jan 2006
    Posts
    667
    Thanks
    10
    Thanked 80 Times in 74 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QScrollView and repaint

    It does not work.

    Also, if I do not translate, and when scrollbars appear, all the drawing is messed up.

    Please Help
    Thanks a lot

  8. #8
    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: QScrollView and repaint

    It should work. Did you reimplement some other methods too? Maybe the viewport isn't set up correctly?

  9. #9
    Join Date
    Jan 2006
    Posts
    667
    Thanks
    10
    Thanked 80 Times in 74 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QScrollView and repaint

    Here is the list of all the methods that I have reimplemented.

    Qt Code:
    1. virtual void drawContents(QPainter *p, int cx, int cy, int cw, int ch);
    2. virtual void keyPressEvent(QKeyEvent *);
    3. virtual void contentsMousePressEvent(QMouseEvent *);
    4. virtual void contentsMouseDoubleClickEvent(QMouseEvent *);
    5. virtual void contentsMouseReleaseEvent(QMouseEvent *);
    6. virtual void contentsMouseMoveEvent(QMouseEvent *);
    7. virtual void focusInEvent(QFocusEvent *);
    8. virtual void resizeEvent(QResizeEvent *);
    To copy to clipboard, switch view to plain text mode 

    Also, can you please explain why you think your code should work ?

    Thanks a lot.

  10. #10
    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: QScrollView and repaint

    Because drawContents should work on the viewport, meaning that you shouldn't need to translate the painter if the viewport was set up correctly -- you draw directly on the viewport. How did you initialise the viewport?

  11. #11
    Join Date
    Jan 2006
    Posts
    667
    Thanks
    10
    Thanked 80 Times in 74 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QScrollView and repaint

    In the constructor I have :

    Qt Code:
    1. viewport()->setBackgroundMode( Qt::NoBackground ); viewport()->setFocusPolicy(QWidget::StrongFocus);
    To copy to clipboard, switch view to plain text mode 

    Apart from the above two lines, I dont do much with the viewport. I only use resizeContents according to the size of my drawing and then call repaint().

    Can you please explain this a little more? All this while I thought I was doing the write thing.

    Thanks a lot

  12. #12
    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: QScrollView and repaint

    This seems to work (more or less):

    Qt Code:
    1. #include <qapplication.h>
    2. #include <qscrollview.h>
    3. #include <qpainter.h>
    4.  
    5. class Test : public QScrollView{
    6. public:
    7. Test() : QScrollView(){ resizeContents(600, 300); }
    8. ~Test(){}
    9. protected:
    10. void drawContents(QPainter *p, int cx, int cy, int cw, int ch){
    11. // p->setClipRect(cx,cy,cw,ch);
    12. // p->setClipping(true);
    13. p->fillRect(0,0,600,300, Qt::white);
    14. p->setPen(Qt::red);
    15. p->drawLine(0,0, 600, 300);
    16. }
    17. };
    18.  
    19. int main(int argc, char **argv){
    20. QApplication app(argc, argv);
    21. Test t;
    22. t.resize(200, 100);
    23. t.show();
    24. app.setMainWidget(&t);
    25. return app.exec();
    26. }
    To copy to clipboard, switch view to plain text mode 

    When clipping is enabled, the widget is not refreshed correctly though...

  13. #13
    Join Date
    Jan 2006
    Posts
    667
    Thanks
    10
    Thanked 80 Times in 74 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QScrollView and repaint

    Can you please do me a favor ?

    Can you try it with the following code ? It should draw red horizontal lines, right ?

    [CODE]

    void drawContents(QPainter *p, int cx, int cy, int cw, int ch)
    {
    //p->setClipRect(cx,cy,cw,ch);
    // p->setClipping(true);
    p->fillRect(0,0,600,300, Qt::white);
    p->setPen(Qt::red);
    for(int i = 0; i < 300; i+=25){
    p->drawLine(0,i, 600, i);
    }
    }

    [CODE]

    Once you have the drawing, resize your browser( or any other window) and move it around this drawing.

    Are the lines getting drawn as the new area is shown and the window is moved ?

    Thanks a lot.

  14. #14
    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: QScrollView and repaint

    No, they don't.

  15. #15
    Join Date
    Jan 2006
    Posts
    667
    Thanks
    10
    Thanked 80 Times in 74 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QScrollView and repaint

    Shouldn't the lines be drawn as I move the window ? I mean, shouldn't the drawContents be called and the drawing should be updated?

    Now, since we know that drawContents is not called, is there a way be which I can detect this and call repaint()

    Thanks a lot for your time.
    Thanks a lot

  16. #16
    Join Date
    Jan 2006
    Posts
    667
    Thanks
    10
    Thanked 80 Times in 74 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QScrollView and repaint

    Any ideas on this one ?

    Thanks.

  17. #17
    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: QScrollView and repaint

    A simple way around this is to add a child widget to the scrollview and do all the drawing in the paint event of the widget.

    Qt Code:
    1. class MyWidget : public QWidget{
    2. public:
    3. MyWidget(QWidget *parent = 0) : QWidget(parent){}
    4. protected:
    5. void paintEvent(QPaintEvent *pe){
    6. //...
    7. }
    8. };
    9. //...
    10. wid = new MyWidget(viewport());
    11. wid.resize(600, 400);
    12. //...
    To copy to clipboard, switch view to plain text mode 

Similar Threads

  1. repaint help pls
    By munna in forum Qt Programming
    Replies: 3
    Last Post: 21st June 2006, 10:52

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.