QTableView with stylesheet – indicators overlap text in Qt6

We had to port code to Qt6, and one of the things that broke was the QTableView.

  • In Qt 5,15 and earlier, applying a style sheet with custom checkbox indicators worked fine.
  • In Qt 6.5.3, the custom image indicators overlapped over the default checkboxes and item text.
  • In Qt 6.6.3 and 6.7.2, the custom image indicators show fine, the default indicators no longer show, but the indicators are still overlapping the text.

It is definitely an improvement – but I don’t know how to fix the overlapping of text, hidden under the indicator. The only options that are easy are also bad:

  • adding lots of spaces (text wrapping would still be affected).
  • inserting the checkbox as a separate column (I tried, but the header is formatted using QHeaderView::section:horizontal:first, QHeaderView::section:horizontal:only-one which I had trouble figuring out for the subsequent columns).
  • I tried to compare Qt versions, but I don’t understand where the changes come from.
  • Currently, to have functionality, I ifdeffed out the style sheet. But it is desired by “stakeholders”, among other reasons for consistency between desktop (upgraded) and embedded (not upgraded and needing large custom boxes).

This is a super simple example showing the behavior, if somebody can help me until Qt fixes it completely. It behaves the same as the real program, the images cover the text.

main.cpp:

#include "mymodel.h"
#include <QApplication>
#include <QTableView>

int main(int argc, char *argv[])
{
    QApplication a(argc, argv);
    QTableView tableView;
    MyModel myModel;
    tableView.setModel(&myModel);

    QString viewStyle = "QTableView::item { padding: 10px; }n"
                        "QTableView::indicator { width: 30px; height: 30px; }n"
                        "QTableView::indicator:unchecked { image: url(:/checkbox_unselected.png); }n"
                        "QTableView::indicator:checked { image: url(:/checkbox_selected.png); } ";
    tableView.setStyleSheet(viewStyle);
    tableView.setColumnWidth(0, 100);
    tableView.setColumnWidth(1, 100);
    tableView.setColumnWidth(2, 100);
    tableView.setRowHeight(0, 100);
    tableView.setRowHeight(1, 100);

    tableView.show();
    return a.exec();
}

mymodel.h

#include <QAbstractTableModel>

class MyModel : public QAbstractTableModel
{
public:
    explicit MyModel(QObject *parent = nullptr);
    int rowCount(const QModelIndex &parent = QModelIndex()) const override;
    int columnCount(const QModelIndex &parent = QModelIndex()) const override;
    QVariant data(const QModelIndex &index, int role = Qt::DisplayRole) const override;
};

mymodel.cpp

#include "mymodel.h"

MyModel::MyModel(QObject *parent) : QAbstractTableModel(parent)  { }

int MyModel::rowCount(const QModelIndex & /*parent*/) const
{
    return 2;
}

int MyModel::columnCount(const QModelIndex & /*parent*/) const
{
    return 3;
}

QVariant MyModel::data(const QModelIndex &index, int role) const
{
    if (role == Qt::DisplayRole) {
        return QString("Row%1, Column%2").arg(index.row() + 1).arg(index.column() +1);
    }
    else if (role == Qt::CheckStateRole) {
        return (index.column() == 0) ? Qt::Checked : Qt::Unchecked;
    }
    return QVariant();
}

I reported this to Qt bug:

https://bugreports.qt.io/browse/QTBUG-129290

9

I suggest using Qt::DecorationRole to display the image, and do without setStyleSheet.
I slightly modified your example based on the example from the Qt model help:

#include <QApplication>
#include <QTableView>
#include <QStandardItemModel>
#include <QStyle>

class MyModel : public QStandardItemModel
{
private:
    QPixmap checkedPix, uncheckedPix;
    const int f=22;
public:
    explicit MyModel(QObject *parent = nullptr):QStandardItemModel(parent){
        checkedPix=qApp->style()->standardPixmap(QStyle::SP_DialogApplyButton).scaled(f,f, Qt::KeepAspectRatio);
        uncheckedPix=qApp->style()->standardPixmap(QStyle::SP_DialogCancelButton).scaled(f,f, Qt::KeepAspectRatio);
    }

    QVariant data(const QModelIndex &index, int role) const override
    {
        if(!index.isValid())
            return {};
        // Qt::DecorationRole
        if(role==Qt::DecorationRole){
            if(item(index.row(), index.column())->checkState()==Qt::Checked)
                return QVariant(checkedPix);
            else
                return QVariant(uncheckedPix);
        }
        else if(role==Qt::DisplayRole){
            return QVariant(item(index.row(), index.column())->text());
        }
        /*
        If you remove this "else if", the checkbox will not be 
        shown in the table cell, and its state can only be 
        controlled programmatically.
        */
        else if (role == Qt::CheckStateRole) {
            return item(index.row(), index.column())->checkState();
        }
        return {};
    }


    int rowCount(const QModelIndex & parent=QModelIndex()) const override
    {
        return 5;
    }


    int columnCount(const QModelIndex & parent=QModelIndex()) const override
    {
        return 1;
    }


    QVariant headerData(int section, Qt::Orientation orientation,
                                         int role) const override
    {
        if (role != Qt::DisplayRole)
            return {};

        if (orientation == Qt::Horizontal)
            return QStringLiteral("Column %1").arg(section+1);
        else
            return QStringLiteral("%1").arg(section+1);
    }


    Qt::ItemFlags flags(const QModelIndex &index) const
    {
        if (!index.isValid())
            return Qt::ItemIsEnabled;
        return QAbstractItemModel::flags(index) | (Qt::ItemIsEditable | Qt::ItemIsUserCheckable);
    }


    bool setData(const QModelIndex &index, const QVariant &value, int role)
    {
        if (index.isValid() && role == Qt::EditRole) {
            item(index.row(), index.column())->setText(value.toString());
            emit dataChanged(index, index, {role});
            return true;
        }
        else if(index.isValid() && role == Qt::CheckStateRole){
            Qt::CheckState st= (value.toInt()==0 ? Qt::Unchecked : Qt::Checked);

            item(index.row(), index.column())->setCheckState(st);
            emit dataChanged(index, index, {role});
            return true;
        }
        return false;
    }
};




int main(int argc, char *argv[])
{
    QApplication a(argc, argv);

    QTableView tableView;
    MyModel myModel;
    tableView.setModel(&myModel);

    for(int row=0; row<myModel.rowCount(); row++){
        auto item=new QStandardItem("item");
        item->setCheckState(row%2==0 ? Qt::Checked : Qt::Unchecked);
        myModel.setItem(row, 0, item);
    }

    tableView.show();
    return a.exec();
}

5

Thalia, among several options, I liked this very simple one.

#include <QApplication>
#include <QTableView>
#include <QStandardItemModel>
#include <QStyle>
#include <QItemDelegate>
#include <QPainter>

class Delegate : public QItemDelegate
{
private:
    QPixmap checkedPix, uncheckedPix;

public:
    Delegate(QObject *parent = nullptr):QItemDelegate(parent){
        checkedPix=qApp->style()->standardPixmap(QStyle::SP_DialogApplyButton);
        uncheckedPix=qApp->style()->standardPixmap(QStyle::SP_DialogCancelButton);
    }    

    void drawCheck(QPainter *painter,
                   const QStyleOptionViewItem &option,
                   const QRect &rect,
                   Qt::CheckState state) const{

        painter->drawPixmap(rect, state==Qt::Unchecked ? uncheckedPix:checkedPix);
    }
    
    /* you can also use: */ 

    // void drawDecoration(QPainter *painter, const QStyleOptionViewItem &option, const QRect &rect, const QPixmap &pixmap) const{
    //  cell pixmap
    // }

    // void drawDisplay(QPainter *painter, const QStyleOptionViewItem &option, const QRect &rect, const QString &text) const{
    //  cell text
    // }

    // void drawBackground(QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index) const{
    //  cell background
    // }
};


int main(int argc, char *argv[])
{
    QApplication a(argc, argv);
    QTableView tableView;
    QStandardItemModel myModel;
    tableView.setModel(&myModel);
    myModel.setRowCount(5);
    myModel.setColumnCount(1);
    tableView.setStyleSheet(" QTableView::indicator { width: 30px; height: 30px; }");

    tableView.setItemDelegateForColumn(0, new Delegate);//setItemDelegate, setItemDelegateForRow

    for(int row=0; row<myModel.rowCount(); row++){
        auto item=new QStandardItem("item");
        item->setCheckable(true);
        item->setCheckState(row%2==0 ? Qt::Checked : Qt::Unchecked);
        myModel.setItem(row, 0, item);
    }    

    tableView.show();
    return a.exec();
}

1

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật