Results 1 to 2 of 2

Thread: child widget does not display in parent widget

  1. #1
    Join Date
    Apr 2013
    Posts
    1
    Qt products
    Qt4
    Platforms
    Windows

    Default child widget does not display in parent widget

    I create a parent widget and then a child widget. In the child's paintEvent() method, I simply draw a rectangle. I show() the child widgent and I see it's paintEvent() method called, but I do not see the rectangle drawn. I'm sure I missed something simple, but just don't know what. Thanks for your help.

    Qt Code:
    1. // main.cpp
    2.  
    3. #include <QApplication>
    4. #include "ChildTest.h"
    5.  
    6. int main(int argc, char** argv)
    7. {
    8. // start the display application
    9. QApplication app(argc, argv);
    10. ChildTest child;
    11.  
    12. child.show();
    13.  
    14. return app.exec();;
    15. }
    16.  
    17. // ChildTest.h
    18.  
    19. #ifndef CHILDTEST_H
    20. #define CHILDTEST_H
    21.  
    22. #include "ChildPart.h"
    23.  
    24. class ChildTest
    25. {
    26. public:
    27. ChildTest();
    28. void show();
    29.  
    30. private:
    31. QWidget* m_mainWindow;
    32. ChildPart* m_child;
    33. };
    34.  
    35. #endif // CHILDTEST_H
    36.  
    37. // ChildTest.cpp
    38.  
    39. #include "ChildTest.h"
    40.  
    41. ChildTest::ChildTest()
    42. {
    43. m_mainWindow = new QWidget();
    44. m_mainWindow->resize(800, 400);
    45. m_mainWindow->show();
    46.  
    47. m_child = new ChildPart(m_mainWindow);
    48. }
    49.  
    50. void ChildTest::show()
    51. {
    52. m_child->show();
    53. }
    54.  
    55. // ChildPart.h
    56.  
    57. #ifndef CHILDPART_H
    58. #define CHILDPART_H
    59.  
    60. #include <QWidget>
    61.  
    62. class ChildPart : public QWidget
    63. {
    64. Q_OBJECT
    65.  
    66. public:
    67. ChildPart(QWidget* parent = 0);
    68.  
    69.  
    70. protected:
    71. void paintEvent(QPaintEvent* event);
    72.  
    73. private:
    74. QRect m_rect;
    75. };
    76.  
    77. #endif // CHILDPART_H
    78.  
    79. // ChildPart.cpp
    80.  
    81. #include <QtGui>
    82. #include "ChildPart.h"
    83. #include <QDebug>
    84.  
    85. ChildPart::ChildPart(QWidget *parent) :
    86. QWidget(parent)
    87. {
    88. }
    89.  
    90. void ChildPart::paintEvent(QPaintEvent*)
    91. {
    92. qDebug() << "ChildPart::paintEvent ";
    93.  
    94. QPainter painter(this);
    95. painter.setBrush(Qt::green);
    96. painter.drawRect(QRect(100, 100, 50, 50));
    97. }
    To copy to clipboard, switch view to plain text mode 

  2. #2
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,729
    Thanks
    13
    Thanked 1,610 Times in 1,537 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Default Re: child widget does not display in parent widget

    What size do you think your m_child is? Where do you expect to see it?

Similar Threads

  1. How to resize parent widget when child widget is resized
    By ainne810329 in forum Qt Programming
    Replies: 4
    Last Post: 29th November 2011, 07:47
  2. Replies: 1
    Last Post: 11th March 2011, 19:34
  3. Replies: 7
    Last Post: 14th January 2010, 08:47
  4. Replies: 4
    Last Post: 3rd October 2009, 08:19
  5. How to move child widget with parent widget?
    By anupamgee in forum Qt Programming
    Replies: 3
    Last Post: 19th June 2009, 15:23

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.