Results 1 to 4 of 4

Thread: setIndexWidget and proxy interaction

  1. #1
    Join Date
    Mar 2006
    Posts
    3
    Thanks
    1
    Qt products
    Qt4
    Platforms
    MacOS X

    Default 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.

    Qt Code:
    1. int main(int argc, char**argv)
    2. {
    3. //------ test proxy and setIndexWidget
    4. QApplication application(argc,argv);
    5. //-- model
    6. QModelIndex parent;
    7. for (int i=0; i<4; ++i) {
    8. parent = testModel.index(0,0,parent);
    9. testModel.insertRows(0,1,parent);
    10. testModel.insertColumns(0,1,parent);
    11. QModelIndex index = testModel.index(0,0,parent);
    12. testModel.setData(index,i);
    13. }
    14.  
    15. //-- tree and filtered tree
    16. QTreeView tree;
    17. tree.setModel(&testModel);
    18. tree.setWindowTitle("direct");
    19. tree.show();
    20.  
    21. filter.setSourceModel(&testModel);
    22.  
    23. QTreeView filteredTree;
    24. filteredTree.setModel(&filter);
    25. filteredTree.setWindowTitle("filtered");
    26. filteredTree.show();
    27.  
    28. //-- add buttons
    29. QModelIndex modelIndex = testModel.index(0,0,QModelIndex());
    30. QToolButton * button1 = new QToolButton(&tree);
    31. button1->setText("button1");
    32. tree.setIndexWidget(modelIndex,button1);
    33.  
    34. QModelIndex filterIndex = filter.mapFromSource(modelIndex);
    35. QToolButton * button2 = new QToolButton(&filteredTree);
    36. button2->setText("button2");
    37. filteredTree.setIndexWidget(filter.mapFromSource(modelIndex),button2);
    38.  
    39. //-- change model
    40. testModel.insertRows(0,1,QModelIndex());
    41. testModel.setData(testModel.index(0,0,QModelIndex()),42);
    42.  
    43. //-- add buttons
    44. modelIndex = testModel.index(0,0,QModelIndex());
    45. QToolButton * button3 =new QToolButton(&tree);
    46. button3->setText("button3");
    47. tree.setIndexWidget(modelIndex,button3);
    48.  
    49. filterIndex = filter.mapFromSource(modelIndex);
    50. QToolButton * button4 = new QToolButton(&filteredTree);
    51. button4->setText("button4");
    52. filteredTree.setIndexWidget(filterIndex,button4);
    53.  
    54. //-- add again button, normal ?
    55. //-- doesn't work with the same (button2) ? is it destroyed when the model changes ?
    56. //modelIndex = testModel.index(1,0,QModelIndex());
    57. //filterIndex = filter.mapFromSource(modelIndex);
    58. //QToolButton * button5 = new QToolButton(&filteredTree);
    59. //button5->setText("button5");
    60. //filteredTree.setIndexWidget(filter.mapFromSource(modelIndex),button5);
    61.  
    62. //-- run
    63. application.exec();
    64. return 0;
    65. }
    To copy to clipboard, switch view to plain text mode 

  2. #2
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: setIndexWidget and proxy interaction

    What do you need those buttons for?

  3. #3
    Join Date
    Mar 2006
    Posts
    3
    Thanks
    1
    Qt products
    Qt4
    Platforms
    MacOS X

    Default 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

  4. #4
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default 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.

  5. The following user says thank you to wysota for this useful post:

    Derf (25th March 2006)

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.