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.
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();
}
1