Results 1 to 6 of 6

Thread: QScrollView Overlay

  1. #1
    Join Date
    Dec 2006
    Posts
    9
    Thanks
    2
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11

    Default QScrollView Overlay

    I'm working on writing yet another geospatial imagery viewing application. Since I have to deal with large images, I've already created a subclass of QWidget specifically for rendering QImage's. I can readily put that into a QScrollArea without any problems.

    I would like to add a compass-rose as an overlay on top of the image displayed within my scroll area. It would have to be rendered in a fixed position (say, in the upper right corner) as the user scrolls around the image. I've tried subclassing QScrollArea, and re-implementing the paintEvent but that paints in the background behind the loaded widget, and so gets obscured by the image.

    I've googled extensively, but haven't been able to find anything very informative, much less any examples from which to bootstrap myself. Any help would be greatly appreciated.

  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 Overlay

    Subclass the scroll area, and reimplement the paintEvent. Inside it first call the base class implementation and then draw the rose. This way you'll be painting over the image and not under it.

  3. #3
    Join Date
    Dec 2006
    Posts
    9
    Thanks
    2
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11

    Default Re: QScrollView Overlay

    my current implementation of paintEvent is this
    (it's not a real rose, I know, it's just a test)

    Qt Code:
    1. void myScrollArea::paintEvent(QPaintEvent* event)
    2. {
    3. QScrollArea::paintEvent(event);
    4. QPainter painter(viewport());
    5.  
    6. painter.setRenderHint(QPainter::Antialiasing, true);
    7. painter.setPen(QPen(Qt::black, 12, Qt::DashDotLine, Qt::RoundCap));
    8. painter.setBrush(QBrush(Qt::green, Qt::SolidPattern));
    9. painter.drawEllipse(80,80,200,240);
    10.  
    11. }
    To copy to clipboard, switch view to plain text mode 

    Instead of using 'viewport()' I also tried 'this' and 'widget()' as an object to paint on. I received the following error for both trials, and neither one painted anything at all.
    QPainter::begin: Widget painting can only begin as a result of a paintEvent
    Painter must be active to set rendering hints.

  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 Overlay

    The painter has to be opened on the viewport. Do you get that error message even if you open the painter on the viewport? That shouldn't happen...

    This code works fine for me:

    Qt Code:
    1. #include <QApplication>
    2. #include <QScrollArea>
    3. #include <QPainter>
    4.  
    5.  
    6. class MyScrollArea : public QScrollArea {
    7. public:
    8. MyScrollArea(QWidget *parent=0) : QScrollArea(parent){}
    9. protected:
    10. void paintEvent(QPaintEvent *e){
    11. QScrollArea::paintEvent(e);
    12. QPainter painter(viewport());
    13. painter.setBrush(Qt::red);
    14. painter.drawEllipse(20, 20, 40, 40);
    15. }
    16. };
    17.  
    18. int main(int argc, char **argv){
    19. QApplication app(argc, argv);
    20. MyScrollArea sa;
    21. sa.show();
    22. return app.exec();
    23. }
    To copy to clipboard, switch view to plain text mode 

  5. #5
    Join Date
    Dec 2006
    Posts
    9
    Thanks
    2
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11

    Default Re: QScrollView Overlay

    Sorry about focusing on the error messages too much. Using viewport() works nicely, without error, but doesn't do what I want. So I experimented with 'widget()' and 'this' as attempts to get different behavior (neither worked).

    So, continuing with viewport() since that's really where I want to paint...

    The code you have will draw an ellipse nicely. However, if you put anything in the ScrollArea, it is rendered atop the custom painter, despite first calling QScrollArea:: paintEvent().

    Qt Code:
    1. int main(int argc, char **argv){
    2. QApplication app(argc, argv);
    3. MyScrollArea sa;
    4.  
    5. QLabel *label = new QLabel;
    6. QImage image("./test.png");
    7. label->setPixmap( QPixmap::fromImage(image) );
    8.  
    9. sa.setWidget(label);
    10. sa.show();
    11. return app.exec();
    12. }
    To copy to clipboard, switch view to plain text mode 

    If you choose an image that doesn't completely cover up the painter, then you can see what I'm talking about. The painter is in the background below the image, whereas I'm looking for things to be the other way around.

  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 Overlay

    The easiest way would be to delegate the painting to the widget (the one with the image) itself. Another possibility is to make the rose a widget as well, just don't put it inside the viewport layout.

Similar Threads

  1. QScrollView size policy?
    By Henrikas[MI] in forum Qt Programming
    Replies: 1
    Last Post: 19th November 2006, 10:52
  2. Pixmap Overlay
    By ToddAtWSU in forum Qt Programming
    Replies: 5
    Last Post: 22nd June 2006, 20:19
  3. faster QScrollView
    By firas in forum Qt Programming
    Replies: 2
    Last Post: 29th April 2006, 18:49
  4. Subclassing QScrollView
    By sumsin in forum Qt Programming
    Replies: 13
    Last Post: 16th March 2006, 14:20
  5. How to find size provided by QScrollView
    By Sarma in forum Qt Programming
    Replies: 1
    Last Post: 7th March 2006, 09:26

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.