Planned maintenance: From Sunday 8th December 10:00 CET there will be changes to try and solve the caching issues that have been experienced. If anyone has a problem connecting after this period then please PM @AndyS or any of the moderators.

Configuring checkbox when using QTreeWidget

  • Hello, I was trying to make something like this using PyQt 5.11.2 and Qt Designer.

    So I marked the "UserCheckable" flag to expect a checkbox next to items "XXX", "YYY", and "ZZZ".


    However, the checkboxes don't appear.
    Is it not possible to configure the items to have checkboxes with Qt Designer?
    Do I have to type the code "ItemIsUserCheckable" manually?


  • Lifetime Qt Champion


    After a quick test, it seems to you have to use setCheckState(Qt::Unchecked) or setCheckState(Qt::Checked) if you want to have the checkbox appearing in your GUI.

  • Thanks for your reply, SGaist.

    I wish to solve this using the Qt Designer.
    I'm using Qt Designer 5.9.1.
    Then, is this a bug or a known issue from Qt Designer?


  • Qt Champions 2018

    @MATTK said in Configuring checkbox when using QTreeWidget:

    is this a bug or a known issue from Qt Designer?

    No, it's 100% intended behaviour. the condition is checked here

    You have 2 options:

    • set the Qt::CheckStateRole for the indexes you want to have a checkbox. In your widget constructor you'd call something like setUnchecked(ui->treeWidget->model());
    void setUnchecked(QAbstractItemModel* model, const QModelIndex& parent = QModelIndex()){
        for(int i=0, maxRow=model->rowCount(parent);i<maxRow;++i){
            for(int j=0, maxCol=model->columnCount(parent);j<maxCol;++j){
                const QModelIndex currIdx = model->index(i,j,parent);
    • subclass the delegate to check the flag instead of Qt::CheckStateRole and call something like ui->treeWidget->setItemDelegate(new CheckFlagDelegate(this)); in your widget constructor
    class CheckFlagDelegate : public QStyledItemDelegate{
        explicit CheckFlagDelegate(QObject* parent = Q_NULLPTR) : QStyledItemDelegate(parent){}
        void initStyleOption(QStyleOptionViewItem *option, const QModelIndex &index) const Q_DECL_OVERRIDE{
            if(index.model()->flags(index) & Qt::ItemIsUserCheckable)
                option->features |= QStyleOptionViewItem::HasCheckIndicator;

    Since the functionality is defined in a QObject (the delegate) and not in a QWidget this is not something designer can and probably ever will manage