PDA

View Full Version : QPainter



phil_
16th December 2006, 15:49
Hello,

I want to use QPainter to draw very simple lines and points. I checked the docs and wanted to copy the example, but the following code doesn't work:



int main(int argc, char *argv[])
{
QApplication app(argc, argv);
QWidget fenster1;
fenster1.resize(400,400);


QPainter achsen(&fenster1);

achsen.setPen(QPen(Qt::blue,2));

achsen.drawPoint(200,100);

achsen.drawLine(100,100,301,301);

fenster1.show();

return app.exec();
}


It doesn't return an error, but I can neither see the line nor the point. It just displays an empty window. Where is the mistake?

Thanks.

jacek
16th December 2006, 16:14
You can't paint on hidden widgets, because these operations will be just ignored. Another problem is that everything you paint on a widget isn't remembered, so if that widget is hidden or covered by another window, you will loose everything you have drawn. Third problem is that you shouldn't draw anything outside paintEvent() in Qt4, because of "backing store".

The correct solution is to subclass QWidget and reimplement paintEvent() (see the attachment).

phil_
16th December 2006, 17:25
Thanks very much. After trying to compile your main.cpp I get a problem that most likely is caused by my qmake:



release/main.moc:35: error: `Test' has not been declared
release/main.moc:40: error: `Test' has not been declared
release/main.moc:41: error: non-member function `const QMetaObject* metaObject()
' cannot have `const' method qualifier
release/main.moc:45: error: `Test' has not been declared
release/main.moc: In function `void* qt_metacast(const char*)':
release/main.moc:49: error: `Test' has not been declared
release/main.moc:49: error: expected `>' before '*' token
release/main.moc:49: error: expected `(' before '*' token
release/main.moc:49: error: expected primary-expression before '>' token
release/main.moc:49: error: invalid use of `this' in non-member function
release/main.moc:49: error: expected `)' before ';' token
release/main.moc:50: error: cannot call member function `virtual void* QWidget::
qt_metacast(const char*)' without object
release/main.moc: At global scope:
release/main.moc:53: error: `Test' has not been declared
release/main.moc: In function `int qt_metacall(QMetaObject::Call, int, void**)':

release/main.moc:55: error: cannot call member function `virtual int QWidget::qt
_metacall(QMetaObject::Call, int, void**)' without object
mingw32-make[1]: *** [release\main.o] Error 1
mingw32-make[1]: Leaving directory `D:/projekte/Schaubild/cpp2'
mingw32-make: *** [release] Error 2


Is there perhaps any option I have to change in any settings?

jacek
16th December 2006, 17:33
If you have saved that file as "main.cpp" in an empty directory, you should be able to compile it using:
qmake -project
qmake
make

phil_
16th December 2006, 19:08
No, that doesn't work.

jacek
16th December 2006, 19:54
No, that doesn't work.
How exactly it "doesn't work"? Do you issue these commands from the "Qt 4.x.x Command Prompt"?