Results 1 to 7 of 7

Thread: QPainter Constructor Syntax Question

  1. #1
    Join Date
    Jan 2011
    Posts
    8
    Thanks
    4
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Question QPainter Constructor Syntax Question

    I have a quick question about the QPainter examples I have seen. The syntax seems strange and doesn't look like it should compile.

    Qt Code:
    1. QPainter painter(this);
    To copy to clipboard, switch view to plain text mode 

    I would expect this to be something like:

    Qt Code:
    1. QPainter painter = new QPainter(this);
    To copy to clipboard, switch view to plain text mode 

    or even something like this:

    Qt Code:
    1. QPainter painter = QPainter::fromWidget(this);
    To copy to clipboard, switch view to plain text mode 

    But instead it looks like a syntax error. So how and why does this work? I'm from C# and still a beginner with C++ and Qt, so thanks in advance for answering such newbie question

  2. #2
    Join Date
    Jan 2006
    Location
    Munich, Germany
    Posts
    4,714
    Thanks
    21
    Thanked 418 Times in 411 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: QPainter Constructor Syntax Question

    Please google about stack vs heap allocation in C++, and the concept of pointers.
    ==========================signature=============== ==================
    S.O.L.I.D principles (use them!):
    https://en.wikipedia.org/wiki/SOLID_...iented_design)

    Do you write clean code? - if you are TDD'ing then maybe, if not, your not writing clean code.

  3. #3
    Join Date
    Jan 2011
    Posts
    8
    Thanks
    4
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: QPainter Constructor Syntax Question

    Since I already gave an example of creating the object on the heap, I can only assume you are trying to say that this is the syntax C++ uses to create objects on the stack. The question I have is why is the object created in this way as opposed to on the heap like nearly every other object I've seen examples of?

  4. #4
    Join Date
    Jan 2006
    Location
    Germany
    Posts
    4,380
    Thanks
    19
    Thanked 1,005 Times in 913 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows Symbian S60
    Wiki edits
    5

    Default Re: QPainter Constructor Syntax Question

    Quote Originally Posted by deejross View Post
    Since I already gave an example of creating the object on the heap,
    ...which were wrong. You are missing a *.
    But anyway. What do you don't like with
    Qt Code:
    1. QPainter p(this);
    To copy to clipboard, switch view to plain text mode 
    Would
    Qt Code:
    To copy to clipboard, switch view to plain text mode 
    satisfy you? If so great, because
    Qt Code:
    1. QPainter p(this);
    To copy to clipboard, switch view to plain text mode 
    is just a shorthand for
    Qt Code:
    1. p.begin(this);
    To copy to clipboard, switch view to plain text mode 

    Or please elaborate, what puzzles you exactly.

  5. The following user says thank you to Lykurg for this useful post:

    deejross (25th January 2011)

  6. #5
    Join Date
    Jan 2011
    Posts
    8
    Thanks
    4
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: QPainter Constructor Syntax Question

    The syntax is what confused me. I had never seen parameters passed right after the variable name before, so I had no idea what was going on. Now that I know that it is creating the object, but just on the stack instead of the heap, that helps. Thanks.

  7. #6
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,729
    Thanks
    13
    Thanked 1,610 Times in 1,537 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Default Re: QPainter Constructor Syntax Question

    The example that puzzles you is allocation on the stack. The bracketed part of the expression is the arguments to the QPainter constructor. If you wrote:
    Qt Code:
    To copy to clipboard, switch view to plain text mode 
    you would be creating a QPainter using its default (parameterless) constructor. The example you give uses a different constructor to build the QPainter and initialise it in some way. The constructor options are in the Assistant docs for QPainter. The same syntax can be used to initialise ('construct') basic types created on the stack:
    Qt Code:
    1. int x(9);
    2. char c('A');
    To copy to clipboard, switch view to plain text mode 

    You have given a (slightly incorrect) example of allocating the QPainter on the heap.
    Your second example may be either stack or heap (with the same missing * as your first) depending on what your fictious QPainter::fromWidget() is supposed to return (a pointer or an instance of QPainter).

  8. The following user says thank you to ChrisW67 for this useful post:

    deejross (25th January 2011)

  9. #7
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: QPainter Constructor Syntax Question

    And not looking too far in nearly all Qt apps we have:
    Qt Code:
    1. QApplication app(argc, argv);
    To copy to clipboard, switch view to plain text mode 

    By the way, there is one glitch - if a class has a default constructor, you might be tempted to do this:
    Qt Code:
    To copy to clipboard, switch view to plain text mode 
    which is wrong as it is a declaration of a function called "p" returning a QPainter object. A small C++ standard anomaly It becomes more obvious if you rewrite it as:
    Qt Code:
    1. QPainter p(void);
    To copy to clipboard, switch view to plain text mode 
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


Similar Threads

  1. Qpainter in Constructor
    By augusbas in forum Newbie
    Replies: 4
    Last Post: 28th October 2010, 00:45
  2. qglwidget and constructor question on mac
    By john_god in forum Qt Programming
    Replies: 0
    Last Post: 17th June 2010, 00:51
  3. QtScript: default constructor question
    By QPlace in forum Qt Programming
    Replies: 1
    Last Post: 22nd October 2009, 19:36
  4. Replies: 0
    Last Post: 31st May 2009, 16:51
  5. QPaintEvent syntax question
    By last2kn0 in forum Newbie
    Replies: 5
    Last Post: 25th January 2008, 20:36

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.