Results 1 to 11 of 11

Thread: Simple C++ question -- Creating a QPoint object

  1. #1
    Join Date
    Aug 2009
    Location
    United States
    Posts
    45
    Thanks
    20
    Qt products
    Qt4
    Platforms
    Windows

    Default Simple C++ question -- Creating a QPoint object

    I was trying to set the position of my program's window and had to use a QPoint object. It's working now, but I'm a little confused on something. I first tried to create the object like this:
    Qt Code:
    1. QPoint windowPos = new QPoint(0, 0);
    To copy to clipboard, switch view to plain text mode 

    But that gave the error:
    error: conversion from `QPoint*' to non-scalar type `QPoint' requested
    I was able to get it to work with this line of code:
    Qt Code:
    1. QPoint windowPos(0, 0);
    To copy to clipboard, switch view to plain text mode 

    But I don't understand why the first line didn't work. I thought that was the correct method for creating an object in C++?

    I would appreciate someone explaining how my thinking is wrong, thanks

  2. #2
    Join Date
    Apr 2009
    Posts
    24
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Simple C++ question -- Creating a QPoint object

    hi, first u tell me that in which file where you used that code.?

  3. #3
    Join Date
    May 2008
    Location
    Kyiv, Ukraine
    Posts
    418
    Thanks
    1
    Thanked 29 Times in 27 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: Simple C++ question -- Creating a QPoint object

    This is not correct
    Qt Code:
    1. QPoint windowPos = new QPoint(0, 0);
    To copy to clipboard, switch view to plain text mode 

    You create and object that is supposed to be on stack as if it were an object to be on heap. Don't mix these things.

    Do it this way
    Qt Code:
    1. QPoint *windowPos = new QPoint(0, 0);
    To copy to clipboard, switch view to plain text mode 
    or
    Qt Code:
    1. QPoint windowPos(0, 0);
    To copy to clipboard, switch view to plain text mode 
    I'm a rebel in the S.D.G.

  4. The following user says thank you to lyuts for this useful post:

    N3wb (17th September 2009)

  5. #4
    Join Date
    Sep 2009
    Posts
    140
    Thanks
    4
    Thanked 17 Times in 17 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Simple C++ question -- Creating a QPoint object

    Hi,

    Quote Originally Posted by N3wb View Post
    But that gave the error:
    error: conversion from `QPoint*' to non-scalar type `QPoint' requested
    The compiler message means that your are trying to affect a pointer to a QPoint (a QPoint*), to a QPoint. It is C++ basic features.

    If you take time to decode (it is true it is not so clear as Java) messages, you'll find out what's happening

  6. #5
    Join Date
    Aug 2009
    Location
    United States
    Posts
    45
    Thanks
    20
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Simple C++ question -- Creating a QPoint object

    So... This creates the QPoint on the heap:
    Qt Code:
    1. QPoint *windowPos = new QPoint(0, 0);
    To copy to clipboard, switch view to plain text mode 

    And this creates it on the stack?
    Qt Code:
    1. QPoint windowPos(0, 0);
    To copy to clipboard, switch view to plain text mode 

  7. #6
    Join Date
    Sep 2009
    Posts
    140
    Thanks
    4
    Thanked 17 Times in 17 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Simple C++ question -- Creating a QPoint object

    You got it

  8. #7
    Join Date
    May 2008
    Location
    Kyiv, Ukraine
    Posts
    418
    Thanks
    1
    Thanked 29 Times in 27 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: Simple C++ question -- Creating a QPoint object

    Yes, this is correct.
    I'm a rebel in the S.D.G.

  9. #8
    Join Date
    Aug 2009
    Location
    United States
    Posts
    45
    Thanks
    20
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Simple C++ question -- Creating a QPoint object

    Great, thanks!

    Since the QPoint is something that is just needed temporarily, would it be better to create it on the stack?

    And should I always create a widget on the heap?

  10. #9
    Join Date
    Sep 2009
    Posts
    140
    Thanks
    4
    Thanked 17 Times in 17 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Simple C++ question -- Creating a QPoint object

    For your case you should better allocate your QPoint on the stack, cause it is small

    Generally, it depends on memory management and perfomance features.

  11. #10
    Join Date
    Sep 2008
    Posts
    18
    Thanks
    2
    Thanked 3 Times in 3 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: Simple C++ question -- Creating a QPoint object

    N3wb: You should really get a good book on C++ first, or try Python instead with some PyQt tutorials and stuff. Jumping into C++ like this is a recipe for very poor code a LOT of frustration.

  12. #11
    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: Simple C++ question -- Creating a QPoint object

    Quote Originally Posted by N3wb View Post
    Since the QPoint is something that is just needed temporarily, would it be better to create it on the stack?
    Always create QPoint on the stack.

    And should I always create a widget on the heap?
    Yes, with exception of widgets that have (and you know will always have) no parents.
    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.


  13. The following user says thank you to wysota for this useful post:

    N3wb (17th September 2009)

Similar Threads

  1. Simple namespace question
    By JPNaude in forum Qt Programming
    Replies: 3
    Last Post: 30th June 2009, 09:31
  2. QTextEdit simple question
    By Marcopolo in forum Qt Tools
    Replies: 4
    Last Post: 11th October 2007, 00:01
  3. Sending simple object via udp...with a twist
    By the scribe in forum Qt Programming
    Replies: 1
    Last Post: 6th June 2007, 10:16
  4. Creating object of other class in Run() method
    By santosh.kumar in forum Qt Programming
    Replies: 2
    Last Post: 15th May 2007, 15:05
  5. Creating simple text editor from the eamples code
    By overcast in forum Qt Programming
    Replies: 4
    Last Post: 14th February 2007, 15:46

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.