Pycharm:Python Qt代码代码补全 [英] Pycharm: Python Qt code code completion

查看:27
本文介绍了Pycharm:Python Qt代码代码补全的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是 Python 中 Qt 的初学者.

I am a beginner with Qt in Python.

我使用 Qt Designer 创建简单.

我需要什么 - 用户点击按钮后,应用程序将文本从编辑复制到标签.

What I need - after user click to button, app copy text from edit to label.

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

I have file example.ui from 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>

如何在我的 Python 代码中使用它?

How can I use it in my Python code?

我修改了一些教程中的代码,它可以工作:

I modify code from some tutorial and it works:

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

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

But code completion doesn't work! So it is unusable for me :-(

我正在使用 JetBrains Pycharm.

在 Python 代码中使用 Qt 设计器输出与 IDE 中的工作代码竞争的正确方法是什么?

What is right way to use Qt designer output in python code with working code competition in IDE?

推荐答案

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

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)

代替

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

self.pushButton.clicked.*

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

这篇关于Pycharm:Python Qt代码代码补全的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆