Results 1 to 6 of 6

Thread: findChild(ObjectName) fails?

  1. #1
    Join Date
    Jan 2008
    Posts
    14
    Thanks
    5

    Default findChild(ObjectName) fails?

    I have a main window which creates multi QWidgets during running. I want to pick up one of them by their Object Name. But when I use findChild(Object Name), it always returns 0. For example,
    at the creation of a widget, I used
    Qt Code:
    1. MyGui *m_MyGui;
    2. m_MyGui->setObjectName("Cam 1");
    To copy to clipboard, switch view to plain text mode 

    and I use the following to when I need to get the widget

    Qt Code:
    1. MyGui *tmp = findChild<MyGui *>("Cam 1");
    To copy to clipboard, switch view to plain text mode 

    Any help?

  2. #2
    Join Date
    Feb 2006
    Location
    Romania
    Posts
    2,744
    Thanks
    8
    Thanked 541 Times in 521 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: findChild(ObjectName) fails?

    From what class are you calling it? If m_MyGui isn't a child of that class, then it is normal for the function to fail.

  3. #3
    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: findChild(ObjectName) fails?

    Also make sure MyGui class has the Q_OBJECT macro.

  4. #4
    Join Date
    Jan 2008
    Posts
    14
    Thanks
    5

    Default Re: findChild(ObjectName) fails?

    Thanks. I get it. This MyGui is not a child of the caller class. Emmm. Need to look for another method of finding an object.

  5. #5
    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: findChild(ObjectName) fails?

    Store a pointer somewhere or use signals and slots.

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

    Ran (21st January 2008)

  7. #6
    Join Date
    Jan 2008
    Posts
    14
    Thanks
    5

    Default Re: findChild(ObjectName) fails?

    Thanks wysota! The method "signal and slot" does the job very well.

Similar Threads

  1. Release build fails to find some resource images
    By MrGarbage in forum Installation and Deployment
    Replies: 3
    Last Post: 8th December 2007, 17:04
  2. Qt 4.3 build fails Suse?linux
    By almost in forum Installation and Deployment
    Replies: 3
    Last Post: 19th July 2007, 22:23
  3. CreateCompatibleBitmap fails in QPixmap
    By JimBrown in forum Qt Programming
    Replies: 14
    Last Post: 29th May 2007, 16:24
  4. QtService fails to start
    By a550ee in forum Qt Programming
    Replies: 1
    Last Post: 9th November 2006, 14:34
  5. Qt 4.1.2 Fails to build on Linux
    By rohandhruva in forum Installation and Deployment
    Replies: 6
    Last Post: 16th April 2006, 22:44

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.