pyqt5 GUI has alot of code lines
-
Hey everybody,
I Develop a GUI using pyqt5, is based on this structure::
from PyQt5 import QtCore, QtGui, QtWidgets class Ui_Dialog(object): def setupUi(self, Dialog): Dialog.setObjectName("Dialog") Dialog.resize(292, 98) self.widget = QtWidgets.QWidget(Dialog) self.widget.setGeometry(QtCore.QRect(40, 40, 217, 27)) self.widget.setObjectName("widget") self.horizontalLayout = QtWidgets.QHBoxLayout(self.widget) self.horizontalLayout.setContentsMargins(0, 0, 0, 0) self.horizontalLayout.setObjectName("horizontalLayout") self.label = QtWidgets.QLabel(self.widget) self.label.setObjectName("label") self.horizontalLayout.addWidget(self.label) self.lineEdit = QtWidgets.QLineEdit(self.widget) self.lineEdit.setObjectName("lineEdit") self.horizontalLayout.addWidget(self.lineEdit) self.retranslateUi(Dialog) QtCore.QMetaObject.connectSlotsByName(Dialog) def retranslateUi(self, Dialog): _translate = QtCore.QCoreApplication.translate Dialog.setWindowTitle(_translate("Dialog", "Dialog")) self.label.setText(_translate("Dialog", "TextLabel")) if __name__ == "__main__": import sys app = QtWidgets.QApplication(sys.argv) Dialog = QtWidgets.QDialog() ui = Ui_Dialog() ui.setupUi(Dialog) Dialog.show() sys.exit(app.exec_())
My application uses a lot of parameters, each parameter has Qlables, QLineEdits, few others use RadioButtons etc...
Eventually after generating all the relevant code line for each parameter i end up with a lot of code lines - the problem mainly interferes while trying to Debug through PyCharm - results with PyCharm work very slowly, and low memory notifications pops up.What is the right way to manage that many parameters being held by one Ui_MainWindow class?
-
@prophet_lolo
Just how many "parameters" --- whatever you mean by that --- i.e. labels, line edits etc. do you mean you have?I used PyCharm with PyQt5, and it would not matter (within reason) how many widgets you have, the debugger works fine. It should not result in "low memory notifications pops up". That would indicate you just don't have enough memory to reasonably run PyCharm to debug a Qt program, so how much (free) memory do you have? PyCharm itself, plus the debugger, will need a fair amount of memory, you will not be able to use it reasonably if you don't have a lot of free memory.
-
@JonB
I have around 480 parameters, each parameter has at minimum the following lines:self.horizontalLayout = QtWidgets.QHBoxLayout() self.horizontalLayout.setObjectName("horizontalLayout") self.qlabel = QtWidgets.QLabel() self.qlabel.setObjectName("qlabel") self.qlabel.setFixedSize(self.width, self.height) self.horizontalLayout.addWidget(self.qlabel) self._spacerItem = QtWidgets.QSpacerItem(20, 40, QtWidgets.QSizePolicy.Minimum, QtWidgets.QSizePolicy.Expanding) self.horizontalLayout.addItem(self._spacerItem) self.qlineEdit = QtWidgets.QLineEdit() self.qlineEdit.setObjectName("qlineEdit") self.qlineEdit.setFixedSize(self.source_qline_width, self.source_qline_height) self.horizontalLayout.addWidget(self.qlineEdit) self.vertical_layout.addLayout(self.horizontalLayout)
so only for the parameters, it sums to around 6240 lines, without additional functions...
-
@prophet_lolo Well, you can put code which is repeated again and again in functions and pass needed information as parameters. The usual thing one does in a programming language like Python.
-
@prophet_lolo
As per @jsulm, you can and should indeed do whatever shared-factoring on your code.However, this has nothing to do with 6,240 lines of code ought not have any great effect on PyCharm, and certainly should not result in "low memory notifications pops up". I asked you a couple of times how much free memory you have on your PC --- before and after entering PyCharm --- but you did not respond.
-
@JonB
Before running PyCharm:
2.4GB used out of 16.6GBAfter running PyCharm:
4.9GB used out of 16.6GBI notice also that when opening this pyqt5 GUI file in PyCharm, all the cores (8) goes up to between 90%-100% -> this happens only with this file, the moment i close it all the cores goes back to around 10%-20%.
-
@prophet_lolo
You have masses of free memory. So why are you getting "low memory notifications pops up".?I notice also that when opening this pyqt5 GUI file in PyCharm, all the cores (8) goes up to between 90%-100% -> this happens only with this file
Depressing :(
It is then unclear whether this behaviour is caused by the volume of 6K lines of code --- ought not be the case --- or by some particular lines in the file --- more likely. You should also be clear whether the difference about "this file" is that it is the only one which imports Qt stuff, or the amount of Qt stuff, if you mean that "other files" do no do any Qt/PyQt.
When I started on PyCharm there was really bad behaviour upon opening projects --- the IDE went off doing massive searching for imported & system files doing its "indexing", which it does. People complained and over time it did get better. BTW, if you load in this file and go away for a cup of coffee for, say, as much as half an hour, does it then settle down on its own?
At the moment your Qt/PyQt usage seems fine to me. 6K lines should not be enough to kill your PC. It looks like you have a PyCharm issue here.... (Make sure you are on the latest version of PyCharm, and check their forums etc. for any reports.)
-
Hey,
I tried to leave it open for a few minutes, and indeed the cores for back to normal after 2-4 minutes,. its probably as you said above, PyCharm doing massive searching over imported files and scanning the whole code.
If i open the same file with visual studio code, it does not happen, interesting.Thank you guys.
-
@prophet_lolo said in pyqt5 GUI has alot of code lines:
I tried to leave it open for a few minutes, and indeed the cores for back to normal after 2-4 minutes,.
Yes, and maybe it shows "Indexing" (or similar) on its status line at the bottom of the screen?
VS, and Qt Creator, are both "fast" on startup. PyCharm is, or can be, unbelievably slow, while it does this "indexing". It ought not need to do this every time one starts up, but it seems to do so. At least with older versions, and who knows what they have/have not corrected now. There are various posts on the web about "PyCharm startup/indexing slow", or similar.
-
@jsulm said in pyqt5 GUI has alot of code lines:
Well, you can put code which is repeated again and again in functions and pass needed information as parameters. The usual thing one does in a programming language like Python.
Sorry for posting an answer not for initial topic but this comment spotted my attention and flashed 'small red lamp' in my mind :)
The reason is when I started with PySide some time ago for a rather complex GUI window I found myself with a lot of similar declarations very fast. And without good Qt knowledge I went this way - wrapping it into functions, creating lists of linked lists of parameters... Indeed it had a result and I got stable code, but its structure was a bit hard to understand for "unprepared mind". After half a year with Qt I got an insight that everything I did may be done with proper subclassing. So I spent sometime and re-wrote it in classes - the complexity and stability remained but I found that now it is much easier to maintain and extend.
So, my advice would be to spend sometime and understand how Qt works and then go not with functions but with subclassing.And for the topic itself - I'm working with PyCharm and can't say it is slow. Yes, it makes indexing sometimes, but for me it happens for max 5 min in background and takes only share of CPU, I still able to do things. Maybe I should say thanks to SSD - I see this operation is disk expensive, maybe it matters.