Hi all!
I am programming in Qt and I am trying to draw a line on the screen, but it fails.
My header file:
#ifndef GAMEAREA_H
#define GAMEAREA_H
#include <QWidget>
#include <QPainter>
#include "character.h"
class GameArea : public QWidget
{
public:
GameArea();
QSize sizeHint() const;
protected:
void paintEvent(QPaintEvent *event);
private:
int leftStop;
int rightStop;
QPainter painter;
};
#endif // GAMEAREA_H
and my source file:
#include "gamearea.h"
#include <QPointF>
#include <QMessageBox>
#include <QDir>
#include <QPaintEvent>
GameArea::GameArea()
: QWidget()
{
leftStop = 20;
rightStop = width() - 20;
QPainter painter(this);
setAutoFillBackground(true);
}
void GameArea::paintEvent(QPaintEvent *event)
{
QPainterPath path;
path.moveTo(10,10);
path.lineTo(50, 10);
path.lineTo(50, 50);
painter.drawPath(path);
}
QSize GameArea::sizeHint() const
{
return QSize(400, 300);
}
I add my drawing in a QMainWindow:
GameArea *gameArea = new GameArea();
QVBoxLayout *mainLayout = new QVBoxLayout;
mainLayout->addWidget(gameArea);
mainWidget->setLayout(mainLayout);
setCentralWidget(mainWidget);
The error I get is:
QPainter::begin: Paint device returned engine == 0, type: 1
As I understand from searching on the net I am trying to draw outside the drawing are.
By I can not understand why!!
Please help!!
Many thanks in advance!!
Marcux