Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct
Make a label editable after clicking on it. How?
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!
Keith Cancel last edited by
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.
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.
giesbert last edited by
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...
To see if there are better alternatives could you tell us why are you trying to do this please?
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.
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).
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).
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.
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?
I agreed with Andre. That way you can have several groups under the same view (if you need them).
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!