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 ?
-
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.
-
Can you please give a small example on this ?
-
@Vikuseth
It will be easy for us if you too could share some code regarding the above problem. As per your previous posts "here":http://qt-project.org/forums/viewthread/19287/ and "here":http://qt-project.org/forums/viewthread/19338/ i believe you are still dealing with the same problem .
Thanks.
-
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@class MyDelegate : public QItemDelegate
{
Q_OBJECTpublic:
MyDelegate(QObject *parent = 0);
void paint(QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index) const;
bool editorEvent(QEvent *event, QAbstractItemModel *model, const QStyleOptionViewItem &option, const QModelIndex &index);
};@delegate.cpp
@ #include <QtGui>
#include "delegate.h"MyDelegate::MyDelegate(QObject *parent)
: QItemDelegate(parent)
{
}void MyDelegate::paint(QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index) const
{
QStyleOptionButton button;
QRect r = option.rect;//getting the rect of the cell
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.rect = QRect(x,y,w,h);
button.text = "=^.^=";
button.state = QStyle::State_Enabled;QApplication::style()->drawControl( QStyle::CE_PushButton, &button, painter);
}
bool MyDelegate::editorEvent(QEvent *event, QAbstractItemModel *model, const QStyleOptionViewItem &option, const QModelIndex &index)
{
if( event->type() == QEvent::MouseButtonRelease )
{
QMouseEvent * e = (QMouseEvent *)event;
int clickX = e->x();
int clickY = e->y();QRect r = option.rect;//getting the rect of the cell 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 ) { QDialog * d = new QDialog(); d->setGeometry(0,0,100,100); d->show(); } }
}
@
main.cpp
@#include "delegate.h"int main(int argc, char *argv[])
{
QApplication app(argc, argv);QStandardItemModel model(4, 2); 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 :-
!http://img577.imageshack.us/img577/7562/capturemji.png(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
@QWidget *createEditor(QWidget *parent, const QStyleOptionViewItem &option, const QModelIndex &index) const;
void setEditorData(QWidget *editor, const QModelIndex &index) const;
void setModelData(QWidget *editor, QAbstractItemModel *model, 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.
-
ya i need both pushbutton and tableviewcell contains ...
-
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 ? -
Just for the knowledge you can have a look on "SpinBox Delegate Example":http://doc.qt.nokia.com/4.7-snapshot/itemviews-spinboxdelegate.html also check out this "video tutorial":http://www.voidrealms.com/viewtutorial.aspx?id=60 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.
-
@andre:Thankx a lot .
-
@andre : can u please say , in the above code at which place i have to implement your concept ..
-
in the paint method of your delegate.
-
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
@button.icon= QIcon(QString::fromUtf8("Resources/Restore.png"));
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 ..
-
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...
-
I got the solution ..
Old paint method :
@void MyDelegate::paint(QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index) const
{
QStyleOptionButton button;
QRect r = option.rect;//getting the rect of the cell
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.rect = QRect(x,y,w,h);
button.text = "=^.^=";
button.state = QStyle::State_Enabled;QApplication::style()->drawControl( QStyle::CE_PushButton, &button, painter);
}@
here is the updated paint() method .
@void MyDelegate::paint(QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index) const
{
QItemDelegate::paint(painter, option, index);
if(index.row()==8)//since i have to make it display only at (8,0) position .
{
if(index.column()==0)
{
QStyleOptionButton button;
QRect r = option.rect;//getting the rect of the cell
int x,y,w,h;
x = r.left() + r.width() - 20;//the X coordinate
y = r.top();//the Y coordinate
w = 15;//button width(based on the requirement)
h = 15;//button height(based on the requirement)
button.icon= QIcon(QString::fromUtf8("Resources/HelpIcon.png"));
button.iconSize = QSize(20,20);
button.rect = QRect(x,y,w,h);
button.text = "";//no text . since if text will be given then it will push the icon to left side based on the coordinates .
button.state = QStyle::State_Enabled;//QApplication::style()->drawControl( QStyle::CE_PushButton, &button, painter);
QApplication::style()->drawControl( QStyle::CE_PushButtonLabel, &button, painter);//To make the Button transparent .
}
}
}@ -
I'd not hard-code the position you want it on in the delegate.
-
Is it possible to implement delegate on QHeaderView ?
i have seen something like sectionPressed() in QHeaderView..
But i am not able to know how do i use this function ? -
[quote author="Vikuseth" date="1344571190"]Is it possible to implement delegate on QHeaderView ?
i have seen something like sectionPressed() in QHeaderView..
But i am not able to know how do i use this function ?[/quote]No, QHeaderViews ignore delegates. They still can be modified, just not using delegates.