Results 1 to 5 of 5

Thread: Custom Delegate

  1. #1
    Join Date
    May 2013
    Location
    Georgia,Tbilisi
    Posts
    32
    Thanks
    7
    Qt products
    Qt5
    Platforms
    Unix/X11 Windows

    Default Custom Delegate

    Hello all again!
    I am stucked and need little help. I have QTableView and Costum Delegate for tableView's second column.
    I think best way to express my problem is to show screenshots and source code:
    mainwindow.cpp
    Qt Code:
    1. #include "mainwindow.h"
    2. #include "ui_mainwindow.h"
    3. #include "cdelegate.h"
    4.  
    5. MainWindow::MainWindow(QWidget *parent) :
    6. QMainWindow(parent),
    7. ui(new Ui::MainWindow)
    8. {
    9. ui->setupUi(this);
    10. model = new QStandardItemModel;
    11. for(int r = 0; r < 10; ++r){
    12. QStandardItem *item = new QStandardItem("Item " + QString::number(r));
    13. model->setItem(r,0,item);
    14. model->setItem(r,1,new QStandardItem("Qt is cool"));
    15. }
    16.  
    17. ui->tableView->setModel(model);
    18. CDelegate *cdelegate = new CDelegate;
    19. ui->tableView->setItemDelegateForColumn(1,cdelegate);
    20. }
    21.  
    22. MainWindow::~MainWindow()
    23. {
    24. delete ui;
    25. }
    To copy to clipboard, switch view to plain text mode 

    cdelegate.h
    Qt Code:
    1. #ifndef CDELEGATE_H
    2. #define CDELEGATE_H
    3. #include <QAbstractItemDelegate>
    4.  
    5. class CDelegate : public QAbstractItemDelegate
    6. {
    7. public:
    8. CDelegate();
    9.  
    10. void paint(QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index) const;
    11. QSize sizeHint(const QStyleOptionViewItem &option, const QModelIndex &index) const;
    12.  
    13.  
    14. //I think this method would help to my problem
    15. void setModelData(QWidget *editor, QAbstractItemModel *model, const QModelIndex &index) const;
    16. };
    17.  
    18. #endif // CDELEGATE_H
    To copy to clipboard, switch view to plain text mode 

    and cdelegate.cpp
    Qt Code:
    1. #include "cdelegate.h"
    2. #include <QPainter>
    3. #include <QTableView>
    4.  
    5. CDelegate::CDelegate()
    6. {
    7. }
    8. QSize CDelegate::sizeHint(const QStyleOptionViewItem &option, const QModelIndex &index) const
    9. {
    10. return QSize(45,30);
    11. }
    12. void CDelegate::paint(QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index) const
    13. {
    14. if(index.row()%2 == 0){
    15. painter->setBrush(QColor(239,239,239));
    16. painter->fillRect(option.rect,painter->brush());
    17. }
    18. }
    19. void CDelegate::setModelData(QWidget *editor, QAbstractItemModel *model, const QModelIndex &index) const
    20. {
    21. //...
    22. }
    To copy to clipboard, switch view to plain text mode 

    So,result is this:
    Capture.JPG

    This is a problem:
    I achieved what I wanted BUT the text: "Qt is cool" isn't appeared in the second column,for every row.
    I tried many solution but without any result.


    Have any idea?

  2. #2
    Join Date
    Oct 2012
    Posts
    132
    Thanks
    10
    Thanked 21 Times in 21 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows Android

    Default Re: Custom Delegate

    The reason why you're text isn't appeared is obvious: You are reimplementing QAbstractItemDelegate:aint. Your reimplementation doesn't paint any text (only a filled rectangle).

    To achieve a background color to be painted I suggest subclassing QAbstractItemModel or QAbstractTableModel. When implementing the data method, you can return the desired background color (as QBrush) for Qt::BackgroundRole.
    You can also subclass QStyledItemDelegate and overwrite the initStyleOption method to set the desired background color.

  3. The following user says thank you to Infinity for this useful post:

    Higgs (23rd February 2014)

  4. #3
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Custom Delegate

    Quote Originally Posted by Higgs View Post
    Have any idea?
    Have you tried calling the base class implementation of paint()?

  5. The following user says thank you to anda_skoa for this useful post:

    Higgs (23rd February 2014)

  6. #4
    Join Date
    May 2013
    Location
    Georgia,Tbilisi
    Posts
    32
    Thanks
    7
    Qt products
    Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Custom Delegate

    Quote Originally Posted by anda_skoa View Post
    Have you tried calling the base class implementation of paint()?
    Quote Originally Posted by Infinity View Post
    The reason why you're text isn't appeared is obvious: You are reimplementing QAbstractItemDelegate:aint. Your reimplementation doesn't paint any text (only a filled rectangle).

    To achieve a background color to be painted I suggest subclassing QAbstractItemModel or QAbstractTableModel. When implementing the data method, you can return the desired background color (as QBrush) for Qt::BackgroundRole.
    You can also subclass QStyledItemDelegate and overwrite the initStyleOption method to set the desired background color.
    Thank you both. I did subclass QStyleItemDelegate and overwrite paint method,then I called base class implementation of paint in overwritten method. that works.

    That is a code:
    Qt Code:
    1. #include "cdelegate.h"
    2. #include <QPainter>
    3. #include <QTableView>
    4. CDelegate::CDelegate()
    5. {
    6. }
    7. void CDelegate::paint(QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index) const
    8. {
    9. if(index.row()%2 == 0){
    10. painter->setBrush(QColor(239,239,239));
    11. painter->fillRect(option.rect,painter->brush());
    12. QStyledItemDelegate::paint(painter,option,index);
    13. }else
    14. QStyledItemDelegate::paint(painter,option,index);
    15. }
    To copy to clipboard, switch view to plain text mode 
    Capture.JPG

    When I Subclass QAbstractItemDelegate and then call base class implementation of paint,compiler gives me an error



    And last question in this thread,In documentation I read that QItemDelegate and QStyledItem Delegates is default for all Item view. But in reallity which one is default delegate for all item view?

  7. #5
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Custom Delegate

    QStyledItemDelegate. It has taken over the job of being the default delegate, i.e. it is newer than QItemDelegate.

    The Qt documentation says this: "Note that QStyledItemDelegate has taken over the job of drawing Qt's item views. We recommend the use of QStyledItemDelegate when creating new delegates."

    Cheers,
    _

Similar Threads

  1. qt and custom delegate
    By giugio in forum Qt Programming
    Replies: 0
    Last Post: 11th November 2012, 16:05
  2. how to repaint the custom delegate
    By yuyue in forum Qt Programming
    Replies: 6
    Last Post: 17th August 2012, 19:53
  3. Custom Model? Custom View? Custom Delegate?
    By Doug Broadwell in forum Newbie
    Replies: 4
    Last Post: 11th February 2010, 20:23
  4. Replies: 0
    Last Post: 1st February 2010, 11:00
  5. Question about custom view (or custom delegate)
    By skuda in forum Qt Programming
    Replies: 1
    Last Post: 21st September 2009, 20:06

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.