Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Make a label editable after clicking on it. How?
Qt 6.11 is out! See what's new in the release blog

Make a label editable after clicking on it. How?

Scheduled Pinned Locked Moved General and Desktop
12 Posts 5 Posters 18.5k Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • B Offline
    B Offline
    Bekos
    wrote on last edited by
    #1

    Hello people!
    I would like your suggestion/opinion about something.
    What is the right way (if there is one) to do the following?: I want to have a label and when the user clicks on the text of the label it should become editable. Then, when the user presses enter or changes the focus, the label should stop being editable.
    How would you implement such thing? I was thinking to have one QLabel and one QLineEdit. Both at the same position. When the user clicks the label, it should become invisible, and the QLineEdit should appear. Then, when user is done editing by hitting enter or by changing focus to an other widget the QLineEdit should dissapear and QLabel appear. Is this a good solution? Do you have to suggest some better alternative way of doing this? I was also thinking to use a QListWidget with only one QWidgetItem and some clever CSS but it sounds very "hacky" to me. Sorry if I am asking something very silly, I don't have a lot of experience with Qt.
    Thank you in advance!

    1 Reply Last reply
    0
    • K Offline
      K Offline
      Keith Cancel
      wrote on last edited by
      #2

      I would say first the clicking a label to modify could be received foreign to some users unless you have some way of making it seem intuitive a logical to the end-user. Like having a button that reads modify values and changes all labels to text boxes or a smaller different colored label next the label or button that reads edit... or modify... and if the user clicks it have it pop open an edit dialog where the user can modify the value and click OK or Cancel. Lastly, for idea if they mouse over label have cursor change to something that lets the user know if the click it they can edit it. I would just make sure the GUI make sense to the user. I do not see anything wrong with you method, but I am fairly new at this as well, and maybe you could your own custom widget that is perhaps more fluid such as fade into a text edit box, and fade out to a label.

      ♫♪♪^。^ Always Whistling A Tune!

      1 Reply Last reply
      0
      • A Offline
        A Offline
        andre
        wrote on last edited by
        #3

        I concur with Keith, UI wise it seems a bit weird.

        Still, I think there are solutions. One way might be to use an actual QLineEdit the whole time, but use some clever CSS to style the line edit to look like a label when it does not have focus.

        1 Reply Last reply
        0
        • G Offline
          G Offline
          giesbert
          wrote on last edited by
          #4

          Another solution would be to use a QStackedWidget with a label and a QLIneEdit. Clicking on the label changes the visible widget and ending editing also. You could create a custom widget out of this.

          But for me, it also seams strange for a UI, I would normally never click on a label...

          Nokia Certified Qt Specialist.
          Programming Is Like Sex: One mistake and you have to support it for the rest of your life. (Michael Sinz)

          1 Reply Last reply
          0
          • Z Offline
            Z Offline
            ZapB
            wrote on last edited by
            #5

            To see if there are better alternatives could you tell us why are you trying to do this please?

            Nokia Certified Qt Specialist
            Interested in hearing about Qt related work

            1 Reply Last reply
            0
            • B Offline
              B Offline
              Bekos
              wrote on last edited by
              #6

              Thank you guys for all your answers! I will try multiple solutions to see what fits best (and if I can actually implement them :P).
              @ZapB: I have a OpenGL application with a dock widget. On the dock widget I have a QListWidget called state list. Each item of the list represents a state of the application. I can add,remove and modify states. So, I want to have a Title for the state list and be able to change/edit it.
              Thanks again!

              1 Reply Last reply
              0
              • Z Offline
                Z Offline
                ZapB
                wrote on last edited by
                #7

                Can you not just use editable items in your QListWidget then? The view paints the items but then when you trigger the editing mode, the delegate creates a suitable editor widget for you (QLineEdit in the case of simple text).

                Nokia Certified Qt Specialist
                Interested in hearing about Qt related work

                1 Reply Last reply
                0
                • A Offline
                  A Offline
                  andre
                  wrote on last edited by
                  #8

                  If you have a QListWidget already, then you don't need to make editable labels. The items in an item view (which QListWidget is) are already editable if you just set the right flags in the model (in your case in the QListWidgetItem).

                  1 Reply Last reply
                  0
                  • B Offline
                    B Offline
                    Bekos
                    wrote on last edited by
                    #9

                    Thanks for the replies guys. I am sorry I didn't describe my problem very good. This editable label i want to create is for the entire list. (it is the title of the list) Of course when I want to rename a single state I edit the QListWidgetItems. The editable label I want is for the whole thing.

                    1 Reply Last reply
                    0
                    • A Offline
                      A Offline
                      andre
                      wrote on last edited by
                      #10

                      Just a thought: wouldn't it make sense to use a QTreeWidget instead, and then instead of using a separate label, use a root item that you can rename instead?

                      1 Reply Last reply
                      0
                      • Z Offline
                        Z Offline
                        ZapB
                        wrote on last edited by
                        #11

                        I agreed with Andre. That way you can have several groups under the same view (if you need them).

                        Nokia Certified Qt Specialist
                        Interested in hearing about Qt related work

                        1 Reply Last reply
                        0
                        • B Offline
                          B Offline
                          Bekos
                          wrote on last edited by
                          #12

                          Sorry for the delayed response. Oh yeah. I see your point and you are indeed right. Unfortunately, I can not do that at the moment because I will need to change a lot of stuff and I am not the only one working on this project. But I will consider this on the next update of our application. For now, as Andre suggested, I inherit from QLineEdit my own class and I do some stylesheet "magic". It actually works fine. But yet, I find the QTreeWidget solution a better idea. I will give it a go soon!
                          Thank you all guys!
                          Cheers!

                          1 Reply Last reply
                          0

                          • Login

                          • Login or register to search.
                          • First post
                            Last post
                          0
                          • Categories
                          • Recent
                          • Tags
                          • Popular
                          • Users
                          • Groups
                          • Search
                          • Get Qt Extensions
                          • Unsolved