Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Pycharm: Python Qt code code completion

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?

like image 674
martin Avatar asked Jul 27 '26 12:07

martin


2 Answers

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 )

like image 58
dayoda Avatar answered Jul 30 '26 02:07

dayoda


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?

like image 27
martin Avatar answered Jul 30 '26 02:07

martin



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!