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