Results 1 to 3 of 3

Thread: function()->function() or function().function() notation

  1. #1
    Join Date
    Sep 2010
    Posts
    1
    Thanks
    1

    Default function()->function() or function().function() notation

    While learning qt through the book : C++ GUI Programming with qt4, I've encounted this notation quite a few times : function()->function() or function().function().

    Here is a common example from : http://doc.qt.nokia.com/4.6/qmainwindow.html (Creating Menus):
    Qt Code:
    1. void MainWindow::createMenus()
    2. {
    3. [B]fileMenu = menuBar()->addMenu(tr("&File"));[/B]
    4. fileMenu->addAction(newAct);
    5. fileMenu->addAction(openAct);
    6. fileMenu->addAction(saveAct);
    To copy to clipboard, switch view to plain text mode 

    Usually, in C++, I encounter Classes containing attributes and methods, but when I look at this notation, I see a function containing a function.
    I could guess that in this example the menubar() function returns a pointer to a QMenuBar which contains a addMenu() function returning a QMenu, but where is the QMenuBar object coming from? It does not seem to be declared as an attribute of QMainWindow.

    How am I supposed interpret this notation ?
    Does this notation have a name, if yes what is it ?

  2. #2
    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: function()->function() or function().function() notation

    You almost have it :-)

    Quote Originally Posted by pierre58 View Post
    I could guess that in this example the menubar() function returns a pointer to a QMenuBar
    To a QMenuBar object to be more specific.

    but where is the QMenuBar object coming from? It does not seem to be declared as an attribute of QMainWindow.
    Not all the class child objects or functions are publicly available. The menu bar for example is a private object inside the main window. You use access functions to reach them.

    The menuBar() function returns the menu bar object.

    Qt Code:
    1. class MyClass
    2. {
    3. public:
    4. MyObject *getMyObject() const;
    5.  
    6. private:
    7. MyObject *theObject;
    8. };
    9.  
    10. MyObject *MyClass::getMyObject() const
    11. {
    12. return theObject;
    13. }
    14.  
    15. ...
    16.  
    17. MyClass *myclass = new MyClass(...);
    18. MyObject *theObjectFromMyClass = myclass->getMyObject();
    To copy to clipboard, switch view to plain text mode 

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

    pierre58 (13th September 2010)

  4. #3
    Join Date
    Aug 2010
    Location
    Ottawa Canada
    Posts
    3
    Qt products
    Qt3 Qt4 Qt/Embedded Qt Jambi PyQt3 PyQt4
    Platforms
    MacOS X Unix/X11 Windows Symbian S60 Maemo/MeeGo

    Default Re: function()->function() or function().function() notation

    Pierre,
    funct1()->funct2()
    The way this works is similar to this from your example:

    Qt Code:
    1. QMenuBar* temp;
    2. QMenu* fileMenu;
    3. temp = menuBar();
    4. fileMenu = temp->addMenu(tr("&File"));
    To copy to clipboard, switch view to plain text mode 

    By calling implementing your code this way you can ensure that menuBar() returns a valid value.
    Using funct1()->funct2() is unsafe because funct1() could return null causing a crash.
    Hope this helps.
    Kyle Arseneault
    You I Labs

Similar Threads

  1. Replies: 3
    Last Post: 25th May 2010, 10:46
  2. XML Dom parsing function
    By FoleyX90 in forum Qt Programming
    Replies: 3
    Last Post: 13th May 2010, 16:25
  3. Replies: 0
    Last Post: 10th March 2010, 09:13
  4. keypressed function?
    By anafor2004 in forum Newbie
    Replies: 2
    Last Post: 17th January 2008, 13:37
  5. Zip function for Qt4
    By devilj in forum Newbie
    Replies: 7
    Last Post: 15th July 2007, 15:37

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.