Cell contains disappears after adding delegate in QTableview
-
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.
-
OK , thanks Andre ..
I am using the paint() method to modify the QHeaderViews .