Results 1 to 3 of 3

Thread: Draw on top of a non empty widget

  1. #1
    Join Date
    Nov 2013
    Location
    Kungsbacka, Sweden
    Posts
    13
    Thanks
    2
    Thanked 2 Times in 2 Posts
    Qt products
    Qt5
    Platforms
    Windows

    Default Draw on top of a non empty widget

    I'm trying to understand the paintEvent function. What I want to do is to draw on top of a widget which contains different objects such as QLabel, QTextEdit, QPushButton or even other widgets. I made a quick example to demonstrate my wish. Consider the following code:

    MyWidget.h
    Qt Code:
    1. #ifndef MYWIDGET_H
    2. #define MYWIDGET_H
    3.  
    4. #include <QWidget>
    5. #include <QLabel>
    6. #include <QTextEdit>
    7.  
    8. class MyWidget : public QWidget
    9. {
    10. Q_OBJECT
    11. protected:
    12. void paintEvent(QPaintEvent *);
    13. public:
    14. MyWidget(QWidget *parent = 0);
    15. private:
    16. QLabel *label;
    17. };
    18.  
    19. #endif
    To copy to clipboard, switch view to plain text mode 
    MyWidget.cpp
    Qt Code:
    1. #include <QVBoxLayout>
    2. #include <QPainter>
    3. #include "MyWidget.h"
    4.  
    5. MyWidget::MyWidget(QWidget *parent)
    6. : QWidget(parent)
    7. {
    8. QString text = "Some random text";
    9. label = new QLabel(text, this);
    10. label->setAlignment(Qt::AlignCenter);
    11. QVBoxLayout *vbox = new QVBoxLayout();
    12. vbox->addWidget(label);
    13. setLayout(vbox);
    14. }
    15.  
    16. void MyWidget::paintEvent(QPaintEvent *)
    17. {
    18. QPainter painter(this);
    19. painter.drawLine(100,50,300,50);
    20. }
    To copy to clipboard, switch view to plain text mode 
    main.cpp
    Qt Code:
    1. #include <QApplication>
    2. #include "MyWidget.h"
    3.  
    4. int main(int argc, char *argv[])
    5. {
    6. QApplication app(argc, argv);
    7. MyWidget window;
    8. window.resize(400,400);
    9. window.show();
    10. return app.exec();
    11. }
    To copy to clipboard, switch view to plain text mode 

    This draws a line on my widget even if it contains a QLabel object.
    Now, if I replace the QLabel with a QTextEdit covering the whole widget, the line is no longer visible. I guess the QTextEdit gets drawn on top of the line. What I'm wondering is if it's possible to force the widget to draw the line after the QTextEdit has been drawn? Or in general, is it possible to specify when a paintEvent shall occur? If this is impossible, what should I do if I want to draw on say a QTextEdit but still be able to write text?

    Help much appreciated

    Thanks!

  2. #2
    Join Date
    Mar 2011
    Location
    Hyderabad, India
    Posts
    1,882
    Thanks
    3
    Thanked 452 Times in 435 Posts
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows
    Wiki edits
    15

    Default Re: Draw on top of a non empty widget

    What I'm wondering is if it's possible to force the widget to draw the line after the QTextEdit has been drawn?
    When the child widgets of a widget are inside a layout, the layout will decide the order of painting the child widget. When the child widgets are not in a layout, then first parent is painted and then children are painted in the order of creation.

    Or in general, is it possible to specify when a paintEvent shall occur?
    Generally speaking no, but order of painting can be controlled by indirect means, like using stacked layout/stacked widgets etc.

    If this is impossible, what should I do if I want to draw on say a QTextEdit but still be able to write text?
    Well you can write a custom QTextEdit and draw on top it, and put this custom TextEdit in a layout or a child widget.
    Qt Code:
    1. class TextEdit : public QTextEdit
    2. {
    3. public:
    4. explicit TextEdit(QWidget * parent = 0) : QTextEdit(parent) { }
    5. protected:
    6. void paintEvent(QPaintEvent * event)
    7. {
    8. QTextEdit::paintEvent(event);
    9. QPainter painter(this->viewport());
    10. painter.drawLine(100, 50, 300, 150);
    11. }
    12. };
    13.  
    14. int main(int argc, char *argv[])
    15. {
    16. QApplication app(argc, argv);
    17. TextEdit window;
    18. window.resize(400,400);
    19. window.show();
    20. return app.exec();
    21. }
    To copy to clipboard, switch view to plain text mode 
    When you know how to do it then you may do it wrong.
    When you don't know how to do it then it is not that you may do it wrong but you may not do it right.

  3. #3
    Join Date
    Nov 2013
    Location
    Kungsbacka, Sweden
    Posts
    13
    Thanks
    2
    Thanked 2 Times in 2 Posts
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: Draw on top of a non empty widget

    Exactly the kind of answer I was hoping for. Thanks a lot Santosh Reddy! Custom made objects will be perfect in my application. Thanks for letting me know how to create them!

    Problem SOLVED

Similar Threads

  1. QGLWidget is empty on Mac, cannot draw
    By rybka007 in forum Qt Programming
    Replies: 1
    Last Post: 29th June 2013, 13:39
  2. layout in widget give me an empty window!
    By rimie23 in forum Qt Programming
    Replies: 20
    Last Post: 5th June 2012, 18:59
  3. Leaving an empty space around a widget
    By moatilliatta in forum Qt Programming
    Replies: 1
    Last Post: 11th October 2010, 16:03
  4. Replies: 1
    Last Post: 10th December 2009, 21:31
  5. Getting an item from an empty table widget
    By ProTonS in forum Qt Programming
    Replies: 2
    Last Post: 13th July 2009, 02:24

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.