Dear visitor, welcome to QtForum.org.
If this is your first visit here, please read the Help. It explains in detail how this page works.
To use all features of this page, you should consider registering.
Please use the registration form, to register here or read more information about the registration process.
If you are already registered, please login here.
problem declaring new painting class
Hi I'm trying to create class whith would paint something
but I get some errors when i try to compile it:
there is my files
pt.h
|
Source code
|
1
2
3
4
5
6
7
8
9
10
11
12
|
#ifndef TARGET_H
#defineTARGET_H
class target {
public:
target();
virtual ~target();
void paintTarget(QPainter &painter);
private:
};
#endif
|
pt.cpp
|
Source code
|
1
2
3
4
5
6
7
8
9
10
11
|
#include "target.h"
target::target()
{}
target::~target() {}
void target::paintTarget(QPainter &painter)
{
painter.setPen(Qt::blue,2);
painter.setBrush(Qt::yellow);
painter.drawEllipse(100,100,10,10);
}
|
i call it in another class using QPainter
|
Source code
|
1
2
3
|
private:
target trgt;
...
|
|
Source code
|
1
2
3
4
5
|
void plot::paintEvent(QPaintEvent* )
{
QPainter painter(this);
trgt.paintTarget(painter);
}
|
error:
|
Source code
|
1
2
3
4
5
|
In file included from pt.cpp:1:
pt.h:8: error: 'QPainter' has not been declared
pt.cpp:6: error: variable or field 'paintTarget' declared void
pt.cpp:6: error: 'QPainter' was not declared in this scope
pt.cpp:6: error: 'painter' was not declared in this scope
|
Where is mistake?
compiler tells you pretty plainly. What this has to do with Qt I don't really know.
#ifndef TARGET_H
#defineTARGET_H
class target {
public:
target();
virtual ~target();
void paintTarget(QPainter &painter);
private:
};
#endif
pt.h:8: error: 'QPainter' has not been declared
If you have a problem, CUT and PASTE your code. Do not retype or simplify it. Give a COMPLETE and COMPILABLE example of your problem. Otherwise we are all guessing the problem from a fabrication where relevant details are often missing.
so i heve to include qpainter to this clss or there is other way to correct the mistake?
In your header, if you use QPainter* or QPainter& const, then you can just forward declare it. But if you use QPainter& or QPainter, then you must #include.
|
Source code
|
1
2
3
4
5
6
|
class QPainter;
....
class xxx
{
void paintTarget(QPainter& const painter);
}
|
OR
|
Source code
|
1
2
3
4
5
6
|
#include <QPainter>
....
class xxx
{
void paintTarget(QPainter& painter);
}
|
If you have a problem, CUT and PASTE your code. Do not retype or simplify it. Give a COMPLETE and COMPILABLE example of your problem. Otherwise we are all guessing the problem from a fabrication where relevant details are often missing.