Results 1 to 9 of 9

Thread: Cell contains disappears after adding delegate in QTableview

  1. #1
    Join Date
    May 2012
    Location
    India
    Posts
    51
    Thanks
    4
    Qt products
    Qt3 Qt4 Qt/Embedded
    Platforms
    Windows

    Default Cell contains disappears after adding delegate in QTableview

    I have created one table by using QTableview and QAbstractTableModel . In one of the cell i have added one help button (right corner of that cell ) using QItemdelegate .

    MyDelegate delegate;
    tableView.setItemDelegate(&delegate);

    when i am adding the delegate by using the above code ,delegate appears but the cell contains disappears .

    but when i am not using delegate the cell contains appears .I want to display both cell contains as well as the delegate in that particular cell .

    is anywhere i am doing wrong ?

  2. #2
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,729
    Thanks
    13
    Thanked 1,610 Times in 1,537 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Default Re: Cell contains disappears after adding delegate in QTableview

    is anywhere i am doing wrong ?
    Yes, you are expecting us to guess what your code does without having seen it. If the cell content is not being shown then it will be because your delegate code is not painting it (display mode) or placing it in the editor (edit mode).

  3. #3
    Join Date
    May 2012
    Location
    India
    Posts
    51
    Thanks
    4
    Qt products
    Qt3 Qt4 Qt/Embedded
    Platforms
    Windows

    Default Re: Cell contains disappears after adding delegate in QTableview

    Can you please give a small example on this ?


    Added after 46 minutes:


    what i am facing is , whenever i am using delegate , the cell contains disappears ..
    here is the sample code

    delegate.h

    Qt Code:
    1. class MyDelegate : public QItemDelegate
    2. {
    3. Q_OBJECT
    4.  
    5. public:
    6. MyDelegate(QObject *parent = 0);
    7. void paint(QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index) const;
    8. bool editorEvent(QEvent *event, QAbstractItemModel *model, const QStyleOptionViewItem &option, const QModelIndex &index);
    9. };
    To copy to clipboard, switch view to plain text mode 

    delegate.cpp
    Qt Code:
    1. #include <QtGui>
    2. #include "delegate.h"
    3.  
    4. MyDelegate::MyDelegate(QObject *parent)
    5. : QItemDelegate(parent)
    6. {
    7. }
    8.  
    9.  
    10. void MyDelegate::paint(QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index) const
    11. {
    12. QRect r = option.rect;//getting the rect of the cell
    13. int x,y,w,h;
    14. x = r.left() + r.width() - 30;//the X coordinate
    15. y = r.top();//the Y coordinate
    16. w = 30;//button width
    17. h = 30;//button height
    18. button.rect = QRect(x,y,w,h);
    19. button.text = "=^.^=";
    20. button.state = QStyle::State_Enabled;
    21.  
    22. QApplication::style()->drawControl( QStyle::CE_PushButton, &button, painter);
    23. }
    24.  
    25. bool MyDelegate::editorEvent(QEvent *event, QAbstractItemModel *model, const QStyleOptionViewItem &option, const QModelIndex &index)
    26. {
    27. if( event->type() == QEvent::MouseButtonRelease )
    28. {
    29. QMouseEvent * e = (QMouseEvent *)event;
    30. int clickX = e->x();
    31. int clickY = e->y();
    32.  
    33. QRect r = option.rect;//getting the rect of the cell
    34. int x,y,w,h;
    35. x = r.left() + r.width() - 30;//the X coordinate
    36. y = r.top();//the Y coordinate
    37. w = 30;//button width
    38. h = 30;//button height
    39.  
    40. if( clickX > x && clickX < x + w )
    41. if( clickY > y && clickY < y + h )
    42. {
    43. QDialog * d = new QDialog();
    44. d->setGeometry(0,0,100,100);
    45. d->show();
    46. }
    47. }
    48. }
    To copy to clipboard, switch view to plain text mode 

    main.cpp
    Qt Code:
    1. #include "delegate.h"
    2.  
    3. int main(int argc, char *argv[])
    4. {
    5. QApplication app(argc, argv);
    6.  
    7. QStandardItemModel model(4, 2);
    8. QTableView tableView;
    9. tableView.setModel(&model);
    10.  
    11. MyDelegate delegate;
    12. tableView.setItemDelegate(&delegate);
    13.  
    14. tableView.horizontalHeader()->setStretchLastSection(true);
    15. tableView.show();
    16. return app.exec();
    17. }
    To copy to clipboard, switch view to plain text mode 

    In the above code if i comment tableView.setItemDelegate(&delegate) , the cell contains appears but if i uncomment this one then the cell contains disappear .
    Last edited by riarioriu3; 6th August 2012 at 06:42.

  4. #4
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,729
    Thanks
    13
    Thanked 1,610 Times in 1,537 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Default Re: Cell contains disappears after adding delegate in QTableview

    Your paint routine only draws a push button... therefore that is all that gets painted. If you want the text you have to paint it yourself, or call the parent class function (perhaps with a reduced option.rect), and then paint the button.

    You should probably derive from QStyledItemDelegate also.
    Last edited by ChrisW67; 7th August 2012 at 09:39.

  5. #5
    Join Date
    May 2012
    Location
    India
    Posts
    51
    Thanks
    4
    Qt products
    Qt3 Qt4 Qt/Embedded
    Platforms
    Windows

    Default Re: Cell contains disappears after adding delegate in QTableview

    @chrisW67 :I am not getting what you are saying ..
    could you please give some hint in the code , referring whatever code i have mentioned above ..Because those are only the files in my small project .. i din't have that much idea on MVC .. so just for practicing i have implemented the above and stuck in the above mentioned problem ..

  6. #6
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,729
    Thanks
    13
    Thanked 1,610 Times in 1,537 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Default Re: Cell contains disappears after adding delegate in QTableview

    When the view wants to draw a cell it calls the delegate's paint() function with some information about how, what, and where to draw the contents of the cell. The default delegate just draws the Qt::DisplayRole text and selection state. If you replace the delegate then you completely replace the default behaviour: you can draw whatever you like. If you want the text then you need to arrange to draw it. You can do it yourself or, using standard C++ mechanisms, you can call the default drawing code first then draw over the top.

    Try something like this:
    Qt Code:
    1. class MyDelegate : public QStyledItemDelegate { ... }
    2.  
    3.  
    4. void MyDelegate::paint(QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index) const
    5. {
    6. // Do the default drawing, you might want to adjust option.rect first
    7. QStyledItemDelegate::paint(painter, option, index);
    8.  
    9. // Then draw my button on top
    10. QRect r = option.rect;//getting the rect of the cell
    11. int x,y,w,h;
    12. x = r.left() + r.width() - 30;//the X coordinate
    13. y = r.top();//the Y coordinate
    14. w = 30;//button width
    15. h = 30;//button height
    16. button.rect = QRect(x,y,w,h);
    17. button.text = "=^.^=";
    18. button.state = QStyle::State_Enabled;
    19.  
    20. QApplication::style()->drawControl( QStyle::CE_PushButton, &button, painter);
    21. }
    To copy to clipboard, switch view to plain text mode 

  7. #7
    Join Date
    May 2012
    Location
    India
    Posts
    51
    Thanks
    4
    Qt products
    Qt3 Qt4 Qt/Embedded
    Platforms
    Windows

    Default Re: Cell contains disappears after adding delegate in QTableview

    @ChrisW67 : Thanks a lot brother ..
    it works ...

  8. #8
    Join Date
    May 2012
    Location
    India
    Posts
    51
    Thanks
    4
    Qt products
    Qt3 Qt4 Qt/Embedded
    Platforms
    Windows

    Default Re: Cell contains disappears after adding delegate in QTableview

    I need one more help .. i want to set one icon and stylesheet on the top of that button ..
    I am using
    Qt Code:
    1. button.icon= QIcon(QString::fromUtf8("Resources/Restore.png"));
    2. button.iconSize = QSize( 12, 12 );
    To copy to clipboard, switch view to plain text mode 
    but here the icon is set left to that button .. i have tried different coordinate but i am not able to find out what coordinate is ok for this ..
    i have searched in google too but not able to find out how to set the stylesheet for QStyleOptionButton .
    can u please give some idea on it ?
    Thankx a lot to all for your valuable reply ..

  9. #9
    Join Date
    May 2012
    Location
    India
    Posts
    51
    Thanks
    4
    Qt products
    Qt3 Qt4 Qt/Embedded
    Platforms
    Windows

    Default Re: Cell contains disappears after adding delegate in QTableview

    I got the solution ..

    Old paint method :

    Qt Code:
    1. void MyDelegate::paint(QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index) const
    2. {
    3. QRect r = option.rect;//getting the rect of the cell
    4. int x,y,w,h;
    5. x = r.left() + r.width() - 30;//the X coordinate
    6. y = r.top();//the Y coordinate
    7. w = 30;//button width
    8. h = 30;//button height
    9. button.rect = QRect(x,y,w,h);
    10. button.text = "=^.^=";
    11. button.state = QStyle::State_Enabled;
    12.  
    13. QApplication::style()->drawControl( QStyle::CE_PushButton, &button, painter);
    14. }
    To copy to clipboard, switch view to plain text mode 

    here is the updated paint() method .

    Qt Code:
    1. void MyDelegate::paint(QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index) const
    2. {
    3. QItemDelegate::paint(painter, option, index);
    4. if(index.row()==8)//since i have to make it display only at (8,0) position .
    5. {
    6. if(index.column()==0)
    7. {
    8. QRect r = option.rect;//getting the rect of the cell
    9. int x,y,w,h;
    10. x = r.left() + r.width() - 20;//the X coordinate
    11. y = r.top();//the Y coordinate
    12. w = 15;//button width(based on the requirement)
    13. h = 15;//button height(based on the requirement)
    14. button.icon= QIcon(QString::fromUtf8("Resources/HelpIcon.png"));
    15. button.iconSize = QSize(20,20);
    16. button.rect = QRect(x,y,w,h);
    17. button.text = "";//no text . since if text will be given then it will push the icon to left side based on the coordinates .
    18. button.state = QStyle::State_Enabled;
    19.  
    20. //QApplication::style()->drawControl( QStyle::CE_PushButton, &button, painter);
    21.  
    22. QApplication::style()->drawControl( QStyle::CE_PushButtonLabel, &button, painter);//To make the Button transparent .
    23.  
    24. }
    25. }
    26. }
    To copy to clipboard, switch view to plain text mode 

Similar Threads

  1. how to color a cell in a QTableView
    By JeanC in forum Qt Programming
    Replies: 13
    Last Post: 9th September 2015, 11:08
  2. Replies: 2
    Last Post: 28th October 2010, 10:26
  3. Replies: 3
    Last Post: 5th April 2010, 22:20
  4. Replies: 8
    Last Post: 23rd October 2009, 16:33
  5. QTableView, QSqlTableModel - data disappears
    By msh in forum Qt Programming
    Replies: 1
    Last Post: 15th November 2008, 12:50

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.