August 5, 2012

Vikuseth Vikuseth
Ant Farmer
76 posts

Cell contains disappears after adding delegate in QTableview

Page  
1

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 ?

20 replies

August 5, 2012

DerManu DerManu
Robot Herder
458 posts

That the delegate replaces the usual content is intended and usually required.

Judging from the other thread you’ve opened about this, it might be more sensible for you to subclass a view and a model and add an interface for “help buttons next to the content”. That would probably be the cleanest method. So the model provides an additional flag that indicates that this modelindex shall have a help button, and it should provide an additional QString/QUrl to the respective help content. The view can then query those properties and display a help button accordingly.

August 6, 2012

Vikuseth Vikuseth
Ant Farmer
76 posts

Can you please give a small example on this ?

August 6, 2012

Sam Sam
Area 51 Engineer
628 posts

@Vikuseth

It will be easy for us if you too could share some code regarding the above problem. As per your previous posts here [qt-project.org] and here [qt-project.org] i believe you are still dealing with the same problem .

Thanks.

August 6, 2012

Vikuseth Vikuseth
Ant Farmer
76 posts

No i have done with all that thing .. but now what i am facing is , whenever i am using delegate , the cell contains disappears ..

here is the sample code
delegate.h

  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. };

delegate.cpp

  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.      QStyleOptionButton button;
  13.      QRect r = option.rect;//getting the rect of the cell
  14.      int x,y,w,h;
  15.      x = r.left() + r.width() - 30;//the X coordinate
  16.      y = r.top();//the Y coordinate
  17.      w = 30;//button width
  18.      h = 30;//button height
  19.      button.rect = QRect(x,y,w,h);
  20.      button.text = "=^.^=";
  21.      button.state = QStyle::State_Enabled;
  22.  
  23.      QApplication::style()->drawControl( QStyle::CE_PushButton, &button, painter);
  24.  }
  25.  
  26.  bool MyDelegate::editorEvent(QEvent *event, QAbstractItemModel *model, const QStyleOptionViewItem &option, const QModelIndex &index)
  27.  {
  28.      if( event->type() == QEvent::MouseButtonRelease )
  29.      {
  30.          QMouseEvent * e = (QMouseEvent *)event;
  31.          int clickX = e->x();
  32.          int clickY = e->y();
  33.  
  34.          QRect r = option.rect;//getting the rect of the cell
  35.          int x,y,w,h;
  36.          x = r.left() + r.width() - 30;//the X coordinate
  37.          y = r.top();//the Y coordinate
  38.          w = 30;//button width
  39.          h = 30;//button height
  40.  
  41.          if( clickX > x && clickX < x + w )
  42.              if( clickY > y && clickY < y + h )
  43.              {
  44.                  QDialog * d = new QDialog();
  45.                  d->setGeometry(0,0,100,100);
  46.                  d->show();
  47.              }
  48.      }
  49.  }

main.cpp
  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. }

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 .

August 6, 2012

Sam Sam
Area 51 Engineer
628 posts

The above code generates a tableView like :-

tableView with buttons

So as per my understanding the requirement here is that both the pushButton and the tableView cell (should be in edit mode) be functional. If that is the case the you can implement the following functions as well

  1. QWidget *createEditor(QWidget *parent, const QStyleOptionViewItem &option, const QModelIndex &index) const;
  2. void setEditorData(QWidget *editor, const QModelIndex &index) const;
  3. void setModelData(QWidget *editor, QAbstractItemModel *model, const QModelIndex &index) const;
  4. void updateEditorGeometry(QWidget *editor, const QStyleOptionViewItem &option, const QModelIndex &index) const;

Where in the edit mode on single click you can display a lineEdit whose size is cellWidth – buttonWidth along with the contents.

This is just an approach.

August 6, 2012

Vikuseth Vikuseth
Ant Farmer
76 posts

ya i need both pushbutton and tableviewcell contains …

August 6, 2012

Vikuseth Vikuseth
Ant Farmer
76 posts

actually i dont know how to implement the above thing in my code , so that it will work fine ..
can u plz give some idea ?

August 6, 2012

Sam Sam
Area 51 Engineer
628 posts

Just for the knowledge you can have a look on SpinBox Delegate Example [doc.qt.nokia.com] also check out this video tutorial [voidrealms.com] that explains more about using delegates.

August 6, 2012

Andre Andre
Area 51 Engineer
6076 posts

The cell contents are rendered by the delegate. If your delegate doesn’t render the contents, then no contents will be shown.

It is probably easiest to subclass an existing delegate (like you already do), but instead of doing all the painting yourself, first let the base class delegate do it’s rendering, and then just overdraw your own help button on top of that.

 Signature 

Looking for Qt developers to join our team @ i-Optics: https://qt-project.org/forums/viewthread/25393/

August 7, 2012

Vikuseth Vikuseth
Ant Farmer
76 posts

@andre:Thankx a lot .

August 7, 2012

Vikuseth Vikuseth
Ant Farmer
76 posts

@andre : can u please say , in the above code at which place i have to implement your concept ..

August 7, 2012

Andre Andre
Area 51 Engineer
6076 posts

in the paint method of your delegate.

 Signature 

Looking for Qt developers to join our team @ i-Optics: https://qt-project.org/forums/viewthread/25393/

August 8, 2012

Vikuseth Vikuseth
Ant Farmer
76 posts

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.

It works after adding QItemDelegate::paint(painter, option, index); at the beginning of my paint() method .

August 8, 2012

Vikuseth Vikuseth
Ant Farmer
76 posts

I need one more help .. i want to set one icon and stylesheet on the top of that button ..

I am using

  1. button.icon= QIcon(QString::fromUtf8("Resources/Restore.png"));
  2. button.iconSize = QSize( 12, 12 );

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 ..

August 8, 2012

Andre Andre
Area 51 Engineer
6076 posts

You don’t have a button, so you can’t expect it to work like one. You’re just rendering a basic button yourself. Still, I would have expected setting the icon like you are doing would have worked. Can you post a screenshot of what happens now, and all the relevant code again (integrated) so we don’t have to go back and forth between your posts?

StyleSheets are not set on QStyleOptions. They are on the widget instead, but you don’t have one…

 Signature 

Looking for Qt developers to join our team @ i-Optics: https://qt-project.org/forums/viewthread/25393/

Page  
1

  ‹‹ OpenGL and GPU acceleration      Dialogs - Windows 7 Look&Feel ››

You must log in to post a reply. Not a member yet? Register here!