Pycharm:Python Qt代码代码完成

mar*_*tin 1 python qt pyqt4 code-completion pycharm

我是Python Qt的初学者。

我可以使用Qt Designer创建简单的东西。

在此处输入图片说明

我需要的-用户单击按钮后,应用程序将文本从编辑复制到标签。

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

<?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>
Run Code Online (Sandbox Code Playgroud)

如何在我的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_()
Run Code Online (Sandbox Code Playgroud)

但是代码完成不起作用!所以对我来说是不可用的:-(

我正在使用JetBrains Pycharm

在IDE中工作代码竞争的情况下,在python代码中使用Qt设计器输出的正确方法是什么?

小智 5

这不是一个完整的答案,但肯定可以提及:代码补全不适用于动态对象。您当然仍然可以使用

self.pushButton.clicked.connect(self.abc)
Run Code Online (Sandbox Code Playgroud)

代替

QtCore.QObject.connect(self.ui.pushButton, QtCore.SIGNAL("clicked()"), self.abc)
Run Code Online (Sandbox Code Playgroud)

但不会有任何代码完成 self.pushButton.clicked.*

(这也回答了问题/sf/answers/1978916971/