QCheckBox easily editable on double click ?



  • Hi community !

    I am wondering if there is an easy way to make a QChechBox label editable like we can do with a list item by set setting a flag. I don't want to create a subClass.

    Thx


  • Lifetime Qt Champion

    Hi,

    No there's no easy way like that.



  • That's crap ... I don t understand why it's not a default feature on each widget that holds a label ...


  • Lifetime Qt Champion

    Because it's something that's really rarely needed


  • Moderators

    @Goffer, could you describe why you need this feature? Why does you want your user need to change a checkbox label?



  • Perhaps try something like adding a Checkbox without text in line with a QLineEdit, styled like a Label and then edit the line edit when double clicked.

    Just a suggestion, not tested.



  • Well I m creating a 'to do list' so when the user adds a task in his list (He can attach comments to his task) and then he realizes that his task doesn t have the correct name. Of course he could delete it and re create it but he would lose the attached comment so I want to be able to rename it by doubleClicking.

    I might try something like suggested by @TheBadger .

    @SGaist : because it would not be used ofter doesn t mean that it is useless ^^ and I think it would be very useful to be able to rename all widgets that contain a label.



  • @Goffer said:

    so I want to be able to rename it by doubleClicking

    You could also popup a UI that would allow the user to edit the field when you double click. It could also have the text larger (if that helped).



  • @dcape

    Yep I had that in mind as a last choice ^^ but it doesn't look very professional in my opinion


  • Lifetime Qt Champion

    Todo - List ? Looks like a job for QListView and a column of checkbox no ?


  • Moderators

    @SGaist said:

    Todo - List ? Looks like a job for QListView and a column of checkbox no ?

    Or a QListView plus a QListModel with checkable items.

    QCheckBox's main purpose is to let users choose from a set of predefined options. This is different from a Todo list, whose main purpose is to let users define their own items.

    @Goffer said:

    I think it would be very useful to be able to rename all widgets that contain a label.

    If you want, you can subscribe to the Development mailing list and present your idea to the Qt engineers. (Note: Personally, I don't think that all labelled widgets should allow editing -- programmers should choose the appropriate tool for their use case)



  • @SGaist I thought about that but i'm doing something more complex than a simple to do list. I have some more features that make the use of QListView non efficient as I would have to modify it too much.

    @JKSH In my case the checkBox is used to let the user choose from a set of defined options, finished/not finished and this option influences an other widget but still it also define an item... I guess I ll go for the popup Field that will let the user rename.

    Thx



  • @Goffer
    So I played around with the Idea and this is what I ended up with:

    Subclass QLineEdit as follow:

    class MyLabel : public QLineEdit {
        Q_OBJECT
    public:
        MyLabel(const QString& text, QWidget* parent = nullptr) 
            : QLineEdit(text, parent)
        {
            setReadOnly(true);
            setStyleSheet("QLineEdit:read-only {"
                                 "    border: none;"
                                 "    background: transparent; }"
                                 "QLineEdit {"
                                 "    background: white; }");
            connect(this, &QLineEdit::editingFinished, [this]{
                this->unsetCursor();
                this->setSelection(0,0);
                this->setReadOnly(true);});
        }
        virtual ~MyLabel() {}
    signals:
        void clicked();
    protected:
        void mousePressEvent(QMouseEvent *) { emit clicked(); }
        void mouseDoubleClickEvent(QMouseEvent *) {
            this->setReadOnly(false);
            this->selectAll();
        }
    };
    

    Then use as follow:

    
        QWidget* widget = new QWidget();
        QHBoxLayout* layout = new QHBoxLayout();
        widget->setLayout(layout);
        QCheckBox* checkbox = new QCheckBox();
        MyLabel* label = new MyLabel("Rename me");
        connect(label, &MyLabel::clicked, checkbox, &QCheckBox::toggle);
        layout->addWidget(checkbox);
        layout->addWidget(label);
        widget->show();
    

    Click the label or the checkbox to change the state. Double click the label to rename (you need c++11, if you can't, recode the lambda to be a member function). When renaming the label, press enter or on another control (switch focus) to finish editing the label (edit)

    That should do what you need, the only annoyance is that if you double click the label to edit it it also changes the state of the checkbox. But you can play around with this to get you started.

    PS: I did not comment the code, it should be self explanatory. If not just ask and I will gladly help

    Regards,


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.