Pycharm: Python Qt code code completion

后端 未结 2 1233
梦谈多话
梦谈多话 2021-01-14 04:47

I am a beginner with Qt in Python.

I create simple from using Qt Designer.

\"enter

相关标签:
2条回答
  • 2021-01-14 05:04

    1) generate python code: pyuic4 -o mygui.py mygui.ui

    2) write code:

    import sys
    from PyQt4 import QtCore, QtGui
    from mygui import Ui_MainWindow
    
    class StartQT4(QtGui.QMainWindow):
        def __init__(self, parent=None):
            QtGui.QWidget.__init__(self, parent)
            self.ui = Ui_MainWindow()
            self.ui.setupUi(self)
            QtCore.QObject.connect(self.ui.pushButton, QtCore.SIGNAL("clicked()"), self.abc)
    
        def abc(self):
            input = self.ui.lineEdit.text()
            self.ui.label_2.setText(input)
    
    if __name__ == "__main__":
        app = QtGui.QApplication(sys.argv)
        myapp = StartQT4()
        myapp.show()
        sys.exit(app.exec_())
    

    It works, but is possible to write QtCore.QObject.connect(self.ui.pushButton, QtCore.SIGNAL("clicked()"), self.abc) simpler?

    0 讨论(0)
  • 2021-01-14 05:14

    Not a complete answer but surely mentionable: Code completion does not work for dynamic objects. You could of course still use

    self.pushButton.clicked.connect(self.abc)
    

    instead of

    QtCore.QObject.connect(self.ui.pushButton, QtCore.SIGNAL("clicked()"), self.abc)
    

    but there won't be any code completion for self.pushButton.clicked.*

    (This also answers the question https://stackoverflow.com/a/28270242/4537483 )

    0 讨论(0)
提交回复
热议问题