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

Widget with checkbox as an editor for ItemDelegate derivative



  • Hello.
    I needed a centered checkbox for column in my QTableView.
    I followed this post and everything worked as soon as I realized that part had to be in .h file. My two problems are:

    • How to make this BooleanWidget appear not near upper left corner of my screen, but in the edited cell? Tried changing QWidget to QFrame, got nothing

    • How do I toggle editor's state just by 1 click? Can I override some 2 slots related to focus events? Like, onCellWithDelegateSelected to just auto toggle and onCellWithDelegateClickedOff to save the state into model?

    P.S. Header & .cpp



  • 0_1530016922136_Безымянный.png


  • Lifetime Qt Champion

    Hi
    Does it sort of become a window?
    That happens if no parent is given. ( or NULL)
    Try to check if you give it a null parent where you create it.

    The issue is that CheckBoxDelegate dont get a parent.
    I assume you do like:
    ui->listWidget->setItemDelegate( new CheckBoxDelegate());
    So
    You need to make its constructor take parent as normally.
    and give it parent.
    .h
    CheckBoxDelegate(QObject* parent);
    .cpp
    CheckBoxDelegate::CheckBoxDelegate(QObject *parent) : QItemDelegate(parent) {
    }

    and then
    ui->listWidget->setItemDelegate( new CheckBoxDelegate(this));

    Then profit:
    alt text



  • @mrjj thank you very much for advice, it sounds legit, and now when I check with qDebug it shows that parent isn't null. However! What you can see on previous screenshot still happens... I mean, it's still grossly misplaced, can you zip me a working code sample with QListView? Or QTableView? Here are my current .h/.cpp

    P.S.For some reason I can see neither 'Locals' nor 'Expressions' (the locals are ever-invisible & expressions are visible when you add them but always with blank (unavailable) values). Iе worked before and now just doesn't. I declined updating from 5.10.1 to 5.11


  • Lifetime Qt Champion

    @starryeyed
    Hi
    sure. here is test project.
    https://www.dropbox.com/s/qfa31g3cjoo0wlr/MyDelegateCombo.zip?dl=0
    its your code but i just added to mainwindow.h to test fast.

    After given it parent, it stays in the cells.


  • Lifetime Qt Champion

    About the expressions.
    Do you have this ON ?
    alt text



  • @mrjj I've found a way to do this without additional subclassing at all. Thanks for sample code! I am making a QButtonGroup of radio buttons using it.
    Here's how to center the box with a stylesheet

    QWidget* CheckBoxDelegate::createEditor(<...>)
        {
            QCheckBox *cb = new QCheckBox(parent);
            cb->setStyleSheet("QCheckBox {margin-left: 43%; margin-right: 57%;}");
            return cb;
        }
    

    P.S. Resetting that checkbox and restarting the IDE helped! Thanks for making those views work :)


  • Lifetime Qt Champion

    @starryeyed
    Clever margin trick :)
    btw i assume you know that QListWidget/all views can be set to be checkable?
    new_item->setFlags(new_item->flags() | Qt::ItemIsUserCheckable);

    but i assume you goal was to have it centered and hence the delegate.


Log in to reply