Results 1 to 3 of 3

Thread: QItemDelegate with QLabel for QTableView

  1. #1
    Join Date
    May 2010
    Location
    Berlin
    Posts
    2
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Question QItemDelegate with QLabel for QTableView

    Hello!

    Currently I'm developing a program with accesses a MySQL-database and with this I encountered one little Problem:

    The program displays one table by using a QSqlQueryModel and a QTableView. There is one column which contains doubles. I want these doubles to be displayed via a QItemDelegate which contains a QLabel.

    Here is my code:

    Header
    Qt Code:
    1. class LabelDelegate : public QItemDelegate
    2. {
    3. Q_OBJECT
    4.  
    5. public:
    6. LabelDelegate(QObject* parent = 0);
    7.  
    8. QWidget* createEditor(QWidget *parent, const QStyleOptionViewItem& option, const QModelIndex& index) const;
    9. void setEditorData(QWidget* editor, const QModelIndex& index) const;
    10. };
    To copy to clipboard, switch view to plain text mode 

    Code
    Qt Code:
    1. QWidget* LabelDelegate::createEditor(QWidget* parent, const QStyleOptionViewItem&, const QModelIndex&) const
    2. {
    3. QLabel* label = new QLabel(parent);
    4.  
    5. return label;
    6. }
    7.  
    8. void LabelDelegate::setEditorData(QWidget* editor, const QModelIndex& index) const
    9. {
    10. double value = index.model()->data(index, Qt::EditRole).toInt();
    11.  
    12. QLabel* label = static_cast<QLabel*>(editor);
    13.  
    14. label->setText(QString::number(value) + " €");
    15. }
    To copy to clipboard, switch view to plain text mode 

    Now I do this:
    Qt Code:
    1. tableview->setItemDelegateForColumn(1, new LabelDelegate);
    To copy to clipboard, switch view to plain text mode 

    But the values are still displayed normally, I can't see the '€' Symbol.

    Does anybody have an idea how to fix this?

    Thanks!

  2. #2
    Join Date
    Sep 2008
    Location
    Poland
    Posts
    80
    Thanks
    4
    Thanked 5 Times in 4 Posts
    Qt products
    Qt4 Qt/Embedded
    Platforms
    Windows

    Default Re: QItemDelegate with QLabel for QTableView

    Yup.Reimplement the QItemDelegate:: paint() function,this one is responsible for drawing the content.
    Besides,QLineEdit is a default editor created for QTableView,so reimplementing setEditorData and createEditor is unnecesary.

  3. #3
    Join Date
    Jan 2008
    Location
    Poland
    Posts
    687
    Thanks
    4
    Thanked 140 Times in 132 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: QItemDelegate with QLabel for QTableView

    items are displayed used paint() method in delegate. Editor is used for editing data - and QLabel is not good for this as you can't input data to it! Look at the Star Delegate example in Qt docs to get know how to use paint() and editor methods in delegate. You can Also inherit from QSqlTableModel and add euro char in data() method or implement proxy model which will add euro char to model's data.
    I would like to be a "Guru"

    Useful hints (try them before asking):
    1. Use Qt Assistant
    2. Search the forum

    If you haven't found solution yet then create new topic with smart question.

Similar Threads

  1. Replies: 2
    Last Post: 6th September 2011, 11:27
  2. QTableView QItemDelegate validation
    By pippo42 in forum Qt Programming
    Replies: 3
    Last Post: 8th March 2010, 09:23
  3. Replies: 0
    Last Post: 2nd March 2010, 18:06
  4. Replies: 1
    Last Post: 29th September 2009, 19:44
  5. Replies: 1
    Last Post: 2nd August 2008, 15:46

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
  •  
Qt is a trademark of The Qt Company.