Solved how to add children to the QTree Widget
-
And i have another doubt .
I am trying to see the data which i have inserted into my QList . I can see the size but i cannot see the data.Code:
QList<QString> m_Data;
for(int i=1;i<3;i++)
{
Data = QString("SubItem"+QString::number(i));
m_Data.append(Data.toLatin1().constData());
QString H = m_Data.at(i); //trying to see data
qDebug()<<"Data"<< m_Data.at(i);
}while running the application crashesdonno why
-
@ManiRon
Hi
what you mean u can see the size but not the data?qDebug()<<"Data"<< m_Data.at(i); <<< u print index from 1,2,3
but u call append so first call, there is data in zero
but u print 1
then next loop
you print from 2, but only data in 0,1
and so on.
it starts in zero. so adjust the for loop.for(int i=0;i<2;i++) { Data = QString("SubItem"+QString::number(i+1)); m_Data.append(Data.toLatin1().constData()); qDebug()<<"Data"<< m_Data.at(i); //trying to see data }
-
@mrjj said in how to add children to the QTree Widget:
but
if i give
int i = m_data.size();
and print the i value , I can see the data size that which i have inserted in the list.But if i try to see the data
QString H = m_data.at(i);
and print the H value using qDebug i data is not coming and the application crashes -
@ManiRon
Yes, you access to high index.
as u start with 1.
and list start with zero.
So that can crash. -
This post is deleted! -
@mrjj but my size is two. I have inserted two data and i am trying to see the second data then it should work right?
-
@ManiRon OK SIR IT WORKED
-
I have another doubt sir
-
@ManiRon
super :)
ask away :) -
How to use this ? for this only i am trying
-
@mrjj What is my doubt is i want to add N number of children to a toplevelitem and one child to each child ? How can i add N Number of childern first
-
@ManiRon
Its a list of QTreeWidgetItems * ( pointers)
So you must add them to a QList then give QList to tree.
Its just a function to add multiple items at once.// top one QTreeWidgetItem *treeItemROOT = new QTreeWidgetItem(ui->treeWidget); treeItem->setText(0, name); treeItem->setText(1, description); // first child QTreeWidgetItem *treeItem = new QTreeWidgetItem(); treeItem->setText(0, name); treeItem->setText(1, description); treeItemROOT ->addChild(treeItem); // add child to the root
-
@mrjj said in how to add children to the QTree Widget:
treeItem
thats what can u give any example code for that "Its a list of QTreeWidgetItems * ( pointers)
So you must add them to a QList then give QList to tree." -
@ManiRon
QList<QTreeWidgetItem> mylist;
mylist.append( new QTreeWidgetItem); // might want to set some text also
..
...
call addChildren with mylist.However, it wont help you build the tree, just other way of adding more than one item at a time.
-
My idea was to insert a list of data as a children to a toplevelitem
-
I gave like this QList<QTreeWidgetItem>m_Data;
m_Data.append(new QTreeWidgetItem);it throwed error saying
error: no matching function for call to 'QList<QTreeWidgetItem>::append(QTreeWidgetItem*)'
m_Data.append(new QTreeWidgetItem);
^ -
Hi
List must be of pointers.
QList<QTreeWidgetItem *>
(u missed the *) -
Now I gave like this sir,
QList<QTreeWidgetItem*>m_Data;
m_Data.append(new QTreeWidgetItem);QString Data = 0;
for(int i=1;i<3;i++)
{
Data = QString("SubItem"+QString::number(i));
m_Data.append(Data.toLatin1().constData());
item->addChildren(m_Data);
}it throws error
error: no matching function for call to 'QList<QTreeWidgetItem*>::append(const char*)'
m_Data.append(Data.toLatin1().constData());
^ -
hi
well u try to add strings to it.
m_Data.append(Data.toLatin1().constData());
and it wants a QTreeWidgetItem *I think you mean something like
for(int i=1;i<3;i++) { Data = QString("SubItem"+QString::number(i)); m_Data.append(Data.toLatin1().constData()); QTreeWidgetItem* item2=new QTreeWidgetItem(); // set text etc on item m_Data.append(item2); item->addChildren(m_Data); }
but why do it liek that since u new it there anyway so just add it directly ?
-
How are you trying to add it ?
aaa bbb ccc
or
aaa bbbb ccc ddd