Results 1 to 2 of 2

Thread: editors returned from QItemDelegate do not show propperly

  1. #1
    Join Date
    Mar 2011
    Posts
    45
    Thanks
    4
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default editors returned from QItemDelegate do not show propperly

    hello folks,

    hope u can help:

    the editors returned from my QItemDelegate do not show propperly (e.g. i see just the string "true" rather than the QCheckBox that i returned, except for the case when i double-click my item to edit it, the right widget shows up.
    but i want it to be displayed all the time, not when i just editing my table-item.

    a little background:

    i have an application which uses a QTableView to display some bunch of data.
    This data is actually just a list of QRegExp and i want to use QTableView (rows) to be able to modify pattern, pattern-syntax and case-sensitivity (three columns) for each
    regex in my data entry.

    for this i implemented the following item delegate (hope the amount of following code wont kill somebody):

    My ItemDelegate:

    Qt Code:
    1. // //////////////////////////////////////////
    2. class FilterRecordItemDelegate : public QItemDelegate
    3. // //////////////////////////////////////////
    4. {
    5. //Q_OBJECT
    6. // //////////////////////////////
    7. public:
    8. // //////////////////////////////
    9. FilterRecordItemDelegate(QObject *parent=NULL);
    10.  
    11. QWidget *createEditor (QWidget *parent, const QStyleOptionViewItem &option, const QModelIndex &index) const;
    12. void setEditorData (QWidget *editor, const QModelIndex &index) const;
    13. void setModelData (QWidget *editor, QAbstractItemModel *model, const QModelIndex &index) const;
    14. };
    15.  
    16.  
    17. // //////////////////////////////////
    18. FilterRecordItemDelegate::FilterRecordItemDelegate(QObject *parent) : QItemDelegate(parent)
    19. // //////////////////////////////////
    20. {
    21. }
    22.  
    23. // //////////////////////////////////
    24. QWidget *FilterRecordItemDelegate::createEditor(QWidget *parent,
    25. const QStyleOptionViewItem &option,
    26. const QModelIndex &index) const
    27. // //////////////////////////////////
    28. {
    29. if (!index.isValid())
    30. return NULL;
    31.  
    32. QWidget *report = NULL;
    33. int col = index.column();
    34. switch(col)
    35. {
    36. case 1:
    37. {
    38. QComboBox *editor = new QComboBox(parent);
    39. editor->addItem( tr("Regular expression"), QRegExp::RegExp );
    40. editor->addItem( tr("Wildcard") , QRegExp::Wildcard );
    41. editor->addItem( tr("Fixed string") , QRegExp::FixedString );
    42. report = editor;
    43. break;
    44. }
    45. case 2:
    46. {
    47. QCheckBox *editor = new QCheckBox(parent);
    48. report = editor;
    49. break;
    50. }
    51. }
    52. return report;
    53. }
    54.  
    55. // //////////////////////////////////
    56. void FilterRecordItemDelegate::setEditorData(QWidget *editor,
    57. const QModelIndex &index) const
    58. // //////////////////////////////////
    59. {
    60. if (!index.isValid())
    61. return;
    62.  
    63. int col = index.column();
    64. switch(col)
    65. {
    66. case 1:
    67. {
    68. int value = index.model()->data(index, Qt::EditRole).toInt();
    69. QComboBox *cmbBox = static_cast<QComboBox*>(editor);
    70. cmbBox->setCurrentIndex(value);
    71. break;
    72. }
    73. case 2:
    74. {
    75. int value = index.model()->data(index, Qt::EditRole).toBool();
    76. QCheckBox *chckBox = static_cast<QCheckBox*>(editor);
    77. chckBox->setChecked(value);
    78. break;
    79. }
    80.  
    81. }
    82. }
    83.  
    84. // //////////////////////////////////
    85. void FilterRecordItemDelegate::setModelData(QWidget *editor,
    86. const QModelIndex &index) const
    87. // //////////////////////////////////
    88. {
    89. if (!index.isValid())
    90. return;
    91.  
    92. int col = index.column();
    93. switch(col)
    94. {
    95. case 1:
    96. {
    97. QComboBox *cmbBox = static_cast<QComboBox*>(editor);
    98. int value = cmbBox->currentIndex();
    99. model->setData(index, value, Qt::EditRole);
    100. break;
    101. }
    102. case 2:
    103. {
    104. QCheckBox *chckBox = static_cast<QCheckBox*>(editor);
    105. bool value = chckBox->isChecked();
    106. model->setData(index, value, Qt::EditRole);
    107. break;
    108. }
    109. }
    110. }
    To copy to clipboard, switch view to plain text mode 

    what could be the reason that (for example) i do not see a checkbox but just the string "true" for an item for which my delegate returned a QCheckBox ???

    Thnx alot.

  2. #2
    Join Date
    Mar 2011
    Posts
    45
    Thanks
    4
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: editors returned from QItemDelegate do not show propperly

    ok, maybe my question was not clear:

    let me tell my problem in a different way:

    how do i put a (lets say) QComboBox or QCheckBox into a QTableView and view these Widgets also when no editing is done, means: i dont want the table to contain entries like "true" or "false", but show the QCheckBox.

    what am i doing wrong?
    please any hints or examples are much appreciated.

    thnx.


    Added after 1 40 minutes:


    some additional infos for my problem:

    i am using a custom model that manages a list of QRegExp. the table-view contains each of the QRegExp instances in rows and the three columns contain the properties
    pattern, patternsyntax and case-sensitivity of each QRegExp instance.

    my delegate shall display first columns as QStrings, second columns as QComboBox and third as QCheckBox.
    this works fine only for editing data (when double clicking items in table) but items are not displayed propperly (means: i see "true" or "false" entries rather than a QCheckBox).

    i think problem is with my custom models data:

    Qt Code:
    1. // //////////////////////////////////////////////
    2. QVariant FilterRecordModel::data(const QModelIndex &index,
    3. int role) const
    4. // //////////////////////////////////////////////
    5. {
    6. if (!index.isValid())
    7. return QVariant();
    8.  
    9. if ((index.row() >= (int)filterRecordLst.size()) ||
    10. (index.row() < 0) ||
    11. filterRecordLst.isEmpty() )
    12. return QVariant();
    13.  
    14. QVariant report;
    15. if ( role == Qt::DisplayRole || role == Qt::EditRole )
    16. {
    17. int row = index.row();
    18. int col = index.column();
    19. QRegExp rexp = filterRecordLst[row];
    20. switch(col)
    21. {
    22. case 0:
    23. {
    24. report = QVariant(rexp.pattern());
    25. break;
    26. }
    27. case 1:
    28. {
    29. int val = -1;
    30. switch( rexp.patternSyntax() )
    31. {
    32. case QRegExp::RegExp: {val=0;break;}
    33. case QRegExp::Wildcard: {val=1;break;}
    34. case QRegExp::FixedString: {val=2;break;}
    35. default:break;
    36. }
    37. report = QVariant(val);
    38. break;
    39. }
    40. case 2:
    41. {
    42. bool cs = (rexp.caseSensitivity()==Qt::CaseSensitive);
    43. report = QVariant(cs);
    44. break;
    45. }
    46. }
    47. }
    48. return report;
    49. }
    To copy to clipboard, switch view to plain text mode 
    Last edited by kerim; 6th May 2011 at 09:25.

Similar Threads

  1. Good use of the MVC pattern for 3d editors.
    By toglia3d in forum Qt Programming
    Replies: 2
    Last Post: 1st October 2010, 14:39
  2. UML Editors based on Qt
    By npclaudiu in forum General Programming
    Replies: 1
    Last Post: 25th March 2010, 22:08
  3. Replies: 0
    Last Post: 2nd March 2010, 18:06
  4. QTableWidget and multiple editors -- how to get them?
    By macias in forum Qt Programming
    Replies: 2
    Last Post: 25th June 2007, 17:49
  5. Closing all editors in a view?
    By smacchia in forum Qt Programming
    Replies: 2
    Last Post: 10th April 2007, 14:29

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.