PDA

View Full Version : changing name of a CheckBox by 2 time left clicking



Alex22
2nd December 2015, 10:27
Hi

thre is a Check Box (object of QChecjBox). I want to change it's name and set my desired name by typing.
for example i have this:


QCheckBox ch;
ch.setText("hi");
ch.show();

now i need when "ch" is showing, change it's name by clicking 2 times on it. I think I must create SIGNAL and SLOT for this.
Thanks for any help

Vikram.Saralaya
2nd December 2015, 10:55
You can add a QLineEdit next to QCheckbox (without text) and that can handle text editing.

Alex22
2nd December 2015, 11:17
@Vikram, thanks for your answer
but I need the name be static (like a Label) and by double clicking on it, it will be editable. if I put a QLineEdit, it will be allways editable. Am I right?

Vikram.Saralaya
2nd December 2015, 12:02
The reason its not that trivial is because check box is not intuitively a GUI element whose text can be edited.

But if you really need then there are quite a few alternatives like using a QStackedWidget and showing QLabel Vs QLineEdit on mouse click. You can refer to this for more options: https://forum.qt.io/topic/5004/make-a-label-editable-after-clicking-on-it-how/3

Alex22
2nd December 2015, 13:55
I created my own MyCheckBox class and using a PushButton for switching to edit mode that appears LineEdit.



#include "mycheckbox.h"

MyCheckBox::MyCheckBox(QWidget *parent) : QCheckBox(parent)
{

}

void MyCheckBox::setname(QString d) // my new slot
{
setText(d);
}


main.cpp code:

QWidget wdg;
QLineEdit le;
MyCheckBox ch;
QGridLayout lay(&wdg);
lay.addWidget(&ch,0,0);
lay.addWidget(&le,0,1);
QObject::connect(&le, SIGNAL(textChanged(QString)), &ch, SLOT(setname(QString)));


wdg.show();

any other idea?