Results 1 to 6 of 6

Thread: Creation of dynamic labels

  1. #1
    Join Date
    Aug 2019
    Posts
    15
    Thanks
    6
    Qt products
    Qt5
    Platforms
    Windows

    Default 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.

    Qt Code:
    1. label=QtWidgets.QLabel()
    2. #label.setStyleSheet("QLabel { background-color : green; color : black; }"); // I tried the commented part of the code for the labels but no luck
    3. #label = self.ui.verticalLayout.itemAt(index).widget()
    4. label.setStyleSheet("QLabel { background-color : white; color : black; }");
    5. label.setText=localFile
    6. self.ui.verticalLayout.addWidget(label)
    7. index=index+1
    To copy to clipboard, switch view to plain text mode 

    I am unable to get the label in the designated place.

  2. #2
    Join Date
    Aug 2019
    Posts
    15
    Thanks
    6
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: Creation of dynamic labels

    Can anyone please help me with this?

  3. #3
    Join Date
    Jan 2008
    Location
    Alameda, CA, USA
    Posts
    4,719
    Thanks
    259
    Thanked 760 Times in 750 Posts
    Qt products
    Qt5
    Platforms
    Windows Android

    Default 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.
    <=== The Great Pumpkin says ===>
    Please use CODE tags when posting source code so it is more readable. Click "Go Advanced" and then the "#" icon to insert the tags. Paste your code between them.

  4. #4
    Join Date
    Aug 2019
    Posts
    15
    Thanks
    6
    Qt products
    Qt5
    Platforms
    Windows

    Default 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.

  5. #5
    Join Date
    Jan 2008
    Location
    Alameda, CA, USA
    Posts
    4,719
    Thanks
    259
    Thanked 760 Times in 750 Posts
    Qt products
    Qt5
    Platforms
    Windows Android

    Default Re: Creation of dynamic labels

    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.
    <=== The Great Pumpkin says ===>
    Please use CODE tags when posting source code so it is more readable. Click "Go Advanced" and then the "#" icon to insert the tags. Paste your code between them.

  6. The following user says thank you to d_stranz for this useful post:

    supratik (20th October 2020)

  7. #6
    Join Date
    Aug 2019
    Posts
    15
    Thanks
    6
    Qt products
    Qt5
    Platforms
    Windows

    Default 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

Similar Threads

  1. Dynamic creation of Singleton
    By remizero in forum Qt Programming
    Replies: 7
    Last Post: 6th June 2016, 02:31
  2. Dynamic creation of Scale object
    By Mookie in forum Qt Quick
    Replies: 4
    Last Post: 28th March 2016, 16:13
  3. Dynamic creation of list<transform>
    By Mookie in forum Qt Quick
    Replies: 1
    Last Post: 27th March 2016, 16:26
  4. Dynamic creation of widget with N images
    By davethomaspilot in forum Qt Programming
    Replies: 1
    Last Post: 30th March 2014, 13:45
  5. Dynamic widget creation from an XML-like file
    By ttvo in forum Qt Programming
    Replies: 2
    Last Post: 1st June 2009, 23:15

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.