0

I create applications using **QtDesigner **by loading .ui files.

Everything works but why there are no suggestions when writing code for clicked.connect(some_def) e.g.:

I have python 3.11.3 and PyCharm IDE installed.

enter image description here

code example:

`from PyQt5 import QtWidgets, uic from PyQt5.QtWidgets import QMainWindow, QAbstractButton import sys

class Ui(QMainWindow): def init(self): super(Ui, self).init() uic.loadUi('gui.ui', self)

    #self.button = QtWidgets.QPushButton("&Print", self)
    self.button = self.findChild(QtWidgets.QPushButton, 'printButton')
    self.button.clicked.connect(self.printButtonPressed)
    self.show()

def printButtonPressed(self):
    print('printButtonPressed')

app = QtWidgets.QApplication(sys.argv) window = Ui() app.exec_()`

I tested on python 3.10.11 and python 3.8.10 but the suggestions still don't work.

  • It seems pretty obvious that pycharm wouldn't be able to recognise objects defined in a ui file (which is xml) that is loaded as runtime. If you want auto-completion, you can use [pyuic](https://www.riverbankcomputing.com/static/Docs/PyQt5/designer.html#pyuic5) to create static python modules instead. – ekhumoro Apr 17 '23 at 21:11
  • Hi So I have to convert ".ui" to ".py", load the MainWindow class and it should work. I will check. – Kacper10714 Apr 18 '23 at 15:40

2 Answers2

0

Not sure but it could be because of the flag on def printButtonPressed(self): It could also be a problem with the text editor, you would have to look into it as I'm not experienced with editors outside of vscode.

0

Problem solved. I'm need to convert ".ui' to ".py" and load class Ui_MainWindow.

Example working code (suggestions worked fine).

` from PyQt5 import QtWidgets, uic from PyQt5.QtWidgets import QMainWindow, QAbstractButton import sys from gui import Ui_MainWindow

class Ui(QMainWindow): def init(self): super(Ui, self).init() # uic.loadUi('gui.ui', self)

    self.ui = Ui_MainWindow()
    self.ui.setupUi(self)

    self.ui.printButton.clicked.connect(self.printButtonPressed)

    self.show()

def printButtonPressed(self):
    print('printButtonPressed')

app = QtWidgets.QApplication(sys.argv) window = Ui() app.exec_() `