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
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));
}
// 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));
}
To copy to clipboard, switch view to plain text mode
Bookmarks