Results 1 to 4 of 4

Thread: QTablewidget and QItemDelegate?

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Join Date
    May 2006
    Posts
    108
    Thanks
    35
    Thanked 2 Times in 1 Post
    Qt products
    Qt4
    Platforms
    Windows

    Default QTablewidget and QItemDelegate?

    Hi,

    i try to display widgets within a QTablewidget:

    1. How can i show the first item in the QComboBox if editing finished and not the model value?
    2. How can i set at row 0 a different QDoubbleValidator dependent the QComboBox entry?

    Thanks in advanced

    Qt Code:
    1. TableCellDelegate::TableCellDelegate(QObject *parent) : QItemDelegate(parent)
    2. {
    3. }
    4.  
    5. void TableCellDelegate::paint(QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index) const
    6. {
    7. int idx = index.column();
    8. if(idx == 1)
    9. {
    10. // Display first QComboBox item ???
    11. }
    12. else if(idx == 2)
    13. {
    14. int secs = index.model()->data(index, Qt::DisplayRole).toInt();
    15. QString text = QString("%1:%2")
    16. .arg(secs / 60, 2, 10, QChar('0'))
    17. .arg(secs % 60, 2, 10, QChar('0'));
    18.  
    19. QStyleOptionViewItem myOption = option;
    20. myOption.displayAlignment = Qt::AlignRight | Qt::AlignVCenter;
    21.  
    22. drawDisplay(painter, myOption, myOption.rect, text);
    23. drawFocus(painter, myOption, myOption.rect);
    24. }
    25. else
    26. {
    27. QItemDelegate::paint(painter, option, index);
    28. }
    29. }
    30.  
    31. QWidget *TableCellDelegate::createEditor(QWidget *parent, const QStyleOptionViewItem &option, const QModelIndex &index)
    32.  
    33. const
    34. {
    35. int idx = index.column();
    36. if(idx == 1)
    37. {
    38. QComboBox *combo = new QComboBox( parent );
    39. combo->addItem("Item 1");
    40. combo->addItem("Item 2");
    41. combo->addItem("Item 3");
    42. combo->setCurrentIndex(0);
    43. connect(combo, SIGNAL( activated( int ) ), this, SLOT(commitAndCloseEditor()));
    44. return combo;
    45. }
    46. else if(idx == 2)
    47. {
    48. QTimeEdit *timeEdit = new QTimeEdit(parent);
    49. timeEdit->setDisplayFormat("mm:ss");
    50. connect(timeEdit, SIGNAL(editingFinished()), this, SLOT(commitAndCloseEditor()));
    51. return timeEdit;
    52. }
    53. else
    54. {
    55. return QItemDelegate::createEditor(parent, option, index);
    56. }
    57. }
    58.  
    59. void TableCellDelegate::setEditorData(QWidget *editor, const QModelIndex &index) const
    60. {
    61. int idx = index.column();
    62. if(idx == 1)
    63. {
    64. QComboBox *combo = qobject_cast<QComboBox *>(editor);
    65. combo->setCurrentIndex(index.model()->data(index).toInt());
    66. }
    67. else if(idx == 2)
    68. {
    69. int secs = index.model()->data(index, Qt::DisplayRole).toInt();
    70. QTimeEdit *timeEdit = qobject_cast<QTimeEdit *>(editor);
    71. timeEdit->setTime(QTime(0, secs / 60, secs % 60));
    72. }
    73. else
    74. {
    75. QItemDelegate::setEditorData(editor, index);
    76. }
    77. }
    78.  
    79. void TableCellDelegate::setModelData(QWidget *editor, QAbstractItemModel *model, const QModelIndex &index) const
    80. {
    81. int idx = index.column();
    82. if(idx == 1)
    83. {
    84. QComboBox *combo = qobject_cast<QComboBox *>(editor);
    85. model->setData(index, combo->currentIndex());
    86. }
    87. else if(idx == 2)
    88. {
    89. QTimeEdit *timeEdit = qobject_cast<QTimeEdit *>(editor);
    90. QTime time = timeEdit->time();
    91. int secs = (time.minute() * 60) + time.second();
    92. model->setData(index, secs);
    93. }
    94. else
    95. {
    96. QItemDelegate::setModelData(editor, model, index);
    97. }
    98. }
    99.  
    100. void TableCellDelegate::commitAndCloseEditor()
    101. {
    102. emit commitData(qobject_cast<QWidget *>(sender()));
    103. }
    To copy to clipboard, switch view to plain text mode 

  2. #2
    Join Date
    May 2006
    Posts
    108
    Thanks
    35
    Thanked 2 Times in 1 Post
    Qt products
    Qt4
    Platforms
    Windows

    Question Re: QTablewidget and QItemDelegate?

    Can this be done or is it so hard because i dont get a tip?

  3. #3
    Join Date
    May 2006
    Posts
    108
    Thanks
    35
    Thanked 2 Times in 1 Post
    Qt products
    Qt4
    Platforms
    Windows

    Question Re: QTablewidget and QItemDelegate?

    1. How can i show the first item in the QComboBox if editing finished and not the model value?
    I know how can i do that, but how can i figure out the currentText from the ComboBox?

    Qt Code:
    1. box->palette = option.palette;
    2. box->rect = option.rect;
    3. box->state = QStyle::State_Active | QStyle::State_Enabled;
    4. box->currentText = "Text"; //comboBox->currentText();
    5. box->frame = false;
    6. painter->save();
    7. QApplication::style()->drawControl(QStyle::CE_ComboBoxLabel, box, painter);
    8. painter->restore();
    To copy to clipboard, switch view to plain text mode 

    I have try this, but thats not right:
    Qt Code:
    1. qDebug() << "DisplayRole: " << index.data(Qt::DisplayRole).toString();
    2. qDebug() << "EditRole: " << index.data(Qt::EditRole).toString();
    To copy to clipboard, switch view to plain text mode 

  4. #4
    Join Date
    May 2006
    Posts
    108
    Thanks
    35
    Thanked 2 Times in 1 Post
    Qt products
    Qt4
    Platforms
    Windows

    Question Re: QTablewidget and QItemDelegate?

    Hi,

    now i have try to set the Qt :: DisplayRole within setModelData() like this:
    Qt Code:
    1. model->setData(index, combo->currentText(), Qt::DisplayRole);
    To copy to clipboard, switch view to plain text mode 

    and write within paint() like this:
    Qt Code:
    1. box->palette = option.palette;
    2. box->rect = option.rect;
    3. box->state = QStyle::State_Active | QStyle::State_Enabled;
    4. box->currentText = index.data(Qt::DisplayRole).toString();
    5. box->frame = false;
    6. painter->save();
    7. QApplication::style()->drawControl(QStyle::CE_ComboBoxLabel, box, painter);
    8. painter->restore();
    To copy to clipboard, switch view to plain text mode 

    But i cant display the text. Must i do anything else in createEditor() or setEditorData()?

    Please help me! Thanks!

Similar Threads

  1. QItemDelegate use in QTableWidget
    By arpspatel in forum Qt Programming
    Replies: 2
    Last Post: 27th October 2009, 22:18
  2. QItemDelegate Align
    By aekilic in forum Qt Programming
    Replies: 43
    Last Post: 1st April 2009, 07:52
  3. QItemDelegate
    By Alex_123 in forum Qt Programming
    Replies: 1
    Last Post: 12th October 2008, 11:40
  4. Using most of QItemDelegate
    By joshuajcarson in forum Qt Programming
    Replies: 4
    Last Post: 6th October 2008, 16:02
  5. QItemDelegate
    By cyberboy in forum Qt Programming
    Replies: 10
    Last Post: 27th June 2008, 17:41

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.