Qt 4.6에서 간단한 게임을 만들려고합니다. 아이디어는 두 개의 위젯을 갖는 것입니다. 하나는 주 윈도우 위젯이고 공간을 나타내며 두 번째 위젯은 공간 (상위) 내의 우주선 위젯입니다. 단순화 된 코드는 다음과 같습니다다른 하나 (부모) 안에 하나의 위젯을 그릴 때의 문제
내가 컴파일/*this is ship and child widget*/
class MyRect : public QWidget {
Q_OBJECT
public:
MyRect(QWidget* parent)
: QWidget(parent)
{
itsParent = parent;
itsx = 120;
itsy = 250;
itsw = 110;
itsh = 35;
body = new QRect(itsx, itsy, itsw, itsh);
}
~MyRect() {}
protected:
void paintEvent(QPaintEvent *event);
private:
int itsx;
int itsy;
int itsw;
int itsh;
QRect* body;
QWidget* itsParent;
};
void MyRect::paintEvent(QPaintEvent *event)
{
QPen pen(Qt::black, 2, Qt::SolidLine);
QColor hourColor(0, 255, 0);
QPainter painter(itsParent);
painter.setBrush(hourColor);
painter.setPen(pen);
painter.drawRect(*body);
}
/*this is space and main window widget*/
class space : public QMainWindow
{
Q_OBJECT
public:
space(QWidget *parent = 0);
~space();
protected:
private:
MyRect* ship;
};
space::space(QWidget *parent)
: QMainWindow(parent)
{
ship = new MyRect(this);
}
, 화면이 비어 및 사각형 MyRect::body
이 그려지지 않습니다. Qt 온라인 문서를 확인하고 운이 좋은 Google 연구를했습니다. 이에 대한 설명은 언제나 환영합니다. 하나의 위젯을 다른 (부모) 위에 그려 넣고 싶습니다.
나는 또한 QGraphicsView를 살펴볼 것을 제안합니다. –