Creation of dynamic labels
I am trying to add QLabels to a QverticalLayout in Pyside2, which should update with the text of certain file names. I want to have dynamic widget creation i.e the widgets should get added as the number of file name increases.
I am having no idea how to implement the same. The widgets are also not shown in the Layout.
Code:
#label.setStyleSheet("QLabel { background-color : green; color : black; }"); // I tried the commented part of the code for the labels but no luck
#label = self.ui.verticalLayout.itemAt(index).widget()
label.setStyleSheet("QLabel { background-color : white; color : black; }");
label.setText=localFile
self.ui.verticalLayout.addWidget(label)
index=index+1
I am unable to get the label in the designated place.
Re: Creation of dynamic labels
Can anyone please help me with this?
Re: Creation of dynamic labels
You haven't posted enough code to understand what you are doing or why.
I am also curious, since you are setting your labels to white background and black text why you aren't just using a QListWidget for this? All you have to do is add or remove strings and the widget will take care of displaying them, adding scrollbars if needed, and so forth. And unlike a QLabel, you can click on one of the strings and get signals telling you that.
Re: Creation of dynamic labels
Hi,
First of all thanks for replying to the thread.
I am really new in Pyside2 , so I am making some mistakes here and there.
Thanks again for the QListWidget idea. I was unaware of it.
I am basically trying to create QLabels to populate some data.
Re: Creation of dynamic labels
Quote:
I am basically trying to create QLabels to populate some data.
Seems like you think that the only tool in your toolbox is a QLabel hammer and any time you want to display text, that's the tool your reach for.
Before reaching for the hammer, take some time to look at the many, many examples and tutorials in the Qt documentation, especially the Widgets examples. If you see something that looks good to you, look at the source code to see how they did it.
The mapping between the C++ and Pyside2 / PyQt5 APIs is very close, so you should be able to read the C++ code and understand it without too much trouble.
Re: Creation of dynamic labels
Yes I agree this hammer comes first to mind of novice like me.
I will go through the examples before deciding on widgets again , as QListWidget saved my day :)