PDA

View Full Version : setIndexWidget and proxy interaction



Derf
25th March 2006, 17:44
Hello, apologies for my English, I speak French, C++, then English.

I begin with Qt, currently using multiple views of a tree model.
I tried to add widgets (buttons) in a filtered view but the buttons are disapearing when I add a row to the model.

In the doc, it is said to use a delegate for dynamic data. It seems to me that delegates are fit for custom paint or data editors. Am I wrong ? setIndexWidget looked quite adequate ...

Shall I use a delegate ? If yes, is there somewhere a delegate example for the equivalent of setIndexWidget? The paint method doesn't look appropriate, is it possible to paint a button in it ?
On the other side, shall I re-add the buttons in the rows each time I change the model ?
I tried to re-add a button, but it seams impossible to reuse a disappeared button. Is it destroyed ?

Here is an example code, thanks in advance you for your answers, Derf.



int main(int argc, char**argv)
{
//------ test proxy and setIndexWidget
QApplication application(argc,argv);
//-- model
QStandardItemModel testModel;
QModelIndex parent;
for (int i=0; i<4; ++i) {
parent = testModel.index(0,0,parent);
testModel.insertRows(0,1,parent);
testModel.insertColumns(0,1,parent);
QModelIndex index = testModel.index(0,0,parent);
testModel.setData(index,i);
}

//-- tree and filtered tree
QTreeView tree;
tree.setModel(&testModel);
tree.setWindowTitle("direct");
tree.show();

QSortFilterProxyModel filter;
filter.setSourceModel(&testModel);

QTreeView filteredTree;
filteredTree.setModel(&filter);
filteredTree.setWindowTitle("filtered");
filteredTree.show();

//-- add buttons
QModelIndex modelIndex = testModel.index(0,0,QModelIndex());
QToolButton * button1 = new QToolButton(&tree);
button1->setText("button1");
tree.setIndexWidget(modelIndex,button1);

QModelIndex filterIndex = filter.mapFromSource(modelIndex);
QToolButton * button2 = new QToolButton(&filteredTree);
button2->setText("button2");
filteredTree.setIndexWidget(filter.mapFromSource(m odelIndex),button2);

//-- change model
testModel.insertRows(0,1,QModelIndex());
testModel.setData(testModel.index(0,0,QModelIndex( )),42);

//-- add buttons
modelIndex = testModel.index(0,0,QModelIndex());
QToolButton * button3 =new QToolButton(&tree);
button3->setText("button3");
tree.setIndexWidget(modelIndex,button3);

filterIndex = filter.mapFromSource(modelIndex);
QToolButton * button4 = new QToolButton(&filteredTree);
button4->setText("button4");
filteredTree.setIndexWidget(filterIndex,button4);

//-- add again button, normal ?
//-- doesn't work with the same (button2) ? is it destroyed when the model changes ?
//modelIndex = testModel.index(1,0,QModelIndex());
//filterIndex = filter.mapFromSource(modelIndex);
//QToolButton * button5 = new QToolButton(&filteredTree);
//button5->setText("button5");
//filteredTree.setIndexWidget(filter.mapFromSource(m odelIndex),button5);

//-- run
application.exec();
return 0;
}

wysota
25th March 2006, 17:50
What do you need those buttons for?

Derf
25th March 2006, 17:57
The tree view in my application (not in the minimal code example) is an inspector for expressions and variables of an entity.
The goal is to permit expressions reevaluation. Some expressions have side effects (like method calls) or random operator.

My idea is to have a little "evaluate" button for each expression.

Derf

wysota
25th March 2006, 18:15
Using delegate's functionality could indeed help here. There are some threads on this forum which explain how to do it. Alternatively you could have a column which would be clickable (using the view's signals) and that would contain an image of a button (you can draw it using style's primitives). Or you could use checkbox capabilities of the model and provide one button which would evaluate all checked rows.