【问题标题】:Connect to a Method/Function on close of QTextEdit in PyQt在 PyQt 中关闭 QTextEdit 时连接到方法/函数
【发布时间】:2017-05-15 04:18:36
【问题描述】:

如何在 QTextEdit 小部件窗口关闭时连接到函数?

我的代码:

self.textBox = QtGui.QTextEdit()
self.textBox.setWindowTitle('Editor')
self.textBox.setGeometry(100, 100, 1000, 500)
self.textBox.show()
self.textBox.???.connect(self.someFunc)   #????

如果我这样做了

self.textBox.close().connect(self.someFunc)

它立即关闭并说

AttributeError: 'bool' object has no attribute 'connect'

如果我这样做

self.textBox.closeEvent(self.someFunc)

它说

TypeError: QTextEdit.closeEvent(self.someFunc): argument 1 has unexpected type 'method'

我该如何解决这个问题?

【问题讨论】:

  • 关闭是什么意思?
  • 或者你的意思是关闭包含它的小部件?
  • 这是一个我实现的文本编辑器,从主窗口的菜单栏中打开,它像一个新窗口一样打开,所以当编辑器打开并在窗口中键入内容时,然后关闭编辑器(QTextEdit)窗口然后我需要关闭事件来连接到一个函数。
  • * 所以当用户打开并在窗口中输入内容然后关闭它时
  • 我个人不喜欢猴子补丁,但self.textBox.closeEvent = self.someFunc 会做你想做的。

标签: python qt pyqt pyqt4 qtextedit


【解决方案1】:

这不是最优雅的方式,但它有效,另一种方式是从 QTextEdit 继承并通过发出信号覆盖 closeEvent 方法。

用途:

    self.textBox = QTextEdit()
    self.textBox.setWindowTitle('Editor')
    self.textBox.setGeometry(100, 100, 1000, 500)
    self.textBox.show()
    self.textBox.closeEvent = self.function

def function(self, e):
    print("test")

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2017-11-01
    • 1970-01-01
    • 1970-01-01
    • 2016-01-23
    • 1970-01-01
    • 2013-12-04
    • 2015-01-13
    相关资源
    最近更新 更多