PyQt5 是否可以让 ScrollArea 在拖动其中的小部件时滚动?

问题描述 投票:0回答:1

尝试回答如何将 QLineEdit 从 QGridLayout 的一个单元格拖动到 pyqt 中的其他单元格?

我有四个单元格的网格布局(0,0),(0,1),(1,0),(1,1)。每个单元格都是带有滚动条的垂直布局。最初只有 (0,0) 单元格包含 QLineEdit。我想将它们拖放到任何一个单元格中。我该怎么办?

我写了这3个文件代码:

main.py

import sys

from PyQt5.QtWidgets import QApplication, QWidget

from myui_bis import Ui_Form 


class MainWidget(QWidget, Ui_Form):
    
    def __init__(self, *args, **kwargs):
        
        super().__init__()
        
        self.setupUi(self)
        
        self.pushButton.clicked.connect(self.printAcceptDrops)
        
        self.show()
        
    def printAcceptDrops(self):
        
        print('printAcceptDrops : ')

        print('Mainwidget : ', self.acceptDrops())

        print('self.scrollAreaWidgetContents : ', self.scrollAreaWidgetContents.acceptDrops())       
        print('self.scrollAreaWidgetContents_2 : ', self.scrollAreaWidgetContents_2.acceptDrops())   
        print('self.scrollAreaWidgetContents_3 : ', self.scrollAreaWidgetContents_3.acceptDrops())   
        print('self.scrollAreaWidgetContents_4 : ', self.scrollAreaWidgetContents_4.acceptDrops()) 
        
        

if __name__ == '__main__':
    app = QApplication(sys.argv)
    w = MainWidget()
    
    sys.exit(app.exec_())

我的

mywidgets.py

from PyQt5.QtWidgets import QLineEdit, QWidget, QScrollArea, QScroller

from PyQt5.QtCore import Qt, QMimeData
from PyQt5.QtGui import QDrag, QPixmap, QCursor

from PyQt5 import QtGui

from PyQt5 import QtCore

from PyQt5 import QtWidgets

class MyLineEdit(QLineEdit):
    
    def dragEnterEvent(self, e):

        print('ENTER DRAG EVENT IN ', self.objectName())
        
        e.accept()
        
        
    def dropEvent(self, e):
        
        widget = e.source()
        
        print('\n dropped on widget !!!!!!!!!!!!\n\n')
        
        
        if widget == self :
            
            return
        
        if widget.parent().layout().count() == 1:
            
            print('=============== 1   qlineedit')
            
            widget.parent().layout().parent().setAcceptDrops(True)
        
       
        widget.setParent(None)
        
self.parent().layout().insertWidget(self.parent().layout().indexOf(self) , widget)
       
    def mouseMoveEvent(self, e):

        if e.buttons() == Qt.LeftButton:
            drag = QDrag(self)
            mime = QMimeData()
            drag.setMimeData(mime)

            pixmap = QPixmap(self.size())
            self.render(pixmap)
            drag.setPixmap(pixmap)

            drag.exec_(Qt.MoveAction)
            
            
class MyWidget(QWidget):
    
    def dragEnterEvent(self, e):
        
        print('ENTER DRAG EVENT IN ', self.objectName())
        
        print('self.layout.count()', self.layout().count())
        
        e.accept()
        
    def dropEvent(self, e):
        
        pos = e.pos()
        
        widget = e.source()
        
        print('pos : ', pos, 'mouse : ', QCursor.pos())
        print('event.source : ', widget , 'event.source.parent() :', widget.parent())
        
        print('event.source  name: ', widget.objectName()) 
        
        print('parent : ', self.parent() , self.parent().objectName())
        
        print('self.layput() : ', self.layout())
        
        print('self.objectName() : ', self.objectName())
        
        if widget.parent().layout().count() == 1:
            
            print('=============== 1   WIDGET')
            
            widget.parent().layout().parent().setAcceptDrops(True)

        if self.layout().count() == 0 :
            
            widget.setParent(None)
            
            self.layout().addWidget(widget)
            
            self.setAcceptDrops(False)
            
        
class MyScrollArea(QScrollArea):
    
    def __init__(self , parent = None):
        super(MyScrollArea, self).__init__(parent)
           

我的 PyQt5-Designer ui 文件用 pyuic5

myui_bis
翻译:

from PyQt5 import QtCore, QtWidgets

from PyQt5.QtCore import Qt, QMimeData
from PyQt5.QtGui import QDrag


class Ui_Form(object):
    def setupUi(self, Form):
        Form.setObjectName("Form")
        Form.resize(698, 672)
        sizePolicy = QtWidgets.QSizePolicy(QtWidgets.QSizePolicy.Preferred, QtWidgets.QSizePolicy.Preferred)
        sizePolicy.setHorizontalStretch(0)
        sizePolicy.setVerticalStretch(0)
        sizePolicy.setHeightForWidth(Form.sizePolicy().hasHeightForWidth())
        Form.setSizePolicy(sizePolicy)
        Form.setAcceptDrops(False)
        self.gridLayout = QtWidgets.QGridLayout(Form)
        self.gridLayout.setObjectName("gridLayout")
        self.scrollArea_3 = MyScrollArea(Form)
        self.scrollArea_3.setAcceptDrops(True)
        self.scrollArea_3.setVerticalScrollBarPolicy(QtCore.Qt.ScrollBarAlwaysOn)
        self.scrollArea_3.setWidgetResizable(True)
        self.scrollArea_3.setObjectName("scrollArea_3")
        self.scrollAreaWidgetContents_3 = MyWidget()
        self.scrollAreaWidgetContents_3.setGeometry(QtCore.QRect(0, 0, 315, 303))
        sizePolicy = QtWidgets.QSizePolicy(QtWidgets.QSizePolicy.Expanding, QtWidgets.QSizePolicy.Preferred)
        sizePolicy.setHorizontalStretch(0)
        sizePolicy.setVerticalStretch(0)
        sizePolicy.setHeightForWidth(self.scrollAreaWidgetContents_3.sizePolicy().hasHeightForWidth())
        self.scrollAreaWidgetContents_3.setSizePolicy(sizePolicy)
        self.scrollAreaWidgetContents_3.setAcceptDrops(True)
        self.scrollAreaWidgetContents_3.setObjectName("scrollAreaWidgetContents_3")
        self.verticalLayout_2 = QtWidgets.QVBoxLayout(self.scrollAreaWidgetContents_3)
        self.verticalLayout_2.setSpacing(30)
        self.verticalLayout_2.setObjectName("verticalLayout_2")
        self.scrollArea_3.setWidget(self.scrollAreaWidgetContents_3)
        self.gridLayout.addWidget(self.scrollArea_3, 1, 0, 1, 1)
        self.scrollArea_4 = MyScrollArea(Form)
        self.scrollArea_4.setAcceptDrops(True)
        self.scrollArea_4.setVerticalScrollBarPolicy(QtCore.Qt.ScrollBarAlwaysOn)
        self.scrollArea_4.setWidgetResizable(True)
        self.scrollArea_4.setObjectName("scrollArea_4")
        self.scrollAreaWidgetContents_4 = MyWidget()
        self.scrollAreaWidgetContents_4.setGeometry(QtCore.QRect(0, 0, 315, 303))
        sizePolicy = QtWidgets.QSizePolicy(QtWidgets.QSizePolicy.Expanding, QtWidgets.QSizePolicy.Preferred)
        sizePolicy.setHorizontalStretch(0)
        sizePolicy.setVerticalStretch(0)
        sizePolicy.setHeightForWidth(self.scrollAreaWidgetContents_4.sizePolicy().hasHeightForWidth())
        self.scrollAreaWidgetContents_4.setSizePolicy(sizePolicy)
        self.scrollAreaWidgetContents_4.setAcceptDrops(True)
        self.scrollAreaWidgetContents_4.setObjectName("scrollAreaWidgetContents_4")
        self.verticalLayout_4 = QtWidgets.QVBoxLayout(self.scrollAreaWidgetContents_4)
        self.verticalLayout_4.setSpacing(30)
        self.verticalLayout_4.setObjectName("verticalLayout_4")
        self.scrollArea_4.setWidget(self.scrollAreaWidgetContents_4)
        self.gridLayout.addWidget(self.scrollArea_4, 1, 1, 1, 1)
        self.scrollArea_2 = MyScrollArea(Form)
        self.scrollArea_2.setAcceptDrops(True)
        self.scrollArea_2.setVerticalScrollBarPolicy(QtCore.Qt.ScrollBarAlwaysOn)
        self.scrollArea_2.setWidgetResizable(True)
        self.scrollArea_2.setObjectName("scrollArea_2")
        self.scrollAreaWidgetContents_2 = MyWidget()
        self.scrollAreaWidgetContents_2.setGeometry(QtCore.QRect(0, 0, 315, 304))
        sizePolicy = QtWidgets.QSizePolicy(QtWidgets.QSizePolicy.Expanding, QtWidgets.QSizePolicy.Preferred)
        sizePolicy.setHorizontalStretch(0)
        sizePolicy.setVerticalStretch(0)
        sizePolicy.setHeightForWidth(self.scrollAreaWidgetContents_2.sizePolicy().hasHeightForWidth())
        self.scrollAreaWidgetContents_2.setSizePolicy(sizePolicy)
        self.scrollAreaWidgetContents_2.setAcceptDrops(True)
        self.scrollAreaWidgetContents_2.setObjectName("scrollAreaWidgetContents_2")
        self.verticalLayout_3 = QtWidgets.QVBoxLayout(self.scrollAreaWidgetContents_2)
        self.verticalLayout_3.setSpacing(30)
        self.verticalLayout_3.setObjectName("verticalLayout_3")
        self.scrollArea_2.setWidget(self.scrollAreaWidgetContents_2)
        self.gridLayout.addWidget(self.scrollArea_2, 0, 1, 1, 1)
        self.scrollArea = MyScrollArea(Form)
        sizePolicy = QtWidgets.QSizePolicy(QtWidgets.QSizePolicy.Expanding, QtWidgets.QSizePolicy.Expanding)
        sizePolicy.setHorizontalStretch(1)
        sizePolicy.setVerticalStretch(1)
        sizePolicy.setHeightForWidth(self.scrollArea.sizePolicy().hasHeightForWidth())
        self.scrollArea.setSizePolicy(sizePolicy)
        self.scrollArea.setAcceptDrops(True)
        self.scrollArea.setVerticalScrollBarPolicy(QtCore.Qt.ScrollBarAlwaysOn)
        self.scrollArea.setWidgetResizable(True)
        self.scrollArea.setObjectName("scrollArea")
        self.scrollAreaWidgetContents = MyWidget()
        self.scrollAreaWidgetContents.setGeometry(QtCore.QRect(0, -304, 315, 897))
        sizePolicy = QtWidgets.QSizePolicy(QtWidgets.QSizePolicy.Expanding, QtWidgets.QSizePolicy.Preferred)
        sizePolicy.setHorizontalStretch(0)
        sizePolicy.setVerticalStretch(0)
        sizePolicy.setHeightForWidth(self.scrollAreaWidgetContents.sizePolicy().hasHeightForWidth())
        self.scrollAreaWidgetContents.setSizePolicy(sizePolicy)
        self.scrollAreaWidgetContents.setAcceptDrops(False)
        self.scrollAreaWidgetContents.setObjectName("scrollAreaWidgetContents")
        self.verticalLayout = QtWidgets.QVBoxLayout(self.scrollAreaWidgetContents)
        self.verticalLayout.setSpacing(30)
        self.verticalLayout.setObjectName("verticalLayout")
        self.lineEdit_1 = MyLineEdit(self.scrollAreaWidgetContents)
        self.lineEdit_1.setObjectName("lineEdit_1")
        self.verticalLayout.addWidget(self.lineEdit_1)
        self.lineEdit_2 = MyLineEdit(self.scrollAreaWidgetContents)
        self.lineEdit_2.setObjectName("lineEdit_2")
        self.verticalLayout.addWidget(self.lineEdit_2)
        self.lineEdit_3 = MyLineEdit(self.scrollAreaWidgetContents)
        self.lineEdit_3.setObjectName("lineEdit_3")
        self.verticalLayout.addWidget(self.lineEdit_3)
        self.lineEdit_4 = MyLineEdit(self.scrollAreaWidgetContents)
        self.lineEdit_4.setObjectName("lineEdit_4")
        self.verticalLayout.addWidget(self.lineEdit_4)
        self.lineEdit_5 = MyLineEdit(self.scrollAreaWidgetContents)
        self.lineEdit_5.setObjectName("lineEdit_5")
        self.verticalLayout.addWidget(self.lineEdit_5)
        self.lineEdit_15 = MyLineEdit(self.scrollAreaWidgetContents)
        self.lineEdit_15.setObjectName("lineEdit_15")
        self.verticalLayout.addWidget(self.lineEdit_15)
        self.lineEdit_14 = MyLineEdit(self.scrollAreaWidgetContents)
        self.lineEdit_14.setObjectName("lineEdit_14")
        self.verticalLayout.addWidget(self.lineEdit_14)
        self.lineEdit_13 = MyLineEdit(self.scrollAreaWidgetContents)
        self.lineEdit_13.setObjectName("lineEdit_13")
        self.verticalLayout.addWidget(self.lineEdit_13)
        self.lineEdit_12 = MyLineEdit(self.scrollAreaWidgetContents)
        self.lineEdit_12.setObjectName("lineEdit_12")
        self.verticalLayout.addWidget(self.lineEdit_12)
        self.lineEdit_11 = MyLineEdit(self.scrollAreaWidgetContents)
        self.lineEdit_11.setObjectName("lineEdit_11")
        self.verticalLayout.addWidget(self.lineEdit_11)
        self.lineEdit_10 = MyLineEdit(self.scrollAreaWidgetContents)
        self.lineEdit_10.setObjectName("lineEdit_10")
        self.verticalLayout.addWidget(self.lineEdit_10)
        self.lineEdit_9 = MyLineEdit(self.scrollAreaWidgetContents)
        self.lineEdit_9.setObjectName("lineEdit_9")
        self.verticalLayout.addWidget(self.lineEdit_9)
        self.lineEdit_8 = MyLineEdit(self.scrollAreaWidgetContents)
        self.lineEdit_8.setObjectName("lineEdit_8")
        self.verticalLayout.addWidget(self.lineEdit_8)
        self.lineEdit_7 = MyLineEdit(self.scrollAreaWidgetContents)
        self.lineEdit_7.setObjectName("lineEdit_7")
        self.verticalLayout.addWidget(self.lineEdit_7)
        self.lineEdit_6 = MyLineEdit(self.scrollAreaWidgetContents)
        self.lineEdit_6.setObjectName("lineEdit_6")
        self.verticalLayout.addWidget(self.lineEdit_6)
        self.scrollArea.setWidget(self.scrollAreaWidgetContents)
        self.gridLayout.addWidget(self.scrollArea, 0, 0, 1, 1)
        self.pushButton = QtWidgets.QPushButton(Form)
        sizePolicy = QtWidgets.QSizePolicy(QtWidgets.QSizePolicy.Minimum, QtWidgets.QSizePolicy.Fixed)
        sizePolicy.setHorizontalStretch(1)
        sizePolicy.setVerticalStretch(0)
        sizePolicy.setHeightForWidth(self.pushButton.sizePolicy().hasHeightForWidth())
        self.pushButton.setSizePolicy(sizePolicy)
        self.pushButton.setObjectName("pushButton")
        self.gridLayout.addWidget(self.pushButton, 2, 0, 1, 1)
        self.gridLayout.setColumnStretch(1, 1)
        self.gridLayout.setRowStretch(1, 1)

        self.retranslateUi(Form)
        QtCore.QMetaObject.connectSlotsByName(Form)

    def retranslateUi(self, Form):
        _translate = QtCore.QCoreApplication.translate
        Form.setWindowTitle(_translate("Form", "Form"))
        self.lineEdit_1.setText(_translate("Form", "1"))
        self.lineEdit_2.setText(_translate("Form", "2"))
        self.lineEdit_3.setText(_translate("Form", "3"))
        self.lineEdit_4.setText(_translate("Form", "4"))
        self.lineEdit_5.setText(_translate("Form", "5"))
        self.lineEdit_15.setText(_translate("Form", "6"))
        self.lineEdit_14.setText(_translate("Form", "7"))
        self.lineEdit_13.setText(_translate("Form", "8"))
        self.lineEdit_12.setText(_translate("Form", "9"))
        self.lineEdit_11.setText(_translate("Form", "10"))
        self.lineEdit_10.setText(_translate("Form", "11"))
        self.lineEdit_9.setText(_translate("Form", "12"))
        self.lineEdit_8.setText(_translate("Form", "13"))
        self.lineEdit_7.setText(_translate("Form", "14"))
        self.lineEdit_6.setText(_translate("Form", "15"))
        self.pushButton.setText(_translate("Form", "Check AcceptDrops"))

from mywidgets import MyLineEdit, MyScrollArea, MyWidget

现在,当这些为空时,

QLineEdit
小部件将添加到scrollAreas布局中,但随后它们的acceptDrops属性设置为False,并且下一个
QLineEdit
小部件必须放置在已经存在的小部件上,并在其中获取其布局索引位置布局。当布局中没有留下任何widgrt时,其acceptDrops值将再次设置为True。

现在唯一的问题是我无法在四个滚动区域中的任何一个中同时滚动和拖动,但之前的答案中也存在该故障。

enter image description here

我终于意识到,在PyQt5(如果我错了请纠正我)

ScrollArea
只能使用鼠标滚轮或拖动
ScrollBar
本身进行滚动。

所以我想知道是否有办法实现拖动同时滚动区域的结果?

我听说过

QScroller
,但我不确定它的作用,并试图弄清楚在进行拖动时我应该过滤什么事件,但我找不到任何线索,而且我应该能够计算出鼠标拖动的垂直分量以某种方式向前移动到
ScrollArea
ScrollArea.viewport
(我什至不确定我是否真的理解视口是什么!!)

python scroll pyqt pyqt5 qscrollarea
1个回答
0
投票

感谢@Musicamante的建议,这是我的代码:

main.py

import sys

from PyQt5.QtWidgets import QApplication, QWidget

from myui_bis import Ui_Form 



class MainWidget(QWidget, Ui_Form):
    
    def __init__(self, *args, **kwargs):
        
        super().__init__()
        
        self.setupUi(self)
        
        
        self.pushButton.clicked.connect(self.printAcceptDrops)
        
        self.pushButton_2.clicked.connect(self.printScrollBars)
        
        self.pushButton_3.clicked.connect(self.setAcceptDropsTrue)
        
         # self.scrollAreaWidgetContents.setAcceptDrops(False) # try uncommenting it
        
        self.show()
        


    def setAcceptDropsTrue(self):
        
        print('self.scrollAreaWidgetContents : ', self.scrollAreaWidgetContents.acceptDrops())       
        print('self.scrollAreaWidgetContents_2 : ', self.scrollAreaWidgetContents_2.acceptDrops())   
        print('self.scrollAreaWidgetContents_3 : ', self.scrollAreaWidgetContents_3.acceptDrops())   
        print('self.scrollAreaWidgetContents_4 : ', self.scrollAreaWidgetContents_4.acceptDrops()) 
        
        
        self.scrollAreaWidgetContents.layout().parent().setAcceptDrops(True)
        self.scrollAreaWidgetContents_2.layout().parent().setAcceptDrops(True)     
        self.scrollAreaWidgetContents_3.layout().parent().setAcceptDrops(True)    
        self.scrollAreaWidgetContents_4.layout().parent().setAcceptDrops(True)     
        
        
        print('self.scrollAreaWidgetContents : ', self.scrollAreaWidgetContents.acceptDrops())       
        print('self.scrollAreaWidgetContents_2 : ', self.scrollAreaWidgetContents_2.acceptDrops())   
        print('self.scrollAreaWidgetContents_3 : ', self.scrollAreaWidgetContents_3.acceptDrops())   
        print('self.scrollAreaWidgetContents_4 : ', self.scrollAreaWidgetContents_4.acceptDrops()) 
        
        

        
    def printAcceptDrops(self):
        
        print('printAcceptDrops : ')


        print('Mainwidget : ', self.acceptDrops())

        print('self.scrollAreaWidgetContents : ', self.scrollAreaWidgetContents.acceptDrops())       
        print('self.scrollAreaWidgetContents_2 : ', self.scrollAreaWidgetContents_2.acceptDrops())   
        print('self.scrollAreaWidgetContents_3 : ', self.scrollAreaWidgetContents_3.acceptDrops())   
        print('self.scrollAreaWidgetContents_4 : ', self.scrollAreaWidgetContents_4.acceptDrops()) 
        
    
    def printScrollBars(self):
        
        self.scrollArea.verticalScrollBar().setValue(self.scrollArea.verticalScrollBar().maximum())
        
       
if __name__ == '__main__':
    app = QApplication(sys.argv)
    w = MainWidget()
    
    sys.exit(app.exec_())

mywidgets.py



from PyQt5.QtWidgets import QLineEdit, QWidget, QScrollArea

from PyQt5.QtGui import QDrag, QPixmap, QCursor

from PyQt5.QtCore import Qt, QMimeData, QTimer


class MyLineEdit(QLineEdit):
    
    def __init__(self , parent = None):
        super(MyLineEdit, self).__init__(parent)
    
    
    def dragEnterEvent(self, e):
        
        e.accept()
        
        
    def dropEvent(self, e):
        
        
        widget = e.source()
        
        print('\n dropped on widget !!!!!!!!!!!!\n\n')
        
        
        if widget == self :
            
            return
        
        
        if widget.parent().layout().count() == 1:
            
            print('=============== 1   qlineedit')
            
            widget.parent().layout().parent().setAcceptDrops(True)
        
       
        widget.setParent(None)
        
        
        self.parent().layout().insertWidget(self.parent().layout().indexOf(self) , widget)
        
        
    
    
    def mouseMoveEvent(self, e):

        if e.buttons() == Qt.LeftButton:
            
            drag = QDrag(self)
            mime = QMimeData()
            drag.setMimeData(mime)

            pixmap = QPixmap(self.size())
            self.render(pixmap)
            drag.setPixmap(pixmap)

            drag.exec_(Qt.MoveAction)
            
            

            
class MyWidget(QWidget):
    
    def __init__(self , parent = None):
        super(MyWidget, self).__init__(parent)
        
        self.timer =  QTimer()
        
        self.timer.timeout.connect(lambda timer = self.timer : self.update_ScrollBar(timer))
        
        
    
    def dragMoveEvent(self, e):
         
        self.timer.start(1000) # time in milliseconds.
        
    def dragLeaveEvent(self , e) :
    
        print('DRAG LEAVED !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!')
        
        self.timer.stop()
    
        print('TIMER STOPPED')
    
    
    def update_ScrollBar(self , timer):
        
        coords = self.parent().rect().getRect()
        
        yup = coords[1]
        
        ydown = coords[1]+coords[3]
        
        ycurs =  self.parent().mapFromGlobal(QCursor().pos()).y() #### OK

        
        if abs(yup - ycurs) < 80 :
            
            print(abs(yup - ycurs) , yup , ycurs)
            
            self.parent().parent().verticalScrollBar().setValue(self.parent().parent().verticalScrollBar().value() - 80)
            
            timer.stop()
            
        elif abs(ydown - ycurs) < 80 :
            
            self.parent().parent().verticalScrollBar().setValue(self.parent().parent().verticalScrollBar().value() + 80)
            
            timer.stop()
        
        else:
        
            timer.stop()
            
            print('TIMER STOPPED')

    
    def dragEnterEvent(self, e):
        
        
        print('ENTER DRAG EVENT IN ', self.objectName())
        
        print('self.layout.count()', self.layout().count())
        
        e.accept()
        
        
    def dropEvent(self, e):
        
        widget = e.source()
        
        
        if widget.parent().layout().count() == 1:
            
            print('=============== 1   WIDGET')
            
            widget.parent().layout().parent().setAcceptDrops(True)
            
        
        if self.layout().count() == 0 :
            
            
            widget.setParent(None)
            
            self.layout().addWidget(widget)
            
            self.setAcceptDrops(False)
            

class MyScrollArea(QScrollArea):
    
    def __init__(self , parent = None):
        super(MyScrollArea, self).__init__(parent)
     

我的 PyQt5-Designer ui 文件用 pyuic5

myui_bis
翻译:

from PyQt5 import QtCore, QtWidgets

class Ui_Form(object):
    def setupUi(self, Form):
        Form.setObjectName("Form")
        Form.resize(757, 708)
        sizePolicy = QtWidgets.QSizePolicy(QtWidgets.QSizePolicy.Preferred, QtWidgets.QSizePolicy.Preferred)
        sizePolicy.setHorizontalStretch(0)
        sizePolicy.setVerticalStretch(0)
        sizePolicy.setHeightForWidth(Form.sizePolicy().hasHeightForWidth())
        Form.setSizePolicy(sizePolicy)
        Form.setAcceptDrops(False)
        self.gridLayout = QtWidgets.QGridLayout(Form)
        self.gridLayout.setObjectName("gridLayout")
        self.scrollArea_4 = MyScrollArea(Form)
        self.scrollArea_4.setAcceptDrops(True)
        self.scrollArea_4.setVerticalScrollBarPolicy(QtCore.Qt.ScrollBarAlwaysOn)
        self.scrollArea_4.setWidgetResizable(True)
        self.scrollArea_4.setObjectName("scrollArea_4")
        self.scrollAreaWidgetContents_4 = MyWidget()
        self.scrollAreaWidgetContents_4.setGeometry(QtCore.QRect(0, 0, 344, 302))
        sizePolicy = QtWidgets.QSizePolicy(QtWidgets.QSizePolicy.Expanding, QtWidgets.QSizePolicy.Preferred)
        sizePolicy.setHorizontalStretch(0)
        sizePolicy.setVerticalStretch(0)
        sizePolicy.setHeightForWidth(self.scrollAreaWidgetContents_4.sizePolicy().hasHeightForWidth())
        self.scrollAreaWidgetContents_4.setSizePolicy(sizePolicy)
        self.scrollAreaWidgetContents_4.setAcceptDrops(True)
        self.scrollAreaWidgetContents_4.setObjectName("scrollAreaWidgetContents_4")
        self.verticalLayout_4 = QtWidgets.QVBoxLayout(self.scrollAreaWidgetContents_4)
        self.verticalLayout_4.setContentsMargins(-1, 30, -1, 30)
        self.verticalLayout_4.setSpacing(30)
        self.verticalLayout_4.setObjectName("verticalLayout_4")
        self.scrollArea_4.setWidget(self.scrollAreaWidgetContents_4)
        self.gridLayout.addWidget(self.scrollArea_4, 1, 1, 1, 1)
        self.scrollArea = MyScrollArea(Form)
        sizePolicy = QtWidgets.QSizePolicy(QtWidgets.QSizePolicy.Expanding, QtWidgets.QSizePolicy.Expanding)
        sizePolicy.setHorizontalStretch(1)
        sizePolicy.setVerticalStretch(1)
        sizePolicy.setHeightForWidth(self.scrollArea.sizePolicy().hasHeightForWidth())
        self.scrollArea.setSizePolicy(sizePolicy)
        self.scrollArea.setAcceptDrops(True)
        self.scrollArea.setVerticalScrollBarPolicy(QtCore.Qt.ScrollBarAlwaysOn)
        self.scrollArea.setWidgetResizable(True)
        self.scrollArea.setObjectName("scrollArea")
        self.scrollAreaWidgetContents = MyWidget()
        self.scrollAreaWidgetContents.setGeometry(QtCore.QRect(0, 0, 345, 945))
        sizePolicy = QtWidgets.QSizePolicy(QtWidgets.QSizePolicy.Expanding, QtWidgets.QSizePolicy.Preferred)
        sizePolicy.setHorizontalStretch(0)
        sizePolicy.setVerticalStretch(0)
        sizePolicy.setHeightForWidth(self.scrollAreaWidgetContents.sizePolicy().hasHeightForWidth())
        self.scrollAreaWidgetContents.setSizePolicy(sizePolicy)
        self.scrollAreaWidgetContents.setAcceptDrops(True)
        self.scrollAreaWidgetContents.setObjectName("scrollAreaWidgetContents")
        self.verticalLayout = QtWidgets.QVBoxLayout(self.scrollAreaWidgetContents)
        self.verticalLayout.setContentsMargins(-1, 30, -1, 30)
        self.verticalLayout.setSpacing(30)
        self.verticalLayout.setObjectName("verticalLayout")
        self.lineEdit_1 = MyLineEdit(self.scrollAreaWidgetContents)
        self.lineEdit_1.setObjectName("lineEdit_1")
        self.verticalLayout.addWidget(self.lineEdit_1)
        self.lineEdit_2 = MyLineEdit(self.scrollAreaWidgetContents)
        self.lineEdit_2.setObjectName("lineEdit_2")
        self.verticalLayout.addWidget(self.lineEdit_2)
        self.lineEdit_3 = MyLineEdit(self.scrollAreaWidgetContents)
        self.lineEdit_3.setObjectName("lineEdit_3")
        self.verticalLayout.addWidget(self.lineEdit_3)
        self.lineEdit_4 = MyLineEdit(self.scrollAreaWidgetContents)
        self.lineEdit_4.setObjectName("lineEdit_4")
        self.verticalLayout.addWidget(self.lineEdit_4)
        self.lineEdit_5 = MyLineEdit(self.scrollAreaWidgetContents)
        self.lineEdit_5.setObjectName("lineEdit_5")
        self.verticalLayout.addWidget(self.lineEdit_5)
        self.lineEdit_15 = MyLineEdit(self.scrollAreaWidgetContents)
        self.lineEdit_15.setObjectName("lineEdit_15")
        self.verticalLayout.addWidget(self.lineEdit_15)
        self.lineEdit_14 = MyLineEdit(self.scrollAreaWidgetContents)
        self.lineEdit_14.setObjectName("lineEdit_14")
        self.verticalLayout.addWidget(self.lineEdit_14)
        self.lineEdit_13 = MyLineEdit(self.scrollAreaWidgetContents)
        self.lineEdit_13.setObjectName("lineEdit_13")
        self.verticalLayout.addWidget(self.lineEdit_13)
        self.lineEdit_12 = MyLineEdit(self.scrollAreaWidgetContents)
        self.lineEdit_12.setObjectName("lineEdit_12")
        self.verticalLayout.addWidget(self.lineEdit_12)
        self.lineEdit_11 = MyLineEdit(self.scrollAreaWidgetContents)
        self.lineEdit_11.setObjectName("lineEdit_11")
        self.verticalLayout.addWidget(self.lineEdit_11)
        self.lineEdit_10 = MyLineEdit(self.scrollAreaWidgetContents)
        self.lineEdit_10.setObjectName("lineEdit_10")
        self.verticalLayout.addWidget(self.lineEdit_10)
        self.lineEdit_9 = MyLineEdit(self.scrollAreaWidgetContents)
        self.lineEdit_9.setObjectName("lineEdit_9")
        self.verticalLayout.addWidget(self.lineEdit_9)
        self.lineEdit_8 = MyLineEdit(self.scrollAreaWidgetContents)
        self.lineEdit_8.setObjectName("lineEdit_8")
        self.verticalLayout.addWidget(self.lineEdit_8)
        self.lineEdit_7 = MyLineEdit(self.scrollAreaWidgetContents)
        self.lineEdit_7.setObjectName("lineEdit_7")
        self.verticalLayout.addWidget(self.lineEdit_7)
        self.lineEdit_6 = MyLineEdit(self.scrollAreaWidgetContents)
        self.lineEdit_6.setObjectName("lineEdit_6")
        self.verticalLayout.addWidget(self.lineEdit_6)
        self.scrollArea.setWidget(self.scrollAreaWidgetContents)
        self.gridLayout.addWidget(self.scrollArea, 0, 0, 1, 1)
        self.scrollArea_2 = MyScrollArea(Form)
        self.scrollArea_2.setAcceptDrops(True)
        self.scrollArea_2.setVerticalScrollBarPolicy(QtCore.Qt.ScrollBarAlwaysOn)
        self.scrollArea_2.setWidgetResizable(True)
        self.scrollArea_2.setObjectName("scrollArea_2")
        self.scrollAreaWidgetContents_2 = MyWidget()
        self.scrollAreaWidgetContents_2.setGeometry(QtCore.QRect(0, 0, 344, 302))
        sizePolicy = QtWidgets.QSizePolicy(QtWidgets.QSizePolicy.Expanding, QtWidgets.QSizePolicy.Preferred)
        sizePolicy.setHorizontalStretch(0)
        sizePolicy.setVerticalStretch(0)
        sizePolicy.setHeightForWidth(self.scrollAreaWidgetContents_2.sizePolicy().hasHeightForWidth())
        self.scrollAreaWidgetContents_2.setSizePolicy(sizePolicy)
        self.scrollAreaWidgetContents_2.setAcceptDrops(True)
        self.scrollAreaWidgetContents_2.setObjectName("scrollAreaWidgetContents_2")
        self.verticalLayout_3 = QtWidgets.QVBoxLayout(self.scrollAreaWidgetContents_2)
        self.verticalLayout_3.setContentsMargins(-1, 30, -1, 30)
        self.verticalLayout_3.setSpacing(30)
        self.verticalLayout_3.setObjectName("verticalLayout_3")
        self.scrollArea_2.setWidget(self.scrollAreaWidgetContents_2)
        self.gridLayout.addWidget(self.scrollArea_2, 0, 1, 1, 1)
        self.scrollArea_3 = MyScrollArea(Form)
        self.scrollArea_3.setAcceptDrops(True)
        self.scrollArea_3.setVerticalScrollBarPolicy(QtCore.Qt.ScrollBarAlwaysOn)
        self.scrollArea_3.setWidgetResizable(True)
        self.scrollArea_3.setObjectName("scrollArea_3")
        self.scrollAreaWidgetContents_3 = MyWidget()
        self.scrollAreaWidgetContents_3.setGeometry(QtCore.QRect(0, 0, 345, 302))
        sizePolicy = QtWidgets.QSizePolicy(QtWidgets.QSizePolicy.Expanding, QtWidgets.QSizePolicy.Preferred)
        sizePolicy.setHorizontalStretch(0)
        sizePolicy.setVerticalStretch(0)
        sizePolicy.setHeightForWidth(self.scrollAreaWidgetContents_3.sizePolicy().hasHeightForWidth())
        self.scrollAreaWidgetContents_3.setSizePolicy(sizePolicy)
        self.scrollAreaWidgetContents_3.setAcceptDrops(True)
        self.scrollAreaWidgetContents_3.setObjectName("scrollAreaWidgetContents_3")
        self.verticalLayout_2 = QtWidgets.QVBoxLayout(self.scrollAreaWidgetContents_3)
        self.verticalLayout_2.setContentsMargins(-1, 30, -1, 30)
        self.verticalLayout_2.setSpacing(30)
        self.verticalLayout_2.setObjectName("verticalLayout_2")
        self.scrollArea_3.setWidget(self.scrollAreaWidgetContents_3)
        self.gridLayout.addWidget(self.scrollArea_3, 1, 0, 1, 1)
        self.pushButton_2 = QtWidgets.QPushButton(Form)
        self.pushButton_2.setObjectName("pushButton_2")
        self.gridLayout.addWidget(self.pushButton_2, 2, 1, 1, 1)
        self.pushButton = QtWidgets.QPushButton(Form)
        sizePolicy = QtWidgets.QSizePolicy(QtWidgets.QSizePolicy.Minimum, QtWidgets.QSizePolicy.Fixed)
        sizePolicy.setHorizontalStretch(1)
        sizePolicy.setVerticalStretch(0)
        sizePolicy.setHeightForWidth(self.pushButton.sizePolicy().hasHeightForWidth())
        self.pushButton.setSizePolicy(sizePolicy)
        self.pushButton.setObjectName("pushButton")
        self.gridLayout.addWidget(self.pushButton, 2, 0, 1, 1)
        self.pushButton_3 = QtWidgets.QPushButton(Form)
        self.pushButton_3.setObjectName("pushButton_3")
        self.gridLayout.addWidget(self.pushButton_3, 3, 0, 1, 1)
        self.gridLayout.setColumnStretch(1, 1)
        self.gridLayout.setRowStretch(1, 1)

        self.retranslateUi(Form)
        QtCore.QMetaObject.connectSlotsByName(Form)

    def retranslateUi(self, Form):
        _translate = QtCore.QCoreApplication.translate
        Form.setWindowTitle(_translate("Form", "Form"))
        self.lineEdit_1.setText(_translate("Form", "1"))
        self.lineEdit_2.setText(_translate("Form", "2"))
        self.lineEdit_3.setText(_translate("Form", "3"))
        self.lineEdit_4.setText(_translate("Form", "4"))
        self.lineEdit_5.setText(_translate("Form", "5"))
        self.lineEdit_15.setText(_translate("Form", "6"))
        self.lineEdit_14.setText(_translate("Form", "7"))
        self.lineEdit_13.setText(_translate("Form", "8"))
        self.lineEdit_12.setText(_translate("Form", "9"))
        self.lineEdit_11.setText(_translate("Form", "10"))
        self.lineEdit_10.setText(_translate("Form", "11"))
        self.lineEdit_9.setText(_translate("Form", "12"))
        self.lineEdit_8.setText(_translate("Form", "13"))
        self.lineEdit_7.setText(_translate("Form", "14"))
        self.lineEdit_6.setText(_translate("Form", "15"))
        self.pushButton_2.setText(_translate("Form", "ScrollBar 1 Maximum"))
        self.pushButton.setText(_translate("Form", "Check AcceptDrops"))
        self.pushButton_3.setText(_translate("Form", "Set AcceptDrops True"))


from mywidgets import MyLineEdit, MyScrollArea, MyWidget

这里是小部件预览:

enter image description here

我的概念验证工作正常(更新 ScrollBar 位置有点慢,但认为可以通过更改 QTimer 超时来改进:

self.timer.start(timeout in milliseconds)
),但我想到我移动 QLineEdits (MyLineEdit) 的方式依赖于 AcceptDrops = False对于 3 个 QScrollArea,将小部件插入到另一个的顶部。现在我意识到,如果没有 AcceptDrops = True,我无法使用触发 self.timer.start() 的 MyLineEdit 小部件 DragMoveEvent。知道如何让这种方法适用于不接受掉落的地区的儿童吗?

使用“Set AcceptDrops True”QPushButton 将设置所有 ScrollArea 能够在拖动时移动滚动条,但最终会像scrollArea 1 一样,不接受 MyLineEdit 小部件之外的拖放,但在释放时不会显示任何禁止符号鼠标左键(尝试放下动作)

您可以尝试取消注释

#self.scrollAreaWidgetContents.setAcceptDrops(False)
看到一旦完成,就不可能在拖动小部件时移动滚动条

© www.soinside.com 2019 - 2024. All rights reserved.