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

How to create a function to choose a specific Label from many and edit it



  • I am trying to come up with a code that will simulate the state of Arduino ports. I have 8 labels, one per bit. I want to create a function that will allow me to choose one of the labels specifically and set it to either high or low and display it in the mainwindow.


  • Lifetime Qt Champion

    @smolram What exactly is the problem? It is really not clear from your post.
    You already have 8 labels, right? Do you show them already in your UI?
    Or do you want to show one of these 8 labels? In this case why do you need 8 labels if you only show one?



  • @jsulm I already have the 8 labels created and shown in my UI. I am just trying to come up with a function the will edit one specific label out of the 8. Lets say I want to change Lable_3 to say High, so I want a function that will be able to choose Label_3 and set it to High. I know how to edit the labels I just don't know how to create a function to choose the label I want to and edit it. But I will be showing all 8 at the same time, but the function would be a quick way to choose one label and change the text.


  • Lifetime Qt Champion

    @smolram Well, write a method which takes an index (0-7). Put pointers to your labels into a vector:

    QVector<QLabel*> labels;
    ...
    labels[0] = Label_0;
    ...
    
    void MyClass::editLabel(int index)
    {
        // Check the index first - it should be in range 0..7
        labels[index]->...
    }
    

Log in to reply