PDA

View Full Version : Compile error for Qpainter



tonnot
7th October 2010, 08:07
I have the next errors. On the analog clock I have create a new function 'dibuja' that uses the 'thepainter' qpainter object ( in this case I use a previous image)

error: request for member 'translate' in 'painter', which is of non-class type 'QPainter*'
error: request for member 'scale' in 'painter', which is of non-class type 'QPainter*'
error: request for member 'setBrush' in 'painter', which is of non-class type 'QPainter*'
and etc..

Here is a piece of code :

QPainter thepainter(&image);
dibuja( &thepainter, thetime);

void AnalogClock::dibuja(QPainter *painter, QTime time)

My last code worked, I had all the code that now is in 'dibuja' at the paintevent. I have only placed the Qpainter code at a new function.
Why I have theese errors ?

Lykurg
7th October 2010, 08:25
Well, in your function you probably call something like
painter.translate();but it must be
painter->translate(); because in the function you have a pointer.

tonnot
7th October 2010, 08:30
AH ! Stupid me .
But, Why I can not to use without pointer ? This are the errors if I use no-pointer scheme...

..\..\..\include/QtGui/../../src/gui/painting/qpainter.h: In member function 'virtual void AnalogClock::paintEvent(QPaintEvent*)':
..\..\..\include/QtGui/../../src/gui/painting/qpainter.h:503: error: 'QPainter::QPainter(const QPainter&)' is private
..\analogclock\analogclock.cpp:115: error: within this context
..\analogclock\analogclock.cpp:115: error: initializing argument 1 of 'void AnalogClock::dibuja(QPainter, QTime)'

Lykurg
7th October 2010, 08:46
Whats the problem with using a pointer? And could you please show the real (simplified) source code alongside the error, that one could better see what exactly you do.

tonnot
7th October 2010, 09:34
This is the code.
It is an ampliation of analog example, with a second indicator
And I want to draw a hundredth indicator, but I'm trying to use a image 'record' (to avoid to draw everything at the hundredth.

I must to pass Qpainter using pointer, if not I have the errors...



analog.h

#ifndef ANALOGCLOCK_H
#define ANALOGCLOCK_H

#include <QWidget>
#include <QTime>

class AnalogClock : public QWidget
{
Q_OBJECT

public:
AnalogClock(QWidget *parent = 0);
void dibuja(QPainter *painter, QTime time);

protected:
void paintEvent(QPaintEvent *event);
void resizeEvent(QResizeEvent *event);
};

#endif

analog.cpp

#include <QtGui>

#include "analogclock.h"
#include <qdebug>


int segundo=0;
QImage image;


static const QPoint hourHand[3] = {
QPoint(7, 8),
QPoint(-7, 8),
QPoint(0, -40)
};
static const QPoint minuteHand[3] = {
QPoint(7, 8),
QPoint(-7, 8),
QPoint(0, -60)
};
static const QPoint secondHand[3] = {
QPoint(3, 12),
QPoint(-3, 12),
QPoint(0, -70)
};
static const QPoint centesima[3] = {
QPoint(2, 12),
QPoint(-2, 12),
QPoint(0, -80)
};




QColor hourColor(127, 0, 127);
QColor minuteColor(0, 127, 127, 191);
QColor secondColor(255, 127, 127, 191);
QColor centeColor(255, 127, 127, 191);

void laimagen(int x, int y)
{
image= QImage(x, y, QImage::Format_ARGB32_Premultiplied); //QImage::Format_RGB32

}

AnalogClock::AnalogClock(QWidget *parent) : QWidget(parent)

{
QTimer *timer = new QTimer(this);

connect(timer, SIGNAL(timeout()), this, SLOT(update()));

setWindowTitle(tr("Analog Clock"));
resize(200, 200);
segundo=0;
laimagen(200,200);
timer->start(10);

}


void AnalogClock::paintEvent(QPaintEvent *)

{
QTime thetime = QTime::currentTime();
if (segundo==0) segundo=thetime.second();

if (thetime.second()==segundo )
{
QPainter thepainter(&image); // se pinta en la imagen
dibuja( &thepainter, thetime);
segundo=(++segundo==60)?0:segundo++;
}
else
{
QPainter thepainter(this);
thepainter.drawImage(image.rect(), image, image.rect());
}
}

void AnalogClock::resizeEvent(QResizeEvent *event) {
int wi=event->size().width();
int he=event->size().height();
laimagen(wi,he);
}

void AnalogClock::dibuja(QPainter *painter, QTime time) {
int side = qMin(width(), height());
painter->setRenderHint(QPainter::Antialiasing);
painter->translate(width() / 2, height() / 2);
painter->scale(side / 200.0, side / 200.0);

painter->setPen(Qt::NoPen);
painter->setBrush(hourColor);
painter->save();
painter->rotate(30.0 * ((time.hour() + time.minute() / 60.0)));
painter->drawConvexPolygon(hourHand, 3);

painter->restore();
painter->setPen(hourColor);
for (int i = 0; i < 12; ++i) {
painter->drawLine(88, 0, 96, 0);
painter->rotate(30.0);
}
painter->setPen(Qt::NoPen);
painter->setBrush(minuteColor);
painter->save();
painter->rotate(6.0 * (time.minute() + time.second() / 60.0));
painter->drawConvexPolygon(minuteHand, 3);
painter->restore();
painter->save();
painter->rotate(time.second()*6.0);
painter->drawConvexPolygon(secondHand, 3);
painter->restore();
painter->save();
painter->setPen(minuteColor);
for (int j = 0; j < 60; ++j) {
if ((j % 5) != 0)
painter->drawLine(92, 0, 96, 0);
painter->rotate(6.0);
}
painter->restore();
painter->save();
painter->setPen(centeColor);
for (int j = 0; j < 360; ++j) {
painter->drawLine(90, 0, 92, 0);
painter->rotate(1.0);
}
painter->restore();
}

nish
7th October 2010, 09:47
QObject and Classes derived from it (eg QPainter) have their copy constructor and assignment operator private. so u can not do QPainter = QPainter.

EDIT: QPainter is not derived from QObject, but from the error it looks like it still has its cctor private

tonnot
7th October 2010, 10:04
MrDearh, can you explain me more about assignment operator private ?
And thanks for the answer

nish
7th October 2010, 10:34
see http://doc.trolltech.com/4.7/qobject.html#no-copy-constructor

nish
7th October 2010, 10:37
QPainter is not derived from QObject, but it follows the same rule of private ctor.