Results 1 to 3 of 3

Thread: Function arguments - how to get a reference

  1. #1
    Join Date
    Feb 2009
    Posts
    79
    Thanks
    11
    Thanked 5 Times in 5 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Question Function arguments - how to get a reference

    Hi,

    I have a basic general question.

    Most QT functions are called by reference, eg.

    Qt Code:
    1. QRectF::contains(QPointF & pt)
    To copy to clipboard, switch view to plain text mode 

    On the other hand, many functions return eg. just a QPointF,

    Qt Code:
    1. QPointF QGraphicsItem::mapToScene()
    To copy to clipboard, switch view to plain text mode 

    How can I combine these arguments in an elegant way without havin to create an intermediate local variable ?

    Tanks for your hints in advance!

    Olli

  2. #2
    Join Date
    Dec 2006
    Posts
    849
    Thanks
    6
    Thanked 163 Times in 151 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: Function arguments - how to get a reference

    those function more often use const references...
    for const references you can just pass the (temporary) value:


    Qt Code:
    1. QRectF rect=...;
    2. QPointF pt=...;
    3. QGraphicsItem *item=...;
    4. rect.contains(item->mapToScene(pt));
    To copy to clipboard, switch view to plain text mode 

    You can't do that for non-const references, though.

    HTH

  3. #3
    Join Date
    Jan 2006
    Location
    Sta. Eugènia de Berga (Vic - Barcelona - Spain)
    Posts
    869
    Thanks
    70
    Thanked 59 Times in 57 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Function arguments - how to get a reference

    Hi,

    You don't need to use an internal variable:

    "mapToScene" returns a QPointF and you can pass it to "contains" method directly.
    QRect::contains(QPonitF & pt) tells you that it gets the QPonitF "pt" as a reference, so it not will copy the object. It's like a pointer to the object
    Òscar Llarch i Galán

Similar Threads

  1. QPSQL problem
    By LoneWolf in forum Installation and Deployment
    Replies: 60
    Last Post: 4th November 2009, 15:22
  2. QPSQL driver in windows
    By brevleq in forum Installation and Deployment
    Replies: 31
    Last Post: 14th December 2007, 13:57
  3. how to add static library into qmake
    By Namrata in forum Qt Tools
    Replies: 1
    Last Post: 20th November 2007, 18:33
  4. error undefined reference ...............
    By amit_pansuria in forum Qt Programming
    Replies: 2
    Last Post: 8th June 2007, 15:28
  5. how to correctly compile threads support?
    By srhlefty in forum Installation and Deployment
    Replies: 9
    Last Post: 25th June 2006, 20:15

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.