Results 1 to 6 of 6

Thread: Problem With findchild()

  1. #1
    Join Date
    Nov 2014
    Posts
    54
    Thanks
    5
    Qt products
    Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11

    Default Problem With findchild()

    Hi i am trying to use the findChlid(), and i was getting error after running it that is
    The program has unexpectedly finished

    Here is my Code:
    The Header File
    Qt Code:
    1. class menuprocess : public QWidget
    2. {
    3. Q_OBJECT
    4. public:
    5. menuprocess();
    6. ~menuprocess();
    7. private:
    8. QLabel *io_one;
    9. private slots:
    10. int tester();
    11. };
    To copy to clipboard, switch view to plain text mode 
    The CPP File
    Qt Code:
    1. menuprocess::menuprocess()
    2. {
    3. io_one = new QLabel(this);
    4. testButton = new QPushButton(this);
    5. testButton->setText("Test");
    6. testButton->setGeometry(250,130,40,30);
    7. }
    8.  
    9. int menuprocess::tester()
    10. {
    11. ios << "RFID" << "MagSwipe" << "RTC" << "IFD" <<"PM";
    12. obs << "io_one" << "io_two" << "io_three" << "io_four";
    13. QLabel *label = parentWidget()->findChild<QLabel *>(obs.at(j));[B]I have used both the Lines this one and the next one by commenting one among this[/B]
    14. QLabel *label = this->findChild<QLabel *>(obs.at(j));
    15. qDebug()<<"The OBJECT NAME is"<<label->objectName();//at this line i am getting error[B]The program has unexpectedly finished[/B]
    16. label->setText(ios.at(i));[B]//And here too the same error if i comment the QDebug Line[/B]
    17. }
    To copy to clipboard, switch view to plain text mode 

    Is it possible todo so in this manner if yes

    Please do need ful

    Thanks in advance

    Regards,
    Rohith.G

  2. #2
    Join Date
    Jul 2008
    Location
    Germany
    Posts
    451
    Thanks
    9
    Thanked 59 Times in 57 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Problem With findchild()

    Hi, findChild() uses the object name of a QObject, not the name of the variable where you store your pointer. So I guess in your case "label" is always NULL, and that's why the program crashes. Use setObjectName() when you create your label in the menuprocess-constructor.

    Ginsengelf

  3. #3
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,419
    Thanks
    37
    Thanked 1,546 Times in 1,496 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Problem With findchild()

    Quote Originally Posted by gunturrohith View Post
    The program has unexpectedly finished
    Meaning it crashed.
    You should have gotten a stack trace when running this in a debugger.

    Quote Originally Posted by gunturrohith View Post
    Here is my Code:
    Don't write that when the code is not actually your code.

    Quote Originally Posted by gunturrohith View Post
    Is it possible todo so in this manner if yes
    Yes, this is possible, as Ginsengelf already wrote.

    However, what do you need this for?
    Why not just store the widgets you need access to in a way to allows you to access them directly?

    Cheers,
    _

  4. #4
    Join Date
    Nov 2014
    Posts
    54
    Thanks
    5
    Qt products
    Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11

    Default Re: Problem With findchild()

    Don't write that when the code is not actually your code.
    Hi anda_soka,

    Thanks for replying and helping and apologies for asking explanation,may i know the reason behind for stating this sentence
    Don't write that when the code is not actually your code. I may not be a great programmer in Qt but all the lines i have written on my own by considering the Qt assistant as reference.

    Thanks in advance,
    Rohith.G

  5. #5
    Join Date
    Jul 2008
    Location
    Germany
    Posts
    451
    Thanks
    9
    Thanked 59 Times in 57 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Problem With findchild()

    Hi, anda_skoa probably meant that your code won't compile the way you posted it and this means that it is not your complete code. For example i and j are undeclared in menuprocess::tester()

    Ginsengelf
    Last edited by Ginsengelf; 24th June 2016 at 08:40. Reason: spelling corrections

  6. #6
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,419
    Thanks
    37
    Thanked 1,546 Times in 1,496 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Problem With findchild()

    Quote Originally Posted by Ginsengelf View Post
    Hi, anda_skoa probably meant that your code won't compile the way you posted it and this means that it is not your complete code.
    Exactly!

    Cheers,
    _

Similar Threads

  1. findChild(ObjectName) fails?
    By Ran in forum Qt Programming
    Replies: 5
    Last Post: 21st January 2008, 07:58

Tags for this Thread

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.