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

[SOLVED] Checkboxes in QListWidget appear as locked



  • Hi everybody,

    I'm quite new to Qt, with a few months of experience.

    I'm currently working with a project that contains a QListWidget, mainly because it's an easy solution to create lists of slightly more complex widgets. I'm trying to give each new list item a checkbox. In my current code, this is done by:

    @ListFrame *frame1 = new ListFrame(this);
    QListWidgetItem *item = new QListWidgetItem();
    item->setCheckState(Qt::Checked);
    item->setFlags(item->flags() | Qt::ItemIsUserCheckable);

    ui->listWidget->addItem(item);
    ui->listWidget->setItemWidget(item,frame1);@

    My ListFrame class is simply a frame with a number of labels in it.
    If I omit the "Qt::ItemIsUserCheckable" flag, the checkbox in my list is greyed out. With the flag, however, the checkbox is not greyed out, but it's still behaving as though it were disabled! I've tried clicking the frame that is my item, the labels that it contains, or the checkbox itself, and it never changes state. Note that this problem persists even if I use a standard class such as QLabel instead of my ListFrame.

    Have I misunderstood something? According to all other info I've found, it should be enough to add the flag ItemIsUserCheckable and set a checked or unchecked state.

    Thanks for any help!




  • Lifetime Qt Champion

    Hi,

    Great you found out and shared the solution !

    Can you also update the thread title prepending [solved] so other forum users may know a solution has been found :)



  • Oh, right. I was looking for a "Solved" button somewhere. Done! :D



  • Hi ,
    Working on similar issue and got the same problem.
    Help will be Appreciated.


  • Lifetime Qt Champion

    @sateesh
    Hi and welcome to the forums.
    Make sure you keep the default item flags when you set checkable

      QListWidgetItem* item = new QListWidgetItem("myitem", ui->listWidget);
        item->setFlags(item->flags() | Qt::ItemIsUserCheckable); // set the checkable flag
        item->setCheckState(Qt::Unchecked); // also initialize check state
    
    

    alt text



  • @mrjj

    Firstly, Thanks for the reply.
    sorry for not mentioning the problem clearly.

    I have an QListWidgetItem and need to make the text editable. so used QLineEdit as itemwidget.

    QLineEdit *lineEditStructureName = new QLineEdit();
    lineEditStructureName->setText(structureName);
    lineEditStructureName->setFocusPolicy(Qt::ClickFocus);
    //listWidgetItem->setSizeHint (lineEditStructureName->sizeHint ());
    this->ui->listWidgetStructures->setItemWidget (listWidgetItem, lineEditStructureName);

    Now the problem is Not getting accessed. as the links description its kind of locked.
    Is there anything I'm doing wrong ?


  • Lifetime Qt Champion

    @sateesh Set Qt::ItemIsEditable flag on your item.


Log in to reply