PCharm, PyQt5 and code completion
-
wrote on 10 Jun 2023, 13:21 last edited by
I'm fairly new to Python and sorry if this might be a trivial question, but I've searched the net without success.
I created a form.ui with Qt Creator putting some components into it, for example lineEdit and horizontalSlider.
Now from my Python code I search and find the component withlineEdit = self.findChild(QtWidgets.QLineEdit, "lineEdit_2")
but when I type lineEdit followed by a dot the code completion opens but it is missing some functions, for example setText().
I know I can write it anyway and it works fine. But why is it not shown?
Also, in other parts of the code I interface with some QThreads through signals, and for example in this line:self.pushButton.clicked.connect(self.start_threads)
PCharm does not suggest connect and reports it as not found, even if after works.
When you're just starting out, it would be important to have all the tips right so you can experiment with new features.
Anyone have any suggestions? I've already tried several things, editor preferences settings, invalidated cache... but nothing has changed.Thanks
-
I'm fairly new to Python and sorry if this might be a trivial question, but I've searched the net without success.
I created a form.ui with Qt Creator putting some components into it, for example lineEdit and horizontalSlider.
Now from my Python code I search and find the component withlineEdit = self.findChild(QtWidgets.QLineEdit, "lineEdit_2")
but when I type lineEdit followed by a dot the code completion opens but it is missing some functions, for example setText().
I know I can write it anyway and it works fine. But why is it not shown?
Also, in other parts of the code I interface with some QThreads through signals, and for example in this line:self.pushButton.clicked.connect(self.start_threads)
PCharm does not suggest connect and reports it as not found, even if after works.
When you're just starting out, it would be important to have all the tips right so you can experiment with new features.
Anyone have any suggestions? I've already tried several things, editor preferences settings, invalidated cache... but nothing has changed.Thanks
wrote on 10 Jun 2023, 14:44 last edited by JonB 6 Oct 2023, 14:44@valterhome
In a word, to get a good editing experience in PyCharm you do not want to go down thefindChild(QtWidgets.QLineEdit)
route. You want to do what you would do from Qt Creator, where you run something likepyside-uic
on the.ui
file saved from Designer to generate a new.py
file from it. That will contain classes/variables for all your UI elements and allow PyCharm to determine their types for use when code completing. You want to use the Option A approach in https://doc.qt.io/qtforpython-6/tutorials/basictutorial/uifiles.html#option-a-generating-a-python-class.As for the
connect()
. ISTR PyCharm has an option where you tell it whether you are using PySide or PyQt (or nothing), have you found this? -
@valterhome
In a word, to get a good editing experience in PyCharm you do not want to go down thefindChild(QtWidgets.QLineEdit)
route. You want to do what you would do from Qt Creator, where you run something likepyside-uic
on the.ui
file saved from Designer to generate a new.py
file from it. That will contain classes/variables for all your UI elements and allow PyCharm to determine their types for use when code completing. You want to use the Option A approach in https://doc.qt.io/qtforpython-6/tutorials/basictutorial/uifiles.html#option-a-generating-a-python-class.As for the
connect()
. ISTR PyCharm has an option where you tell it whether you are using PySide or PyQt (or nothing), have you found this?wrote on 10 Jun 2023, 16:17 last edited by@JonB Sorry, i didn't say that, but i use pyqt5 and and it seems that the link you gave me refers to pyside. I actually have no preference between the two, but I tried installing pyside2 and PCharm raises an error.
As for the option to tell PCharm what library is in use, I couldn't find it. Do you have time to give me more specifics? -
@valterhome
In a word, to get a good editing experience in PyCharm you do not want to go down thefindChild(QtWidgets.QLineEdit)
route. You want to do what you would do from Qt Creator, where you run something likepyside-uic
on the.ui
file saved from Designer to generate a new.py
file from it. That will contain classes/variables for all your UI elements and allow PyCharm to determine their types for use when code completing. You want to use the Option A approach in https://doc.qt.io/qtforpython-6/tutorials/basictutorial/uifiles.html#option-a-generating-a-python-class.As for the
connect()
. ISTR PyCharm has an option where you tell it whether you are using PySide or PyQt (or nothing), have you found this?wrote on 10 Jun 2023, 19:28 last edited by@JonB Ok, thanks to your suggestion, I'm starting to understand something more.
Using Pyuic5 I converted the form.ui into a Python module and imported it into the application.
With some changes to the code, now the code completion works perfectly. Thank you!I'm just bored with those reports of "reference not found" on emit of line
self.signals.updateLineEdit.emit("text")
or unexpected argument on setRange of line
self.ui.graph_widget.setRange(yRange=[-40000, 40000 ])
I haven't really figured out how to set Python up like this.
-
@JonB Ok, thanks to your suggestion, I'm starting to understand something more.
Using Pyuic5 I converted the form.ui into a Python module and imported it into the application.
With some changes to the code, now the code completion works perfectly. Thank you!I'm just bored with those reports of "reference not found" on emit of line
self.signals.updateLineEdit.emit("text")
or unexpected argument on setRange of line
self.ui.graph_widget.setRange(yRange=[-40000, 40000 ])
I haven't really figured out how to set Python up like this.
wrote on 11 Jun 2023, 06:40 last edited by JonB 6 Nov 2023, 07:02@valterhome
I don't recall having any issue withemit
. I am slightly "surprised" to see yourself.signals.updateLineEdit.emit()
, I don't know whatself.signals
is, I would have expectedself.ui.updateLineEdit.emit()
. [OIC, I think this is a signal you have added.]I don't know what type your
graph_widget
to have asetRange(yRange=...)
. I don't see an inbuilt widget which has this.Have you tried Googling for
pycharm qt emit
as I would do for this? Does e.g. PyQt5 returnPressed.connect "Cannot find reference 'connect' in function" address this? You could post to PyCharm forum to ask. -
@valterhome
I don't recall having any issue withemit
. I am slightly "surprised" to see yourself.signals.updateLineEdit.emit()
, I don't know whatself.signals
is, I would have expectedself.ui.updateLineEdit.emit()
. [OIC, I think this is a signal you have added.]I don't know what type your
graph_widget
to have asetRange(yRange=...)
. I don't see an inbuilt widget which has this.Have you tried Googling for
pycharm qt emit
as I would do for this? Does e.g. PyQt5 returnPressed.connect "Cannot find reference 'connect' in function" address this? You could post to PyCharm forum to ask.wrote on 11 Jun 2023, 08:44 last edited by@JonB said in PCharm, PyQt5 and code completion:
@valterhome
I am slightly "surprised" to see yourself.signals.updateLineEdit.emit()
I'm still learning and experimenting, so I might make mistakes, even if the code seems to work fine. I use signals because I need to update lineEdits from a QThread, and I understand this was the way to go. I'll see if it's possible to optimize the code differently.
I don't know what type your
graph_widget
to have asetRange(yRange=...)
. I don't see an inbuilt widget which has this.You are right, I found this example on the net and implemented it without checking. I have now corrected it according to the official documentation. Strangely, the wrong instruction still set the Y-axis correctly without returning errors.
Have you tried Googling for
pycharm qt emit
as I would do for this? Does e.g. PyQt5 returnPressed.connect "Cannot find reference 'connect' in function" address this? You could post to PyCharm forum to ask.Thanks for the link, it helps.
1/6