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 ?
Re: Cell contains disappears after adding delegate in QTableview
Quote:
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).
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
Code:
{
Q_OBJECT
public:
};
delegate.cpp
Code:
#include <QtGui>
#include "delegate.h"
MyDelegate
::MyDelegate(QObject *parent
) {
}
{
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;
}
{
if( event
->type
() == QEvent::MouseButtonRelease ) {
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 )
{
d->setGeometry(0,0,100,100);
d->show();
}
}
}
main.cpp
Code:
#include "delegate.h"
int main(int argc, char *argv[])
{
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 .
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.
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 ..
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:
Code:
class MyDelegate : public QStyledItemDelegate { ... }
{
// Do the default drawing, you might want to adjust option.rect first
QStyledItemDelegate::paint(painter, option, index);
// Then draw my button on top
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;
}
Re: Cell contains disappears after adding delegate in QTableview
@ChrisW67 : Thanks a lot brother ..
it works ...
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
Code:
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 ..
Re: Cell contains disappears after adding delegate in QTableview
I got the solution ..
Old paint method :
Code:
{
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;
}
here is the updated paint() method .
Code:
{
if(index.row()==8)//since i have to make it display only at (8,0) position .
{
if(index.column()==0)
{
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 .
}
}
}