Pycharm:Python Qt代码完成

时间:2015-02-02 01:15:36

标签: python qt pyqt4 pycharm code-completion

我是Python的Qt初学者。

我使用 Qt Designer 创建简单。

enter image description here

我需要什么 - 在用户点击按钮后,app将文本从编辑复制到标签。

我有来自Qt Designer的文件example.ui

<?xml version="1.0" encoding="UTF-8"?>
<ui version="4.0">
 <class>MainWindow</class>
 <widget class="QMainWindow" name="MainWindow">
  <property name="geometry">
   <rect>
    <x>0</x>
    <y>0</y>
    <width>308</width>
    <height>143</height>
   </rect>
  </property>
  <property name="windowTitle">
   <string>MainWindow</string>
  </property>
  <widget class="QWidget" name="centralwidget">
   <widget class="QLabel" name="label">
    <property name="geometry">
     <rect>
      <x>10</x>
      <y>20</y>
      <width>121</width>
      <height>17</height>
     </rect>
    </property>
    <property name="text">
     <string>Enter name</string>
    </property>
   </widget>
   <widget class="QLineEdit" name="lineEdit">
    <property name="geometry">
     <rect>
      <x>100</x>
      <y>20</y>
      <width>113</width>
      <height>27</height>
     </rect>
    </property>
   </widget>
   <widget class="QPushButton" name="pushButton">
    <property name="geometry">
     <rect>
      <x>80</x>
      <y>60</y>
      <width>85</width>
      <height>27</height>
     </rect>
    </property>
    <property name="text">
     <string>Display</string>
    </property>
   </widget>
   <widget class="QLabel" name="label_2">
    <property name="geometry">
     <rect>
      <x>10</x>
      <y>90</y>
      <width>261</width>
      <height>21</height>
     </rect>
    </property>
    <property name="font">
     <font>
      <weight>75</weight>
      <bold>true</bold>
     </font>
    </property>
    <property name="text">
     <string>TextLabel</string>
    </property>
   </widget>
  </widget>
  <widget class="QStatusBar" name="statusbar"/>
 </widget>
 <resources/>
 <connections/>
</ui>

如何在Python代码中使用它?

我从一些教程中修改代码并且它可以工作:

import sys
from PyQt4 import QtCore, QtGui, uic

form_class = uic.loadUiType("example.ui")[0] 

class MyWindowClass(QtGui.QMainWindow, form_class):
    def __init__(self, parent=None):
        QtGui.QMainWindow.__init__(self, parent)
        self.setupUi(self)
        self.pushButton.clicked.connect(self.pushButton_clicked) 

    def pushButton_clicked(self):
        input = self.lineEdit.text()
        self.label_2.setText(input)


app = QtGui.QApplication(sys.argv)
myWindow = MyWindowClass(None)
myWindow.show()
app.exec_()

但代码完成不起作用!所以它对我来说无法使用: - (

我正在使用 JetBrains Pycharm

在IDE中使用Qt设计器输出以及在IDE中进行代码竞争的正确方法是什么?

2 个答案:

答案 0 :(得分:4)

不是一个完整的答案,但肯定会提到:代码完成不适用于动态对象。你当然可以使用

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

而不是

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

self.pushButton.clicked.*

不会有任何代码完成

(这也回答了问题https://stackoverflow.com/a/28270242/4537483

答案 1 :(得分:1)

1)生成python代码:pyuic4 -o mygui.py mygui.ui

2)编写代码:

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_())

它有效,但可以写QtCore.QObject.connect(self.ui.pushButton, QtCore.SIGNAL("clicked()"), self.abc)更简单吗?

相关问题