Results 1 to 5 of 5

Thread: How to draw in a QScrollArea?

  1. #1
    Join Date
    Oct 2008
    Location
    Aachen
    Posts
    20
    Thanks
    5
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Windows

    Question How to draw in a QScrollArea?

    Hi,

    I have a classed that inherits QWidget. Inside of it, I have a QScrollArea with some QLabel items wrapped in a QGridLayout. Since I want to connect my labels through lines, I reimplemented the method paintEvent(QPaintEvent *). I unfortunately have the problem that the drawing occurs outside the scroll area as you can see in the attached image. Any hints?

    Thanks.
    Attached Images Attached Images

  2. #2
    Join Date
    Jan 2006
    Location
    Munich, Germany
    Posts
    4,714
    Thanks
    21
    Thanked 418 Times in 411 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: How to draw in a QScrollArea?

    Looks like you are painting in the widget containing the scroll-area and not in the scroll area widget.
    Why don't you put your labels in to a widget, do all the special stuff on that widget, and put that widget as content widget for the scroll area?
    ==========================signature=============== ==================
    S.O.L.I.D principles (use them!):
    https://en.wikipedia.org/wiki/SOLID_...iented_design)

    Do you write clean code? - if you are TDD'ing then maybe, if not, your not writing clean code.

  3. #3
    Join Date
    Oct 2008
    Location
    Aachen
    Posts
    20
    Thanks
    5
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Windows

    Default Re: How to draw in a QScrollArea?

    You're completely right. I just can't figure out why? I subclassed QWidget and setup the UI like the following:
    Qt Code:
    1. void PaintEventInScrollArea::setupUi()
    2. {
    3. QGridLayout *gridLayout = new QGridLayout;
    4.  
    5. for (int i = 0; i < inputCount; ++i) {
    6. inputLabel = new QLabel(QString("Input %1").arg(i + 1));
    7. inputLabel->setMargin(60);
    8. gridLayout->addWidget(inputLabel, i + 1, 0);
    9. }
    10.  
    11. for (int i = 0; i < outputCount; ++i) {
    12. outputLabel = new QLabel(QString("Output %1").arg(i + 1));
    13. outputLabel->setMargin(60);
    14. gridLayout->addWidget(outputLabel, 0, i + 1);
    15. }
    16.  
    17. for (int i = 0; i < inputCount; ++i) {
    18. for (int j = 0; j < outputCount; ++j) {
    19. connectionLabel = new QLabel(QString("Connect %1").arg(j + 1));
    20. connectionLabel->setFixedHeight(20);
    21. connectionLabel->setMargin(60);
    22. connectionLabel->setFrameStyle(QFrame::Box);
    23. connectionLabel->setAlignment(Qt::AlignCenter);
    24. gridLayout->addWidget(connectionLabel, i + 1, j + 1);
    25. }
    26. }
    27.  
    28. QWidget *viewport = new QWidget;
    29. viewport->setLayout(gridLayout);
    30.  
    31. QScrollArea *scrollArea = new QScrollArea;
    32. //scrollArea->setFrameShape(QFrame::NoFrame);
    33. scrollArea->setWidget(viewport);
    34.  
    35. QBoxLayout *mainLayout = new QBoxLayout(QBoxLayout::LeftToRight);
    36. mainLayout->addWidget(scrollArea);
    37. this->setLayout(mainLayout);
    38. }
    To copy to clipboard, switch view to plain text mode 
    and reimplement paintEvent. A simple working example looks like
    Qt Code:
    1. void PaintEventInScrollArea::paintEvent(QPaintEvent *)
    2. {
    3. QPainter painter(this);
    4. painter.setRenderHint(QPainter::Antialiasing);
    5. painter.setPen(Qt::darkRed);
    6. QRectF rectangle(10.0, 20.0, 80.0, 60.0);
    7. painter.drawRect(rectangle);
    8. }
    To copy to clipboard, switch view to plain text mode 

    If my painter take as parent the widget inside the scroll area, I get the error
    Qt Code:
    1. paint device returned engine == 0
    To copy to clipboard, switch view to plain text mode 
    I think I will subclass QAbstractScrollArea and insert my widget inside it.

  4. #4
    Join Date
    Jan 2006
    Location
    Munich, Germany
    Posts
    4,714
    Thanks
    21
    Thanked 418 Times in 411 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: How to draw in a QScrollArea?

    You're completely right. I just can't figure out why?
    Why what? your are painting in the upper parent widget!
    Your upper parent widget IS PaintEventInScrollArea.
    You put in to it a QSrollArea and a QWidget.
    And its in the QWidget where you should paint, not in PaintEventInScrollArea which is the upper one!
    Subclass your QWidget (the one you put in to your QScrollArea, the one your call 'viewport ') and overload it's paintEvent().
    ==========================signature=============== ==================
    S.O.L.I.D principles (use them!):
    https://en.wikipedia.org/wiki/SOLID_...iented_design)

    Do you write clean code? - if you are TDD'ing then maybe, if not, your not writing clean code.

  5. #5
    Join Date
    Oct 2008
    Location
    Aachen
    Posts
    20
    Thanks
    5
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Windows

    Default Re: How to draw in a QScrollArea?

    Fine. I simply left the widget as it was and later wrap the whole stuff as you suggested in a QScrollArea with the method setWidget().

Similar Threads

  1. Draw a bar chart inisde the QScrollArea
    By ultrasonic in forum Qt Programming
    Replies: 6
    Last Post: 9th August 2012, 21:06
  2. Replies: 10
    Last Post: 11th February 2011, 00:31
  3. QScrollArea
    By franco.amato in forum Qt Programming
    Replies: 10
    Last Post: 6th January 2010, 21:19
  4. Replies: 2
    Last Post: 10th March 2008, 21:16
  5. QScrollArea
    By ToddAtWSU in forum Qt Programming
    Replies: 5
    Last Post: 13th December 2007, 18:20

Tags for this Thread

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.