Results 1 to 3 of 3

Thread: Using QPainter in MainWindow

  1. #1
    Join Date
    Nov 2013
    Location
    Chandigarh, India
    Posts
    62
    Thanks
    8
    Thanked 11 Times in 7 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Windows

    Default Using QPainter in MainWindow

    I explored various examples of QPainter wherein it is used by separate classes to draw objects. I wish to use QPainter in the Mainwindow itself to draw rectangles. Is it possible to do so? If yes, Can anyone help me with it?

  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: Using QPainter in MainWindow

    I wish to use QPainter in the Mainwindow itself to draw rectangles. Is it possible to do so?
    Well technically QMainWindow is a QWidget so you can use QPainter, but you will either have to sacrifice some features which QMainWindow provides (like dock, menu, toolbars, etc) or have to come up with a way to put your rectangle painting over or below the QMainWindow painting. Either ways it is not trival.

    If you just need to draw some rectangles, you better draw them on a custom QWidget and put that widget as central widget of the mainwindow.

    If yes, Can anyone help me with it?
    It is very similar to how you do it with a QWidget. Here is an example, run it as is you can see the rectangle on mainwindow, and then un-comment the code in main() and see the combox will be added to mainwidget and rectangle is not visible, this is because child widgets are draw over the parent widget.
    Qt Code:
    1. class MyMainWindow : public QMainWindow
    2. {
    3. Q_OBJECT
    4. public:
    5. explicit MyMainWindow(QWidget * parent = 0)
    6. : QMainWindow(parent)
    7. {
    8. ;
    9. }
    10.  
    11. protected:
    12. void paintEvent(QPaintEvent * event)
    13. {
    14. QMainWindow::paintEvent(event);
    15.  
    16. QPainter p(this);
    17. p.setPen(QColor(Qt::red));
    18. p.drawRect(event->rect().adjusted(10, 10, -10, -10));
    19. }
    20. };
    21.  
    22. int main(int argc, char ** argv)
    23. {
    24. QApplication app(argc, argv);
    25.  
    26. MyMainWindow m;
    27. // QComboBox * comboBox = new QComboBox;
    28. // comboBox->addItems(QStringList() << "One" << "Two" << "Three" << "Four");
    29. // m.setCentralWidget(comboBox);
    30. m.show();
    31.  
    32. return app.exec();
    33. }
    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. The following user says thank you to Santosh Reddy for this useful post:

    aaditya190 (9th December 2013)

  4. #3
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Using QPainter in MainWindow

    Quote Originally Posted by Santosh Reddy View Post
    If you just need to draw some rectangles, you better draw them on a custom QWidget and put that widget as central widget of the mainwindow.
    I would recommend you follow that suggestion instead of interfering with QMainWindow's internals

    Cheers,
    _

  5. The following user says thank you to anda_skoa for this useful post:

    aaditya190 (9th December 2013)

Similar Threads

  1. Replies: 2
    Last Post: 26th December 2012, 01:03
  2. Replies: 0
    Last Post: 6th November 2011, 09:22
  3. Replies: 10
    Last Post: 14th September 2011, 10:08
  4. Replies: 1
    Last Post: 12th April 2011, 09:53
  5. Replies: 3
    Last Post: 30th April 2006, 19:22

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.