Frage

I've create a frame with scroll by using the code below. And inside the frame i created another widget that can be drag and drop. The problem is when the vertical scrollbar is present and let say you scroll it down then drag and drop the object, the object drops at the wrong y coordinates. Can i get the value of the scroll bar then add it to the y coordinate? Or is there other ways to solve this? Thanks in advance :)

#Main Layout 
layout = QtGui.QVBoxLayout() 
layout.setSpacing(0)         
self.centralWidget.setLayout(layout) 

#Content Layout 
self.frame = QtGui.QFrame()
self.frame.setMinimumSize(400, 1000)

self.scrollArea = QtGui.QScrollArea() 
self.scrollArea.setWidget(self.frame)
self.scrollArea.setWidgetResizable(True)
layout.addWidget(self.scrollArea)
War es hilfreich?

Lösung

You can get the value of the scroll bar with:

yOffset = self.scrollArea.verticalScrollBar().value()

But there might be an additional offset to calculate.

You can also map the mouse coordinates to the relative coordinates of the widget inside the scroll area by using QWidget.mapFrom, mapTo, mapFromGlobal or mapToGlobal.

For example, if the main window is the widget receiving the event:

def dropEvent(self, event):
    pos = self.frame.mapFrom(self, event.pos())
Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top