PDA

View Full Version : add child item in list view



johan07
2nd January 2013, 22:19
Hi,
i create list view , i set model to the list like that


listView->setModel( model );

i createthe model like that


void MainWindow::createModel()
{
model = new QStandardItemModel( 0,0,this );
model->setHorizontalHeaderLabels( QStringList() << "liste" );
createModelItems();
}

void MainWindow::createModelItems()
{
QStringList choiceList;
QStandardItem *pItem;

choiceList << "conf1" << "conf2" << "conf3";
foreach( QString str, choiceList ){ // parent
pItem = new QStandardItem( str );
pItem->setTristate( true );
model->appendRow( pItem );
}

}


and i added new item like that with signal slot
the sig,al is


void MainWindow::add(vector3 point)
{
QStandardItem *item=new QStandardItem(QString ("Marker %0").arg(nb_mark));
nb_mark++;
emit itemAded(item);
}

the slot is


void MainWindow::itemAded( QStandardItem *pItem )
{

model->appendRow( pItem );
}

but the problem is that it add to me new item and that's not what i want
i want to add the new item as child of an item of the "chois list"

i know that i must add this infiormation in the signal , but how i do that ? have you an idea ?

Santosh Reddy
3rd January 2013, 07:04
and i added new item like that with signal slot
the sig,al is
Do not implement/define signal method. (Qt will define it). Move the code in the signal into slot and remove the signal definition.


i want to add the new item as child of an item of the "chois list"
List items cannot have child item, you should use Tree (TreeView) if you want to add new items as children.

johan07
3rd January 2013, 09:34
hi,

Do not implement/define signal method. (Qt will define it). Move the code in the signal into slot and remove the signal definition.
no i must add it , because it's important and there is another things i must made it in the signal.

List items cannot have child item, you should use Tree (TreeView) if you want to add new items as children.
if i use treeview i will change à lot the code ?
another thing i see in some exemple thy used listview with child

void MainWindow::createModelItems()
{
QStringList choiceList, optionList;

choiceList << "Alpha" << "Beta" << "Charlie";
optionList << "Opt1" << "Opt2" << "Opt3";

QStandardItem *pItem, *cItem;

foreach( QString str, choiceList ){ // parent
pItem = new QStandardItem( str );
pItem->setCheckable( true );
pItem->setTristate( true );
foreach( QString str, optionList ){ // children
cItem = new QStandardItem( str );
cItem->setCheckable( true );
pItem->appendRow( cItem );
}

model->appendRow( pItem );
}
}

Santosh Reddy
3rd January 2013, 09:54
hi,


Do not implement/define signal method. (Qt will define it). Move the code in the signal into slot and remove the signal definition.
no i must add it , because it's important and there is another things i must made it in the signal.

The signal still exists, I mean only the signal declaration should be there (class.h), and signal definition/implementation (class.cpp) should not exists. Qt framework creates the implementation for you.




List items cannot have child item, you should use Tree (TreeView) if you want to add new items as children.
if i use treeview i will change à lot the code ?
another thing i see in some exemple thy used listview with child
The code snipet is using QStandardItemModel, this model has to set on a QTreeView.