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.
Code:
// main.cpp
#include <QApplication>
#include "ChildTest.h"
int main(int argc, char** argv)
{
// start the display application
ChildTest child;
child.show();
return app.exec();;
}
// ChildTest.h
#ifndef CHILDTEST_H
#define CHILDTEST_H
#include "ChildPart.h"
class ChildTest
{
public:
ChildTest();
void show();
private:
ChildPart* m_child;
};
#endif // CHILDTEST_H
// ChildTest.cpp
#include "ChildTest.h"
ChildTest::ChildTest()
{
m_mainWindow->resize(800, 400);
m_mainWindow->show();
m_child = new ChildPart(m_mainWindow);
}
void ChildTest::show()
{
m_child->show();
}
// ChildPart.h
#ifndef CHILDPART_H
#define CHILDPART_H
#include <QWidget>
{
Q_OBJECT
public:
protected:
private:
};
#endif // CHILDPART_H
// ChildPart.cpp
#include <QtGui>
#include "ChildPart.h"
#include <QDebug>
ChildPart
::ChildPart(QWidget *parent
) :{
}
{
qDebug() << "ChildPart::paintEvent ";
painter.setBrush(Qt::green);
painter.
drawRect(QRect(100,
100,
50,
50));
}
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?