Results 1 to 2 of 2

Thread: QSortFilterProxyModel tableview Combobox problem

  1. #1
    Join Date
    Jul 2015
    Posts
    9
    Thanks
    4
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Windows

    Default QSortFilterProxyModel tableview Combobox problem

    I have searched and I have not found any valid solutions to my problem with QSortFilterProxyModel. I am asking in the same line with this thread
    and another one here which where not answered. I need to filter the output on my tableview but also be able to edit the columns however, when QSortFilterProxyModel is called, I loose focus of the comboboxes. Is there a work around on this? Bellow is my code

    Qt Code:
    1. void clsList::strModel()
    2. {
    3. strmodel = new QSqlRelationalTableModel(this);
    4. strmodel->setEditStrategy(QSqlTableModel::OnRowChange);
    5. strmodel->setTable("setCls_str");
    6. strmodel->setFilter("setCls_str.actv1=1");
    7.  
    8. int clInd = strmodel->fieldIndex("classCode");
    9. int strInd = strmodel->fieldIndex("streams");
    10. int clTInd = strmodel->fieldIndex("clTeach");
    11.  
    12. strmodel->setRelation(clInd, QSqlRelation("tblClass", "clID", "clCode"));
    13. strmodel->setRelation(strInd, QSqlRelation("tblStreams", "strID", "strName"));
    14. strmodel->setRelation(clTInd, QSqlRelation("allTeachers", "trID", "trName"));
    15.  
    16. strmodel->setHeaderData(strInd, Qt::Horizontal, tr("Stream"));
    17. strmodel->setHeaderData(clInd, Qt::Horizontal, tr("Class"));
    18. strmodel->setHeaderData(clTInd, Qt::Horizontal, tr("Teacher"));
    19.  
    20. strmodel->database().transaction();
    21. if(strmodel->submitAll())
    22. {
    23. strmodel->database().commit();
    24. }
    25. else
    26. {
    27. strmodel->database().rollback();
    28. }
    29.  
    30. if (!strmodel->select()) {
    31. showError(strmodel->lastError());
    32. return;}
    33.  
    34. strProxy = new SortFilterProxyModel(this);
    35. strProxy->setSourceModel(strmodel);
    36. strProxy->setFilterCaseSensitivity(Qt::CaseInsensitive);
    37.  
    38. ui->strmv->setModel(strProxy); // with this line all comboboxes disappear on my tableview
    39. // ui->strmv->setModel(strModel); // This line works well....with comboboxes visible on the grid
    40. //but I need to filter data according to some parameters
    41. ui->strmv->setItemDelegate(new QSqlRelationalDelegate(ui->strmv));
    42. ui->strmv->setColumnWidth(clInd,50);
    43. ui->strmv->setColumnWidth(strInd,100);
    44. ui->strmv->setColumnWidth(clTInd,100);
    45. }
    To copy to clipboard, switch view to plain text mode 

  2. #2
    Join Date
    Jul 2015
    Posts
    9
    Thanks
    4
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Windows

    Default Re: QSortFilterProxyModel tableview Combobox problem

    I am coming to a conclusion that Sqlite Views do not work with the default QSqlRelationalDelegate. When a view is used, editing is locked even when the right flags have been "raised". This could be due to the fact that Sqlite views are ReadOnly, conversely, with MS SQL Server, views work exactly the same way like tables. To whoever is facing the same challenge, you need to create a custom delegate to handle editing otherwise use the table itself. Unfortunately sometimes you may need to some calculated field (i.e where you need to add 'first name' and 'last name'). Below is my delegate that I used to handle editing on my table:

    Qt Code:
    1. #ifndef DELSTRSETTABLE
    2. #define DELSTRSETTABLE
    3. #include <QtWidgets>
    4. #include <QtSql>
    5.  
    6. class delStrSetTable : public QSqlRelationalDelegate
    7. {
    8. Q_OBJECT
    9. public:
    10. delStrSetTable(QObject *parent=0):QSqlRelationalDelegate(parent){
    11. QString strg("SELECT trID, trName FROM allTch WHERE trNo>0");
    12. tch = new QSqlQueryModel();
    13. tch->setQuery(strg);
    14. allt = tch->rowCount();
    15.  
    16. QString str2("SELECT strID, strName FROM tblstreams WHERE strNo>0");
    17. str = new QSqlQueryModel();
    18. str->setQuery(str2);
    19. alls = str->rowCount();
    20.  
    21. font2.setFamily(QStringLiteral("Arial Narrow"));
    22. font2.setPointSize(10);
    23. }
    24. virtual QWidget *createEditor(QWidget *parent, const QStyleOptionViewItem &option,
    25. const QModelIndex &index) const
    26. {
    27. // ComboBox ony in column 4
    28. if(index.column() == 3) {
    29.  
    30. // Create the combobox on stream column and populate it
    31. QComboBox *sbCb = new QComboBox(parent);
    32. sbCb->setModel(str);
    33. sbCb->setModelColumn(1);
    34. sbCb->setFont(font2);
    35. sbCb->setFrame(false);
    36. return sbCb;
    37. // Create the combobox on teacher column and populate it
    38. } else if (index.column() == 4) {
    39. QComboBox *tchCb = new QComboBox(parent);
    40. tchCb->setModel(tch);
    41. tchCb->setModelColumn(1);
    42. tchCb->setFont(font2);
    43. tchCb->setFrame(false);
    44. return tchCb;
    45.  
    46. }
    47. else
    48. return QSqlRelationalDelegate::createEditor(parent, option, index);
    49. }
    50. void setEditorData(QWidget *editor, const QModelIndex &index) const
    51. {
    52. if(index.column()==3)
    53. { QComboBox *combobox = qobject_cast<QComboBox*>(editor);
    54. Q_ASSERT(combobox);
    55.  
    56. for(int x = 0;x<alls;++x)
    57. {
    58. QModelIndex ID = str->index(x,0);
    59. QModelIndex name = str->index(x,1);
    60. QString fulname = QString(name.data(Qt::DisplayRole).toString());
    61. int id = ID.data().toInt();
    62.  
    63. if(index.data().toInt() == id){
    64. int cbIndex = combobox->findText(fulname);
    65. if(cbIndex >= 0)
    66. combobox->setCurrentIndex(cbIndex);
    67. }
    68. }
    69. }
    70. else if(index.column()==4)
    71. { QComboBox *combobox = qobject_cast<QComboBox*>(editor);
    72. Q_ASSERT(combobox);
    73.  
    74. for(int x = 0;x<allt;++x)
    75. {
    76. QModelIndex ID = tch->index(x,0);
    77. QModelIndex name = tch->index(x,1);
    78. QString fulname = QString(name.data(Qt::DisplayRole).toString());
    79. int id = ID.data().toInt();
    80.  
    81. if(index.data().toInt() == id){
    82. int cbIndex = combobox->findText(fulname);
    83. if(cbIndex >= 0)
    84. combobox->setCurrentIndex(cbIndex); }
    85. }
    86.  
    87. } else {
    88. QSqlRelationalDelegate::setEditorData(editor, index);
    89. }
    90. }
    91. void setModelData(QWidget *editor, QAbstractItemModel *model, const QModelIndex &index) const
    92. {
    93. if(index.column()==3)
    94. { QComboBox *combbox = qobject_cast<QComboBox*>(editor);
    95. Q_ASSERT(combbox);
    96.  
    97. for(int x = 0;x<alls;++x)
    98. {
    99. QModelIndex ID = str->index(x,0);
    100. QModelIndex name = str->index(x,1);
    101. QString fulname = QString(name.data().toString());
    102. int id = ID.data().toInt();
    103.  
    104. if(combbox->currentText() == fulname)
    105. model->setData(index,id);
    106. }
    107. }
    108. else if(index.column()==4)
    109. {
    110. QComboBox *combobx = qobject_cast<QComboBox*>(editor);
    111. Q_ASSERT(combobx);
    112.  
    113. for(int x = 0;x<allt;++x)
    114. {
    115. QModelIndex ID = tch->index(x,0);
    116. QModelIndex name = tch->index(x,1);
    117. QString fulname = QString(name.data().toString());
    118. int id = ID.data(Qt::EditRole).toInt();
    119.  
    120. if(combobx->currentText() == fulname)
    121. model->setData(index,id);
    122. }
    123.  
    124. } else
    125. QSqlRelationalDelegate::setModelData(editor, model, index);
    126. }
    127. private:
    128. QSqlQueryModel *tch,*str;
    129. int allt,alls;
    130. QFont font2;
    131. };
    132. #endif // DELSTRSETTABLE
    To copy to clipboard, switch view to plain text mode 

Similar Threads

  1. Replies: 1
    Last Post: 27th September 2012, 22:07
  2. Problem with QSortFilterProxyModel
    By pcheng in forum Newbie
    Replies: 1
    Last Post: 18th June 2012, 16:39
  3. How to get text for Tableview to Combobox ?
    By hohoanganh205 in forum Newbie
    Replies: 1
    Last Post: 26th December 2011, 10:54
  4. Replies: 0
    Last Post: 31st October 2010, 21:55
  5. Replies: 1
    Last Post: 7th February 2010, 08:01

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.