Results 1 to 3 of 3

Thread: QStackedWidget - paintevent of widget not getting called

  1. #1
    Join Date
    Jul 2010
    Posts
    34
    Thanks
    10
    Qt products
    Qt4
    Platforms
    Windows

    Default QStackedWidget - paintevent of widget not getting called

    Hi,

    Please consider the below codes:

    File : mainwindow.cpp
    Qt Code:
    1. mainwindow::mainwindow()
    2. {
    3. men = new menu1(this);
    4.  
    5. q1 = new QPushButton("q1", men);
    6. connect(quran1, SIGNAL(clicked()), this, SLOT(qopen()));
    7.  
    8. qs = new QStackedWidget(this);
    9. qs->addWidget(men);
    10. qs->setCurrentIndex(0);
    11.  
    12. show();
    13. }
    To copy to clipboard, switch view to plain text mode 

    File : men.cpp
    Qt Code:
    1. menu1::menu1(QWidget *parent) :
    2. QWidget(parent)
    3. {
    4. resize(880, 580);
    5. }
    6.  
    7. void menu1::paintEvent(QPaintEvent *) // THIS JUST DRAWS A GREEN CURVE
    8. {
    9. QPainter painter(this);
    10. painter.setRenderHint(QPainter::Antialiasing, true);
    11. path.moveTo(qrand() % 80, qrand() % 320);
    12. path.cubicTo(200, 200, 320, 80, 480, 320);
    13. painter.setPen(QPen(Qt::green, 8));
    14. painter.drawPath(path);
    15. }
    To copy to clipboard, switch view to plain text mode 


    The problem is, whenever I run the above code, in the output, all I get is the pushbutton but not the green curve which is in the paintevent of menu1.


    HOWEVER,

    When I remove the QStackedWidget from mainwindow.cpp as shown below, I get both the pushbutton AND the green curve.

    Qt Code:
    1. mainwindow::mainwindow()
    2. {
    3. men = new menu1(this);
    4.  
    5. q1 = new QPushButton("q1", men);
    6. connect(quran1, SIGNAL(clicked()), this, SLOT(qopen()));
    7.  
    8. show();
    9. }
    To copy to clipboard, switch view to plain text mode 


    What is the problem with QStackedWidget? How can I get both the pushbutton and the curve to appear with the first example of mainwindow?

    Regards.

  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: QStackedWidget - paintevent of widget not getting called

    Try giving the stacked widget a large enough size explicitly.
    ==========================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. The following user says thank you to high_flyer for this useful post:

    el33t (16th February 2011)

  4. #3
    Join Date
    Jul 2010
    Posts
    34
    Thanks
    10
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: QStackedWidget - paintevent of widget not getting called

    Quote Originally Posted by high_flyer View Post
    Try giving the stacked widget a large enough size explicitly.
    Yeah, your trick worked perfectly. Thanks.

Similar Threads

  1. paintEvent not getting called with QMainWindow
    By DiamonDogX in forum Qt Programming
    Replies: 12
    Last Post: 15th June 2011, 23:23
  2. No paintEvent is called
    By HeReSY in forum Qt Programming
    Replies: 2
    Last Post: 26th January 2011, 17:11
  3. Replies: 0
    Last Post: 29th April 2010, 06:44
  4. Shared widget by pages in a QStackedWidget
    By ouekah in forum Newbie
    Replies: 2
    Last Post: 7th March 2010, 11:45
  5. Replies: 4
    Last Post: 13th August 2007, 16:28

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.