PDA

View Full Version : Accessing children



codeman
14th December 2010, 18:12
Hello friends,

when I create a tabwidget say like this :



m_qtbw_myTab = new QTabWidget(this);

for (int i = 0; i < m_DetailInfos.size(); ++i)
{
QWidget* customWidget1 = new QWidget(m_qtbw_OrderTab) ;
QGridLayout* myLayout = new QGridLayout();
QLabel * qlbl_Nr = new QLabel(tr("Auftrag"),customWidget1);
qlbl_Nr->setObjectName("qlbl_Nr_"+m_DetailInfos.at(i).value(0).toString());
QLineEdit * qle_Nr = new QLineEdit(customWidget1);
qle_Nr->setObjectName("qle_Nr_"+m_DetailInfos.at(i).value(0).toString());
qle_Nr->setText(m_DetailInfos.at(i).value(0).toString());
qle_Nr->setEnabled(false);
myLayout->addWidget(qlbl_Nr,0,0,1,1,Qt::AlignLeft);
myLayout->addWidget(qle_Nr,0,1,1,1,Qt::AlignLeft);
customWidget1->setLayout(myLayout);

m_qtbw_myTab->addTab(customWidget1,m_DetailInfos.at(i).value(0). toString());
}


And I want access the widget in a special tab I do that with:


QWidget * myWidget = qobject_cast<QWidget *>(m_qtbw_myTab->widget(0));


But what about accessing the Lineedit in a Widget??

I see the children during debugging and try this:



QLineEdit * myedit = qobject_cast<QLineEdit *>(myWidget->childAt(1,0));//lineedit


but I get a crash.

Can I access the components on the widget without subclassing the Widget??

Thanx in advance.

squidge
14th December 2010, 19:14
Wouldn't it make more sense to store the value of the QLineEdit pointer and use that rather than using magic indexes which may change?

codeman
14th December 2010, 19:58
I don´t know if it make sense, cause it is the first situation for me to implement this thing.
What do you mean exactly with storing pointers?

QLineEdit& m_ptr_qle;
m_ptr_qle = &QLineEdit;

squidge
14th December 2010, 21:56
Well, you already have the pointer:



QLineEdit * qle_Nr = new QLineEdit(customWidget1);


So why not assign this somewhere rather than throwing it away?



m_DetailInfoUI[i].LineEdit = new QLineEdit(customWidget1);

codeman
15th December 2010, 13:25
Thank you very nuch for your suggestions, but what is this for a contruct :


m_DetailInfoUI[i].LineEdit

Perhaps this:


struct UIElements
{
QLineEdit* LineEdit
....
....
};

QVector <UIElements> m_DetailInfoUI;



Am I right??

squidge
15th December 2010, 14:28
That'll definitely work.

codeman
15th December 2010, 16:08
Thanx I will try