PyQt4.x Python 2.7 + QGraphicview with custom widget, move position with mouse
Hope this is the right place.
I have added a QGroupBox to QGraphicview via:
self.Scene = QGraphicsScene() ab = CustomGroupBox() # Subclassed self.Scene.addWidget(ab)
I then subclassed the QGroupBox with hoping that I can override
With something like this :
def mouseMoveEvent(self, event): super(CustomGroupBox, self).mouseMoveEvent(event) print event p = QtGui.QCursor.pos() pos_wid = self.mapToGlobal(self.pos()) self.move(p.x(),p.y())
So that I can move the widget around in the scene. However every time I click and start dragging the widget jumps by xx pixels right/left anywhere depending where the window is. I know is an issue of positioning I just don't know how to solve it :- (.
Any help would be amazing!
Hi and welcome to devnet,
Hope it helps
Mmmm I'm lost... where do you want me to set the flag up ?
I tried setting it up on my custom widget but that does nothing as far as I can tell.. When I disabled my mouseMoveEvent and just let ur flags I could not move icons at all...
Here is video of the issues : https://vid.me/hwaa
Here is most of my custom widget:
class CustomGroupBox(QGroupBox): def __init__(self, parent=None): super(CustomGroupBox, self).__init__(parent) def mouseMoveEvent(self, event): super(CustomGroupBox, self).mouseMoveEvent(event) p = QtGui.QCursor.pos() self.move(p.x(),p.y())
I think what I need is to somehow remap my mouse position to graphic view position and then on top of that offset widget position to mouse... but no idea how :- (
On the item returned by
Humh I must be missing something... when I tried :
ab = QGroupBox() ab.setFixedWidth(200) xx = QGridLayout() wa = QPushButton("wow") xx.addWidget(wa) wa.clicked.connect(self.printWiw) ab.setLayout(xx) ab.setFlag(QtGui.QGraphicsItem.ItemIsMovable) self.Scene.addWidget(ab)
I got :
ab.setFlag(QtGui.QGraphicsItem.ItemIsMovable) AttributeError: 'QGroupBox' object has no attribute 'setFlag'
When I tried to add it after scene :
for items in self.Scene.items(): items.setFlag(QtGui.QGraphicsItem.ItemIsMovable)
The item groupbox is still unmovable.
for items in self.Scene.items(): items.setFlag(QtGui.QGraphicsItem.ItemIsMovable, True) items.setFlag(QtGui.QGraphicsItem.ItemIsSelectable, True)
This stackoverflow answer might give you some clues.