Results 1 to 14 of 14

Thread: QAbstractScrollArea and paintEvent

  1. #1
    Join Date
    Nov 2007
    Posts
    291
    Thanks
    85
    Thanked 1 Time in 1 Post

    Default QAbstractScrollArea and paintEvent

    hi
    In the below code the pixmap is moved(mouse click and move) only when the window is resized ,where is the problem.ie.the postion of pixmap is updated only when the window is resized . If i inherit from the QWidget there is no problem.

    Qt Code:
    1. #include <QtGui>
    2. class Widget : public QAbstractScrollArea
    3. {
    4. private:
    5. QPixmap pixmap;
    6. QPoint mouse_press;
    7. QPoint offset;
    8.  
    9. public:
    10. Widget()
    11. {
    12. offset.setX(0);
    13. offset.setY(0);
    14.  
    15. pixmap = QPixmap(100, 100);
    16. pixmap.fill(Qt::transparent);
    17. QPainter painter(&pixmap);
    18. painter.drawRect(0, 0, 50, 50);
    19. }
    20.  
    21. protected:
    22. void paintEvent(QPaintEvent *event)
    23. {
    24. QAbstractScrollArea::paintEvent(event);
    25. if (pixmap.isNull())
    26. return;
    27. QPainter painter(viewport());
    28. painter.drawPixmap(offset, pixmap);
    29. }
    30.  
    31. void mousePressEvent(QMouseEvent *event)
    32. {
    33. mouse_press=event->pos();
    34. event->ignore();
    35. }
    36.  
    37. void mouseMoveEvent(QMouseEvent * event)
    38. {
    39. offset=event->pos()-mouse_press+offset;
    40. mouse_press=event->pos();
    41. update();
    42. }
    43. };
    44.  
    45. int main(int argc, char *argv[])
    46. {
    47. QApplication a(argc, argv);
    48. Widget w;
    49. w.show();
    50. return a.exec();
    51. }
    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: QAbstractScrollArea and paintEvent

    Do you do anything with the viewport of the scrollarea?

  3. #3
    Join Date
    Nov 2007
    Posts
    291
    Thanks
    85
    Thanked 1 Time in 1 Post

    Default Re: QAbstractScrollArea and paintEvent

    no ,if i substitue viewport() with this then i get "QPainter::begin: Widget painting can only begin as a result of a paintEvent" this message.

  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: QAbstractScrollArea and paintEvent

    But you had to setup the viewport somehow in the first place... otherwise scrollbars wouldn't work. Do they?

  5. #5
    Join Date
    Nov 2007
    Posts
    291
    Thanks
    85
    Thanked 1 Time in 1 Post

    Default Re: QAbstractScrollArea and paintEvent

    i have not tested it yet,but now i solved the problem.Instead of calling update(), calling viewport()->update() works fine.

  6. #6
    Join Date
    Nov 2007
    Posts
    291
    Thanks
    85
    Thanked 1 Time in 1 Post

    Default Re: QAbstractScrollArea and paintEvent

    But you had to setup the viewport somehow in the first place... otherwise scrollbars wouldn't work. Do they?
    yes thats true .i only want to show a pixmap on the viewport ,how to get the scrollbars .if i need the scrollbars should i have to paint on the viewport widget.

  7. #7
    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: QAbstractScrollArea and paintEvent

    If you don't want the scrollbars, why do you use QAbstractScrollArea instead of QWidget?

  8. #8
    Join Date
    Nov 2007
    Posts
    291
    Thanks
    85
    Thanked 1 Time in 1 Post

    Default Re: QAbstractScrollArea and paintEvent

    of course i want scrollbars,i am making a image viewer with scrollbars, if i need the scrollbars should i have to paint on the viewport widget.

  9. #9
    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: QAbstractScrollArea and paintEvent

    So why not put a QLabel inside QScrollArea and forget about the whole problem?

    As for the current situation scrollbars will only appear if you declare the viewport size larger than the scrollarea size. Currently you didn't declare anything so the viewport size is practically undetermined. Unless you implement scrollbar handling yourself (setting the sizes and handling moving them) you won't be able to use them.

  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: QAbstractScrollArea and paintEvent

    So why not put a QLabel inside QScrollArea and forget about the whole problem?

    As for the current situation scrollbars will only appear if you declare the viewport size larger than the scrollarea size. Currently you didn't declare anything so the viewport size is practically undetermined. Unless you implement scrollbar handling yourself (setting the sizes and handling moving them) you won't be able to use them.

  11. #11
    Join Date
    Nov 2007
    Posts
    291
    Thanks
    85
    Thanked 1 Time in 1 Post

    Default Re: QAbstractScrollArea and paintEvent

    So why not put a QLabel inside QScrollArea and forget about the whole problem?
    if i need to have zoom and image moving features(through wheel event and mouse move event) ,should i have to reimplement the QLabel.

  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: QAbstractScrollArea and paintEvent

    No, you just set the zoomed pixmap to the label. Panning should work out of the box.

  13. #13
    Join Date
    Nov 2007
    Posts
    291
    Thanks
    85
    Thanked 1 Time in 1 Post

    Default Re: QAbstractScrollArea and paintEvent

    So why not put a QLabel inside QScrollArea and forget about the whole problem?
    i am following the same .I need rubberband zooming .Is it possible with QScrollArea.

  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: QAbstractScrollArea and paintEvent

    Yes, of course. Use QRubberBand and calculate the zoom rectangle based on the position of the viewport.

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.