PDA

View Full Version : QPainter *painter vs. QPainter painter



gebbissimo
2nd September 2012, 15:36
Hi again,

probably a really simple question, but I have searched the web for quite a while now, without resolving it:


void Field::paintEvent(QPaintEvent *)
{
QPainter *painter;
(*painter).begin(this);
}


This doesn't work (I get no compile error, just a warning message "uninitialized local variable 'painter' used". When I run it however, the error message pops up: "read access violiation...". ) However, if I remove the asterix it works. Looking forward to an answer...

mvuori
2nd September 2012, 15:49
With QPainter *painter; you declare a pointer variable -- that happens to point to a random memory location, but never to QPainter object... And when you try to access that memory location, of course you have an access violation.

When you use a pointer variable, you need to allocate the object in heap with new operator: QPainter painter = new QPainter(this);

Of course the simplest way is to create painter in stack: QPainter painter(this). You don't need begin at all().

gebbissimo
2nd September 2012, 16:09
mvuori, thank you very much for the super quick reply. It does make a lot of sense!

So to initiate an object there are really just two options, are there? Either

Object myobject
Object *myobject = new Object



Just one more question: Which method does make more sense in this case using QPainter? I've read that for QWidget types it's beneficial to always create them dynamically (on the heap). I didn't get the point 100%, but somehow they delete themselves automatically with their parents then. Since QPainter does not inherit QWidget I believe the first method is appropriate here, since static (on the stack) allocation is a bit quicker? Or am I talking rubbish...

tbscope
2nd September 2012, 18:44
You should start with basic C++. Find a good book or tutorial.

gebbissimo
4th September 2012, 18:53
@tbscope: It's not that I started Qt right away, I did read a C++ tutorial (http://cplusplus.com/doc/tutorial/) at first. And when I read sample code now, I feel like I understand all of it (at least the c++ syntax).
I agree with you that my initial question was very beginnerish and that I maybe should have known it after reading the tutorial. On the other hand I believe that practicing to code is more helpful to me at the moment than to read another tutorial / book.


I stumbled upon one more question, on which I couldn't find help on the web. In the following program the button with the text "doesn't work" is not being shown. The other two buttons (in comments) work though. Why is that so?


// main.cpp

int main(int argc, char *argv[])
{
QApplication a(argc, argv);

Mywidget w;
//QPushButton mybutton("works",&w);
w.resize(200,200);
w.show();

return a.exec();
}





// mywidget.cpp

#include "mywidget.h"
#include <QWidget>
#include <QDebug>
#include <QPushButton>


Mywidget::Mywidget(QWidget *parent) : QWidget(parent)
{
QPushButton mybutton("doesn't work",this);
//QPushButton *dynamic = new QPushButton("works, too", this);
}

spirit
4th September 2012, 19:15
Did you know the difference between local variables and allocated ones?
In you case "mybutton" will be destroyed immediately after reaching the end of the MyWidget constructor.
You should really listen tbscope suggestion -- get & read C++ book.

ZikO
5th September 2012, 00:58
You should start with basic C++. Find a good book or tutorial.
Thinking in C++ 2nd ed. (http://www.mindview.net/Books/TICPP/ThinkingInCPP2e.html) - well explained theory and a lot of practical examples including quite challenging exercises that test level of understanding the language. Tom I covers basics: variables, their scopes, pointers, references, classes, constructors, destructors, inheritance, polymorphism, operators, dynamic memory allocation, and the introduction to templates. Tom II covers exceptions, I/O streams, strings, multi inheritance, locales, wide characters, advanced templates, algorithms, STL etc.
EDIT: it's for free

Introduction to Design Patterns in C++ with Qt (http://www.ics.com/files/designpatterns/book/index.html) - more like a manual but provides an explanation of both C++ and Qt. I would read it when C++ is fairly well known.

EDIT: I used to think I'd know C++ just reading the manual but I Thinking in C++