Results 1 to 7 of 7

Thread: QPainter *painter vs. QPainter painter

  1. #1
    Join Date
    Aug 2012
    Posts
    5
    Thanks
    2
    Qt products
    Qt4
    Platforms
    Windows

    Unhappy QPainter *painter vs. QPainter painter

    Hi again,

    probably a really simple question, but I have searched the web for quite a while now, without resolving it:
    Qt Code:
    1. void Field::paintEvent(QPaintEvent *)
    2. {
    3. QPainter *painter;
    4. (*painter).begin(this);
    5. }
    To copy to clipboard, switch view to plain text mode 

    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...

  2. #2
    Join Date
    May 2011
    Posts
    239
    Thanks
    4
    Thanked 35 Times in 35 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Symbian S60

    Default Re: QPainter *painter vs. QPainter painter

    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().

  3. The following user says thank you to mvuori for this useful post:

    gebbissimo (2nd September 2012)

  4. #3
    Join Date
    Aug 2012
    Posts
    5
    Thanks
    2
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: QPainter *painter vs. QPainter painter

    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...

  5. #4
    Join Date
    Jan 2006
    Location
    Belgium
    Posts
    1,938
    Thanked 268 Times in 268 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows
    Wiki edits
    20

    Default Re: QPainter *painter vs. QPainter painter

    You should start with basic C++. Find a good book or tutorial.

  6. #5
    Join Date
    Aug 2012
    Posts
    5
    Thanks
    2
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: QPainter *painter vs. QPainter painter

    @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?
    Qt Code:
    1. // main.cpp
    2.  
    3. int main(int argc, char *argv[])
    4. {
    5. QApplication a(argc, argv);
    6.  
    7. Mywidget w;
    8. //QPushButton mybutton("works",&w);
    9. w.resize(200,200);
    10. w.show();
    11.  
    12. return a.exec();
    13. }
    To copy to clipboard, switch view to plain text mode 


    Qt Code:
    1. // mywidget.cpp
    2.  
    3. #include "mywidget.h"
    4. #include <QWidget>
    5. #include <QDebug>
    6. #include <QPushButton>
    7.  
    8.  
    9. Mywidget::Mywidget(QWidget *parent) : QWidget(parent)
    10. {
    11. QPushButton mybutton("doesn't work",this);
    12. //QPushButton *dynamic = new QPushButton("works, too", this);
    13. }
    To copy to clipboard, switch view to plain text mode 

  7. #6
    Join Date
    Aug 2008
    Location
    Ukraine, Krivoy Rog
    Posts
    1,963
    Thanked 370 Times in 336 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: QPainter *painter vs. QPainter painter

    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.
    Qt Assistant -- rocks!
    please, use tags [CODE] & [/CODE].

  8. #7
    Join Date
    Nov 2009
    Posts
    61
    Thanks
    9
    Thanked 3 Times in 3 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Windows

    Default Re: QPainter *painter vs. QPainter painter

    Quote Originally Posted by tbscope View Post
    You should start with basic C++. Find a good book or tutorial.
    Thinking in C++ 2nd ed. - 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 - 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++
    Last edited by ZikO; 5th September 2012 at 01:17.

Similar Threads

  1. Replies: 5
    Last Post: 20th October 2009, 07:18
  2. Replies: 5
    Last Post: 7th September 2009, 20:57
  3. Replies: 1
    Last Post: 4th September 2008, 14:55
  4. use Qpainter to draw lines + problem in my painter
    By ReSu in forum Qt Programming
    Replies: 4
    Last Post: 5th March 2008, 15:44
  5. Replies: 3
    Last Post: 30th April 2006, 19:22

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.