PDA

View Full Version : child widget does not display in parent widget



fitzsir
4th April 2013, 22:22
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.



// main.cpp

#include <QApplication>
#include "ChildTest.h"

int main(int argc, char** argv)
{
// start the display application
QApplication app(argc, argv);
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:
QWidget* m_mainWindow;
ChildPart* m_child;
};

#endif // CHILDTEST_H

// ChildTest.cpp

#include "ChildTest.h"

ChildTest::ChildTest()
{
m_mainWindow = new QWidget();
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>

class ChildPart : public QWidget
{
Q_OBJECT

public:
ChildPart(QWidget* parent = 0);


protected:
void paintEvent(QPaintEvent* event);

private:
QRect m_rect;
};

#endif // CHILDPART_H

// ChildPart.cpp

#include <QtGui>
#include "ChildPart.h"
#include <QDebug>

ChildPart::ChildPart(QWidget *parent) :
QWidget(parent)
{
}

void ChildPart::paintEvent(QPaintEvent*)
{
qDebug() << "ChildPart::paintEvent ";

QPainter painter(this);
painter.setBrush(Qt::green);
painter.drawRect(QRect(100, 100, 50, 50));
}

ChrisW67
5th April 2013, 03:22
What size do you think your m_child is? Where do you expect to see it?