【问题标题】:Set QLineEdit to Read-Only but still Accept Drops将 QLineEdit 设置为只读但仍接受 Drops
【发布时间】:2020-05-17 14:57:39
【问题描述】:

我注意到,当我将 QLineEdit 设置为只读时,这不允许我的小部件接受丢弃。

class CustomLineEdit(QtGui.QLineEdit):
    def __init__(self):
        super(CustomLineEdit, self).__init__()
        self.setReadOnly(True)
        self.setAcceptDrops(True)


    def dropEvent(self, event):
        input_text = event.mimeData().text()
        if input_text.endswith('Stalk'):
            self.setText(input_text.split(' ')[0])

【问题讨论】:

    标签: python pyqt pyqt4 qlineedit


    【解决方案1】:

    允许您启用 dropEvent 的 dragEnterEvent 方法在 QLineEdit 的情况下默认不接受 QLineEdit 只读时的事件。解决方案是覆盖该方法并接受该事件。

    class CustomLineEdit(QtGui.QLineEdit):
        def __init__(self):
            super(CustomLineEdit, self).__init__()
            self.setReadOnly(True)
            self.setAcceptDrops(True)
    
        def dragEnterEvent(self, event):
            event.acceptProposedAction()
    
        def dropEvent(self, event):
            input_text = event.mimeData().text()
            if input_text.endswith("Stalk"):
                values = input_text.split(" ")
                if values:
                    self.setText(values[0])

    更多信息请查看Drag-and-drop documentation

    【讨论】:

      猜你喜欢
      • 2012-11-05
      • 1970-01-01
      • 2016-03-27
      • 2021-04-13
      • 1970-01-01
      • 2021-07-21
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多