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
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.
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
- {
- Q_OBJECT
- public:
- bool editorEvent(QEvent *event, QAbstractItemModel *model, const QStyleOptionViewItem &option, const QModelIndex &index);
- };
delegate.cpp
- #include <QtGui>
- #include "delegate.h"
- {
- }
- void MyDelegate::paint(QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index) const
- {
- QStyleOptionButton button;
- int x,y,w,h;
- x = r.left() + r.width() - 30;//the X coordinate
- y = r.top();//the Y coordinate
- w = 30;//button width
- h = 30;//button height
- button.text = "=^.^=";
- }
- bool MyDelegate::editorEvent(QEvent *event, QAbstractItemModel *model, const QStyleOptionViewItem &option, const QModelIndex &index)
- {
- {
- int clickX = e->x();
- int clickY = e->y();
- int x,y,w,h;
- x = r.left() + r.width() - 30;//the X coordinate
- y = r.top();//the Y coordinate
- w = 30;//button width
- h = 30;//button height
- if( clickX > x && clickX < x + w )
- if( clickY > y && clickY < y + h )
- {
- d->setGeometry(0,0,100,100);
- d->show();
- }
- }
- }
main.cpp
- #include "delegate.h"
- int main(int argc, char *argv[])
- {
- QTableView tableView;
- tableView.setModel(&model);
- MyDelegate delegate;
- tableView.setItemDelegate(&delegate);
- tableView.horizontalHeader()->setStretchLastSection(true);
- tableView.show();
- return app.exec();
- }
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 .
The above code generates a tableView like :-

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
- QWidget *createEditor(QWidget *parent, const QStyleOptionViewItem &option, const QModelIndex &index) const;
- 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.
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.
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.
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 .
I need one more help .. i want to set one icon and stylesheet on the top of that button ..
I am using
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 ..
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…
You must log in to post a reply. Not a member yet? Register here!




