K
K
KerroViT2020-07-31 20:35:24
Python
KerroViT, 2020-07-31 20:35:24

How to write a "generic" function to work with QLineEdit with different objectName?

I made a GUI for an application with several tabs (Tab)
On several Tabs there is a LineEdit with correspondingly different objectName (like qlineedit_tab1, qlineedit_tab2, etc.) and also buttons for selecting a file
. We need a "universal" function for each of these LineEdit

Now we have this:

def browse_file(self):
    file = QtWidgets.QFileDialog.getOpenFileName(self)
    self.qlineedit_tab1.setText(file[0])


Everything works, but the main problem is that now it is bound to a specific objectName (qlineedit_tab1). I don't feel like writing almost the same function for each objectName.

How can this problem be solved?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
T
Ternick, 2020-07-31
@Ternick

You can pass qlineedit_tab there and it will shove text from there, you just need to make it global, preferably or transfer it to another class, but these are OOP subtleties.
To put it simply, you can take the function out of the class and you get something like this:

def browse_file(qlineedit_tab):
    file = QtWidgets.QFileDialog.getOpenFileName(self)
    qlineedit_tab.setText(file[0])

But you need to make sure that you pass objects of the qlineedit_tab class to the browse_file function and nothing else. There is a built-in function of some kind for such things, the name flew out of my head ...

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question