3
votes

I am a beginner with Qt in Python.

I create simple from using Qt Designer.

enter image description here

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

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>

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 :-(

I'm using JetBrains Pycharm.

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

2
I am also using Pycharm for Python & PyQT developing, it works fine. When you mean auto-complete doesn't work, can you give some details? The PyQt module auto-complete failed or the UI part failed? - Cui Heng
OK, I got your point. You should not expect auto-complete when using "loadUiType",as "loadUiType" happens at runtime, but auto-complete depends on static analysis, so the correct way is you uses "pyuic" to generate the UI files into a .py file, then in your project, your import it and make use of it, then auto-complete will work - Cui Heng
Example: I want to use QLabel with name="label_2". In Pycharm I start writing "labe" and press ctrl+space and get no suggestions dl.dropboxusercontent.com/u/6943408/pyqt_completion_err.png - martin
Yes, see my last post, make use of the generated file by pyuic, instead of calling "loadUiType", then use setupUi to install the generated python file, in that case, the auto-complete will work fine! - Cui Heng
FYI, as pyuic will output to stdout as default, so you need to redirect the output to a python file, then import it. - Cui Heng

2 Answers

5
votes

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 )

1
votes

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?