Results 1 to 3 of 3

Thread: Delegate for QTableWidget

  1. #1
    Join Date
    Aug 2010
    Posts
    65
    Thanks
    5
    Qt products
    Qt4
    Platforms
    Windows

    Default Delegate for QTableWidget

    I've been trying to figure out how Delegates and whatnot work, but am having quite a bit of trouble. I'm fairly certain there's something wrong with my concepts, but after reading the Delegate Classes page and looking through the SpinBox Delegate example, I thought I'd figured out how things work, but clearly haven't.

    I'm trying to use delegates to control user input in a QTableWidget so that only numbers are allowed.

    The code is as follows.
    Qt Code:
    1. class NTableDelegate : public QStyledItemDelegate
    2. {
    3. Q_OBJECT
    4.  
    5. public:
    6. NTableDelegate(QObject* parent);
    7. QWidget *createEditor(QWidget *parent, const QStyleOptionViewItem &option, const QModelIndex &index) const;
    8. void setEditorData(QWidget *editor, const QModelIndex &index) const;
    9. void setModelData(QWidget *editor, QAbstractItemModel *model, const QModelIndex &index) const;
    10. void updateEditorGeometry(QWidget *editor, const QStyleOptionViewItem &option, const QModelIndex &index) const;
    11. };
    12.  
    13. NTableDelegate::NTableDelegate(QObject *parent) : QStyledItemDelegate(parent)
    14. {
    15. }
    16.  
    17. QWidget* NTableDelegate::createEditor(QWidget* parent,const QStyleOptionViewItem &option,const QModelIndex &index) const
    18. {
    19. QLineEdit* editor = new QLineEdit(parent);
    20. QDoubleValidator* val = new QDoubleValidator(editor);
    21. val->setBottom(0);
    22. val->setNotation(QDoubleValidator::StandardNotation);
    23. editor->setValidator(val);
    24. return editor;
    25. }
    26. void NTableDelegate::setEditorData(QWidget *editor, const QModelIndex &index) const
    27. {
    28. double value = index.model()->data(index,Qt::EditRole).toDouble();
    29. QLineEdit* line = static_cast<QLineEdit*>(editor);
    30. line->setText(QString().setNum(value));
    31. }
    32. void NTableDelegate::setModelData(QWidget* editor,QAbstractItemModel* model,const QModelIndex &index) const
    33. {
    34. QLineEdit* line = static_cast<QLineEdit*>(editor);
    35. QString value = line->text();
    36. model->setData(index,value);
    37. }
    38. void NTableDelegate::updateEditorGeometry(QWidget *editor, const QStyleOptionViewItem &option, const QModelIndex &index) const
    39. {
    40. editor->setGeometry(option.rect);
    41. }
    To copy to clipboard, switch view to plain text mode 

    I basically got the SpinBox Delegate example and tried altering it for my needs, but to no avail. The cells are not visible and not modifiable. If I click the headers, they're selected, which tells me the table is populated, but simply locked away.

    However, should I simply comment out the QTableWidget::setItemDelegate() line, all goes back to normal, as shown in the images below. So, my question remains: what have I done wrong?

    default.JPGcustom.JPG

  2. #2
    Join Date
    Jul 2010
    Posts
    41
    Thanks
    6
    Thanked 4 Times in 3 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: Delegate for QTableWidget

    I tested your code and it works!
    EDIT: I set delegate like this,
    Qt Code:
    1. //table is a QTableWidget* instance
    2. table->setItemDelegate(new NTableDelegate(table));
    To copy to clipboard, switch view to plain text mode 
    Last edited by srazi; 6th December 2010 at 18:37.

  3. #3
    Join Date
    Aug 2010
    Posts
    65
    Thanks
    5
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Delegate for QTableWidget

    As soon as I saw that EDIT, I slapped myself. I created the delegate on the stack with a modeless dialog.

    A question though: I've been told in another thread to use a delegate to control the positioning of the QTableWidgetItem's checkbox. As you can see in the "default" image above, the second column is entirely made up of checkable items. These items are in fact disabled for text-editing, but I can't figure out how to center the checkbox and remove the text-box next to it. It's a safe bet that I need to use the delegate's paint() function, but I haven't a clue how to do it.
    Last edited by Wasabi; 7th December 2010 at 13:26.

Similar Threads

  1. Need help with my delegate
    By Guilo in forum Qt Programming
    Replies: 6
    Last Post: 1st July 2010, 12:15
  2. Is delegate necessary here?
    By scythe in forum Qt Programming
    Replies: 1
    Last Post: 22nd June 2010, 19:59
  3. QTableWidget Delegate
    By aekilic in forum Qt Programming
    Replies: 12
    Last Post: 15th May 2009, 09:21
  4. Again QTableWidget and QComboBox delegate
    By Aki-Matti in forum Qt Programming
    Replies: 2
    Last Post: 4th March 2008, 13:40
  5. Delegate but when
    By uygar in forum Qt Programming
    Replies: 1
    Last Post: 12th October 2007, 20:28

Tags for this Thread

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.