PDA

View Full Version : Problem With findchild()



gunturrohith
23rd June 2016, 13:03
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


class menuprocess : public QWidget
{
Q_OBJECT
public:
menuprocess();
~menuprocess();
private:
QLabel *io_one;
private slots:
int tester();
};

The CPP File


menuprocess::menuprocess()
{
io_one = new QLabel(this);
testButton = new QPushButton(this);
testButton->setText("Test");
testButton->setGeometry(250,130,40,30);
}

int menuprocess::tester()
{
QStringList ios;
ios << "RFID" << "MagSwipe" << "RTC" << "IFD" <<"PM";
QStringList obs;
obs << "io_one" << "io_two" << "io_three" << "io_four";
QLabel *label = parentWidget()->findChild<QLabel *>(obs.at(j));I have used both the Lines this one and the next one by commenting one among this
QLabel *label = this->findChild<QLabel *>(obs.at(j));
qDebug()<<"The OBJECT NAME is"<<label->objectName();//at this line i am getting errorThe program has unexpectedly finished
label->setText(ios.at(i));//And here too the same error if i comment the QDebug Line
}


Is it possible todo so in this manner if yes

Please do need ful

Thanks in advance

Regards,
Rohith.G

Ginsengelf
23rd June 2016, 14:30
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

anda_skoa
23rd June 2016, 14:49
The program has unexpectedly finished

Meaning it crashed.
You should have gotten a stack trace when running this in a debugger.



Here is my Code:

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



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,
_

gunturrohith
24th June 2016, 05:18
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

Ginsengelf
24th June 2016, 07:32
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

anda_skoa
24th June 2016, 10:38
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,
_