Qt 6.3.1 QFileSystemModel TextAlignmentRole issue
-
My program subclasses QFileSystemModel to set the alignment for a data in a QTreeView. I have created a minimal program to show the issue. I am using a Mac Studio and the latest MacOS and XCode.
pro file:
QT += core gui widgets SOURCES += main.cpp qnx: target.path = /tmp/$${TARGET}/bin else: unix:!android: target.path = /opt/$${TARGET}/bin !isEmpty(target.path): INSTALLS += target
main.cpp
#include <QtWidgets> class FSModel : public QFileSystemModel { public: FSModel(QWidget *parent = nullptr) {} QVariant data(const QModelIndex &index, int role) const { if (role == Qt::TextAlignmentRole) return static_cast<QVariant>(Qt::AlignRight | Qt::AlignVCenter); return QFileSystemModel::data(index, role); } }; int main(int argc, char *argv[]) { QApplication a(argc, argv); QMainWindow mw; QTreeView tree; FSModel fsModel; fsModel.setRootPath(fsModel.myComputer().toString()); tree.setModel(&fsModel); tree.setStyleSheet("QTreeView::item{padding:10px;}"); tree.setFixedSize(500,60); mw.setCentralWidget(&tree); mw.show(); return a.exec(); }
The line
return static_cast<QVariant>(Qt::AlignRight | Qt::AlignVCenter);
works when compiled in any prior version of Qt, but combining horizontal and vertical alignments does not work in 6.3.1.
Result with 6.3.0
Result with 6.3.1
-
Hi,
I am surprised it works at all, you should not use static_cast, it's the wrong tool. You should properly create a QVariant for example using the fromValue method.