Unsolved How to determine in MainWindow's mouseReleaseEvent() whether mouse is over a specific widget
-
Hey,
the problem is, that the enterEvent and the leaveEvent, I implemented in my widget I am talking about (a QGraphicsView in my case) only get executed, when the mouse is not pressed. And the widget->underMouse(), I tried to use in the mainWindow in the mouseReleaseEvent func also returns false, because the mouse is (/was) pressed.
To talk in code:void MainWindow::mouseReleaseEvent(QMouseEvent *event){ if(arrayOfQGraphicsViews[ui->Graph_TabWidget->currentIndex()]->underMouse()){ //returns false qDebug() << "placing..."; } }
and
void MainWindow::mouseReleaseEvent(QMouseEvent *event){ if(arrayOfQGraphicsViews[ui->Graph_TabWidget->currentIndex()]->mouseOverMe){ //also returns false qDebug() << "placing..."; } }
void GraphWidget::enterEvent(QEvent *event){ qDebug() << "enter"; // not printed when the mouse is pressed mouseOverMe = true; }
So, how could I check whether the mouse is over my widget in the mouseReleaseEvent in MainWindow?
Thanks for answers! -
Hi
Enter/Leave events are not ment to be send if u are holding mouse down.
Some widgets (like buttons) call grabMouse so they are sure to get the Release
event so they can draw correctly.You can try
http://doc.qt.io/qt-5/qapplication.html#widgetAtBut can you tell why you need Enter event while holding mouse ?
That is normally only needed for drag&drop operations which Qt has system for. -
@mrjj
Thanks!
Uhm, yes I want to drag and drop something.
I have a widget with pushButtons and when I click on such a pushButton and drag the mouse over the QGraphicsScene, I want to add a QGraphicsItem to the scene.
Which system are you talking about? I didn't know, there is something like that, but I would completely appreciate it :D -
@Niagarer
Oh the buttons grab the mouse so the Release event go back to the button you clicked on.
You should consider using a list for the items that can be added as QPushButton will work
against you.http://doc.qt.io/qt-5/dnd.html
This one could be good base
http://doc.qt.io/qt-5/qtwidgets-draganddrop-puzzle-example.html
It also shows how you store your own data in the drop event so the
QGraphicsScene can know what to add.
Look at the PiecesList. You can easy adapt that to hold your "Drag items"
and reuse the code to allow it to be dragged. ( same code for all cases) -
@mrjj
Ok, thanks I will try to solve it this way.
Oh hell, looks complicated, this will take time, but I will update this post once Im finished. -
@mrjj
Well, I took a closer look to this (to the implementation).
And here comes the question I can't find anything about in the docs: How can I display custom widgets as items in the QListView but keeping all stuff like drag and drop usable but without using a delegate class (or is this even possible)? -
I used images. For each custom widget, i used the render() function to make it draw a
pixmap (thumbnail) i then used in the list.While you can use ListWidget and setItemWidget, it is very heavy and
listView + delegate is recommended.
If you only have few items and its on Desktop pc, then setItemWidget
works ok but for many items, delegate is the way to do it.
Alternativt, scrollarea can also be used for a list of widgets. but really depends
on how many items u have. -
@mrjj
Ok, so using a delegate class with the ListView would be the best way, if I understood correctly.
I unfortunately can't use jsut a thumbnail pixmap, the user should be able to interact with the widget and the widget should be able to display many things (but every item I add to the ListView would be from the same widget class, otherwise I could not use a ListView, I have read).
Would I still have these handeling features in a ListWidget like drag and drop in the ListView? If yes, is it more easy to implement this in a ListWidget or does it not really make a difference? -
@Niagarer
It will have drag & drop support but its ment for the normal items and if u have full blown
widgets instead, you will need to add support for those.
The delegate must be able to draw (in code) the looks of the real widget so
if its complex widgets, it will be tons of work.
But yes it could work.How many of these custom widgets are we talking about?
-
@mrjj
If I'm unlucky, one day it could go to a thousand.
So I should not use a ListWidget if I understood correctly, but a ListView will be tons of work...
:/
I thought, this would not be such a big problem >.< -
@Niagarer
Then both setItemWidget and ScrolLArea will potentially be very slow.
Depending on the hardware it must run on. The memory alone will be high.Are all those different ?
-
@mrjj
Well the widget objects will come from the same class. What I mean is that I display only widget objects from f.e. MyWidget-class but with different text shown, and a different icon and some more different icons I will add to the widgets in the MyWidget-class, but the class is just one. (Actually I wanted to make two different ones for a QTreeWidget, but I guess I sould avoid that, it seems to be hard...)
Yes, actually I wanted to make it runnable on very slow hardware -
@Niagarer
Then no doubt you should use a delegate.
Also since its only the data that changes, then it wont be so much work
as if many different widgets. -
Do you have a picture of what you want ?
-
@mrjj
Well actually yes, give me a minute, the engine has to start... -
@mrjj
In the end I would really like to have something like this:
With the List I want only to represent the content inside the green rect.
I think I will not need the tree structure, only these widgets with f.e. this f, because once this list gets very big, only the search function will be used anymore (but if it is completele a joke to implement it with a treeList after I get it work with the ListView, I would appreciate it). But I would like to add some more little icons like that at the right side of the widget. -
@Niagarer
So its like a text line with some icons ?What is the "Get BlackBoard thing ?"
-
@mrjj
Thats a tooltip, the mouse was over the Get Blackboard thing and on the left of the widget there is also shown a little pushButton (the little star) to mark it as favourite (because the mouse was over this component). -
Ok. i think a delegate will work super since its relatively simple structure.
We often refer to
http://doc.qt.io/qt-5/qtwidgets-itemviews-stardelegate-example.html
for delegate sample -
@mrjj
In the example, a QTableWidget is used. And so it has no model (no QAbstractItemModel, the struct is actually the model). Would all this also work with QListViews or QTableViews?