Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

How to generate a new label each time a button is pressed?



  • I would like to show the updated result of my dictionary. Currently, I think the way to show value of the dictionary is by set text on a label... is there a way I can generate a new label each time a button is pressed. Or is there another way that doesn't use text label that I should use?



  • @ndkt

    is there a way I can generate a new label each time a button is pressed.

    Yes. In your slot (just like anywhere else) you can create widgets dynamically by newLabel = QLabel() or similar, and then add the new widget wherever appropriate somewhere in your widget hierarchy.

    Or is there another way that doesn't use text label that I should use?

    You could represent these via individual QLabels, and add those onto, say, a QVerticalLayout. However, there are many other ways, perhaps better suited. You could, say, add them onto a QTableWidget via QTableWidgetItem(const QString &text), or use a QGridLayout which allows the adding of rows/columns, or you might pick a QListWidget to store/show them as a list in a single widget, which would be my first inclination. Especially if there are going to be a large number of items, you don't want to create a large number of individual QLabels, speed & memory.


Log in to reply