PDA

View Full Version : How do I obtain mouse hover/popup functionality in a Qtableview?



georgep
18th March 2009, 08:40
Hi,

i am using a Qtableview to display data in table form, what classes do I need to use/subclass so that when I hover the mouse over an item I can display some image.
Basically i want to see the picture in decorationRole, only bigger, so hovering over the cell with the mouse would be perfect.

Does QWidget::mouseMoveEvent ( QMouseEvent * event ) help me in any way?

thank you.

wysota
18th March 2009, 09:04
QAbstractItemDelegate::editorEvent() and enabling WA_Hover attribute for the viewport of the view are the way to go.

georgep
18th March 2009, 21:27
should I use QHoverEvent ?

wysota
18th March 2009, 23:28
Yes, viewport :)

Yes, it will work like this - you have to react on enterEvent and leaveEvent probably.

georgep
18th March 2009, 23:34
haha, sorry for the rewrite, i just had figured it out and didnt see your reply:D

im using QEvent::Hoverenter but apparently its behaving like HoverMove...ie: if I move the mouse in the cell.. it still "fires"


thanks for the replys!

wysota
18th March 2009, 23:44
Use QEvent::Enter and QEvent::Leave.

georgep
19th March 2009, 00:22
I set the delegate on a column (view->setItemDelegateForColumn(1, new BoxDelegate(this) ); ) ...but somehow when entering the mouse over the column I dont get Enter of leave, i get MouseMove... dont understand why yet....



ps: someone should have told me to RTFM the "Events and Event Filters" article in Assistant.

wysota
19th March 2009, 00:38
Did you set the hover attribute on the view's viewport?

georgep
19th March 2009, 00:47
view->setMouseTracking(true);
view->viewport()->setAttribute(Qt::WA_Hover,true);



bool BoxDelegate::editorEvent ( QEvent * event, QAbstractItemModel * model, const QStyleOptionViewItem & option, const QModelIndex & index )
{

qDebug()<<index.row();
if ( event->type() == QEvent::HoverEnter ) { qDebug()<<"enter..."; } // doesnt appear. (it does on mousemove)

return 0;
}

wysota
19th March 2009, 09:18
If you want to enable mouse tracking, you have to do it on the viewport as well (then you'll receive enter and leave events). But it's not required. After taking a closer look on a working example I think you don't need to react on QEvent::Enter or QEvent::Leave. Just reimplement paint() and check whether the mouse is currently hovering over your item.

georgep
19th March 2009, 10:35
I tried view->viewport()->setMouseTracking(true); but I still dont get any enter or Leave :(
About reimplementing paint, is there any example somewhere that shows how to see if mouse is over something?

wysota
19th March 2009, 12:38
See QStyleOption::state.

georgep
20th March 2009, 22:41
In the paint implementation :


void BoxDelegate::paint(QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index) const

using qdebug()<<option.state; i am able to "see" a QStyle::State_MouseOver State, but in the



bool BoxDelegate::editorEvent ( QEvent * event, QAbstractItemModel * model, const QStyleOptionViewItem & option, const QModelIndex & index )

implementation, i dont.. why is this?

wysota
20th March 2009, 22:57
Probably because it's irrelevant there.

georgep
21st March 2009, 22:57
just one small unrelated question :)
Can i show images (.jpg,.gif) that are stored as BLOB in mysql directly in a qtableview ?

wysota
22nd March 2009, 00:27
Depends what you mean by "directly". You have to read them from the database and create pixmaps out of them. Once you do that, you can show the images in a widget.

georgep
16th April 2009, 19:27
I hope i get lucky and someone tells me where im screwing up so I dont have to open yet another thread...


I have a
model = new QSqlRelationalTableModel(this);
which gets data from mysql. I also have a myView subclass from QTableView.


If I :
model->setData(model->index( 1 ,1) , QPixmap(":/images/new.png"), Qt::DecorationRole);
I would expect that the decoration role icon would become visible in the tableview, but it doesnt :(( What do I have to do to get the icon to show ? Am i forced to subclass the model ? Isnt there any other way ?
I tryed to reimplment paint() but with no luck ....
(PS: the pixmap and resource exist/work)

wysota
17th April 2009, 07:56
SQL models don't support decorationRole out of the box. You have to subclass the model and introduce support for it by reimplementing data(), setData() and adding some container for storing the icons.

georgep
17th April 2009, 12:46
Awwww man, thanks! Ive been trying for hours :(
That phrase should be in the documentation :< Thx again!

georgep
19th April 2009, 22:42
I have subclassed QSqlTableModel in order to have a DecorationRole, no problem there.

Problem is that when editing (double click on a cell) with My_model the cell turns blank and Remains blank after the cell looses focus.. (of course i dont type anything).
With QSqlTableModel when i click a cell the contents remain there...
I get the same behavior with or without my own setData(..) .
Tried having return QSqlTableModel::setData(index,value,role); in my setdata() but no success....
Whats the trick? I am speculating that i am loosing the delegates used by qsqltablemodel....

georgep
20th April 2009, 11:44
in my_model::data I added a:

if(role== Qt::EditRole ) {
return QString(index.data(0).toString());
}
since I read on http://wiki.qtcentre.org/index.php?title=QAbstractItemModel
that Qt::EditRole returns a string, and now upon doubleclick the values "stay" in the cell.
But the story isnt over, I still dont understand how to use QSqlTableModel 's delegates on my_model too :(
(i have a datetime type in mysql which automagically has a spinbox editor in QSqlTableModel )

Someone should further edit the wiki article with examples of the functions :)

georgep
6th May 2009, 00:28
bump.
I still havent solved the last issue... :crying::crying::(

wysota
6th May 2009, 08:39
since I read on http://wiki.qtcentre.org/index.php?title=QAbstractItemModel
that Qt::EditRole returns a string,

It returns a variant. The default delegate interprets it as a string later on. So you can return an int from this role and it will be displayed as a textual representation of the integer.


But the story isnt over, I still dont understand how to use QSqlTableModel 's delegates on my_model too :(
(i have a datetime type in mysql which automagically has a spinbox editor in QSqlTableModel )

I don't understand the problem. What is it exactly that you don't know?


Someone should further edit the wiki article with examples of the functions :)

Hmm... why don't you do it?

georgep
6th May 2009, 11:37
It returns a variant. The default delegate interprets it as a string later on. So you can return an int from this role and it will be displayed as a textual representation of the integer.



I don't understand the problem. What is it exactly that you don't know?



it might be simple, but I dont understand how to get the spinbox delegate in the datetime column of the database (and not in others)[see pic]. I searched for the code in
src\sql\models\qsqltablemodel.cpp but couldnt manage to find anything...




Hmm... why don't you do it?


Because im very new to Qt, and not very experienced in cpp either so there is a significant chance i would write something stupid.

wysota
6th May 2009, 15:09
it might be simple, but I dont understand how to get the spinbox delegate in the datetime column of the database (and not in others)[see pic]. I searched for the code in
src\sql\models\qsqltablemodel.cpp but couldnt manage to find anything...
Because it's nothing related to the model. You need to provide your own delegate - subclass one of the existing classes and reimplement QAbstractItemDelegate::createEditor() to return a spinbox for the appropriate column or datatype.



Because im very new to Qt, and not very experienced in cpp either so there is a significant chance i would write something stupid.

So please restrain yourself from such statements. It's either writing examples at the wiki or answering questions here. The time and will of people willing to help is limited -- don't reduce their motivation even more, please.

georgep
6th May 2009, 15:39
So please restrain yourself from such statements. It's either writing examples at the wiki or answering questions here. The time and will of people willing to help is limited -- don't reduce their motivation even more, please.

Thanks for the suggestion, i will reimplement a delegate class.

What statements :confused: ??? I think you might have misunderstood me (my fault probably), I was merely speaking about miself, and i never commanded anyone anything...:confused:

cheers

xtiwix
7th January 2016, 20:26
I have subclassed QSqlTableModel in order to have a DecorationRole, no problem there.

Problem is that when editing (double click on a cell) with My_model the cell turns blank and Remains blank after the cell looses focus.. (of course i dont type anything).
With QSqlTableModel when i click a cell the contents remain there...
I get the same behavior with or without my own setData(..) .
Tried having return QSqlTableModel::setData(index,value,role); in my setdata() but no success....
Whats the trick? I am speculating that i am loosing the delegates used by qsqltablemodel....

How did you achieved this... I am trying to do the same but no luck. any hint or advice?