setIndexWidget and proxy interaction
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.
Code:
int main(int argc, char**argv)
{
//------ test proxy and setIndexWidget
//-- model
for (int i=0; i<4; ++i) {
parent = testModel.index(0,0,parent);
testModel.insertRows(0,1,parent);
testModel.insertColumns(0,1,parent);
testModel.setData(index,i);
}
//-- tree and filtered tree
tree.setModel(&testModel);
tree.setWindowTitle("direct");
tree.show();
filter.setSourceModel(&testModel);
filteredTree.setModel(&filter);
filteredTree.setWindowTitle("filtered");
filteredTree.show();
//-- add buttons
button1->setText("button1");
tree.setIndexWidget(modelIndex,button1);
QModelIndex filterIndex
= filter.
mapFromSource(modelIndex
);
button2->setText("button2");
filteredTree.setIndexWidget(filter.mapFromSource(modelIndex),button2);
//-- change model
testModel.
setData(testModel.
index(0,
0,
QModelIndex()),
42);
//-- add buttons
button3->setText("button3");
tree.setIndexWidget(modelIndex,button3);
filterIndex = filter.mapFromSource(modelIndex);
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(modelIndex),button5);
//-- run
application.exec();
return 0;
}
Re: setIndexWidget and proxy interaction
What do you need those buttons for?
Re: setIndexWidget and proxy interaction
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
Re: setIndexWidget and proxy interaction
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.