PDA

View Full Version : Cell contains disappears after adding delegate in QTableview



riarioriu3
5th August 2012, 19:57
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 ?

ChrisW67
5th August 2012, 22:45
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).

riarioriu3
6th August 2012, 05:42
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


class MyDelegate : public QItemDelegate
{
Q_OBJECT

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

ChrisW67
7th August 2012, 08:31
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.

riarioriu3
7th August 2012, 11:41
@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 ..

ChrisW67
8th August 2012, 00:11
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:


class MyDelegate : public QStyledItemDelegate { ... }


void MyDelegate::paint(QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index) const
{
// Do the default drawing, you might want to adjust option.rect first
QStyledItemDelegate::paint(painter, option, index);

// Then draw my button on top
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);
}

riarioriu3
8th August 2012, 05:24
@ChrisW67 : Thanks a lot brother ..
it works ...

riarioriu3
8th August 2012, 15:22
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 ..

riarioriu3
9th August 2012, 08:14
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 .

}
}
}