How to remove the area on the left of QTreeView first column?
-
How can I remove this area seen inside the red rectangle?
Things I have tried:
- setIndentation(0);
- setRootIsDecorated(false);
- overriding drawBranches
- Styling it with QTreeView::branch and setting width: 0px and also background-color: transparent;
I'm using Qt 6.6.
I dont know what this area from, as indentation is set to 0 what else could be this?Setting the first column hidden also hides this area but i would need to use everything like column()+1 it would be a mess.
class MyTreeView : public QTreeView { public: MyTreeView(QWidget* parent = nullptr) : QTreeView(parent) { QStandardItemModel* model = new QStandardItemModel(this); setModel(model); setObjectName("treeView"); setIndentation(0); setRootIsDecorated(false); for (int i =0; i< 2; i++) { QList<QStandardItem*> row; for (int j = 0; j < 2; j++) { row.append(new QStandardItem(QString::number(j))); } model->appendRow(row); } setStyleSheet(R"( QTreeView { outline: 0px; } QTreeView::item { min-height: 32px; border: 2px solid rgba(222, 222, 222, 255); border-radius: 12px; padding-left: 10px; } QTreeView::item:hover { background-color: rgba(177, 177, 177, 255); border: 2px solid rgba(177, 177, 177, 255); } QTreeView::item:selected { background-color: rgba(157, 157, 157, 255); border: 2px solid rgba(177, 177, 177, 255); } QTreeView::branch { background-color: transparent; border: 0px; width: 0px; } )"); } void drawBranches(QPainter* painter, const QRect& rect, const QModelIndex& index) const override { //QTreeView::drawBranches(painter, rect, index); } void drawRow(QPainter* painter, const QStyleOptionViewItem& option, const QModelIndex& index) const override { QStyleOptionViewItem opt = option; if (index.column() == 0) opt.rect.moveLeft(-10); QTreeView::drawRow(painter, opt, index); } }; int main(int argc, char* argv[]) { QApplication a(argc, argv); QWidget w; QHBoxLayout layout(&w); MyTreeView myTreeview; layout.addWidget(&myTreeview); w.show(); return a.exec(); }
-
Use another style. The windows11 style is using this as the indicator for the current row
-
@Christian-Ehrlicher I'm testing this on Windows 10, do you know where in the source this indicator is drawn?
-
In the windows 11 style code.
-
@Lailla said in How to remove the area on the left of QTreeView first column?:
How helpful
What's wrong?
windows11 style is in qwindows11style.cpp and then look for itemview. You will find two places (PE_PanelItemViewRow and CE_ItemViewItem) - somewhere there it is drawn. -
AFAIK the Windows 11 style (AKA "modern windows style") only works on Windows 11 proper - and then only starting Qt 6.7. Qt apps running on Windows 10 still use the older Qt Windows Vista style. However this particular bit might be the same in both, not sure, never could read QStyle code all that well.
@Lailla to see if the style does matter, try running your app with different styles by running the EXE with a
-style
flag. You can try theFusion
andwindows
(Windows 9.x) styles, they are built-in. -
AFAIK the Windows 11 style (AKA "modern windows style") only works on Windows 11 proper - and then only starting Qt 6.7. Qt apps running on Windows 10 still use the older Qt Windows Vista style. However this particular bit might be the same in both, not sure, never could read QStyle code all that well.
@Lailla to see if the style does matter, try running your app with different styles by running the EXE with a
-style
flag. You can try theFusion
andwindows
(Windows 9.x) styles, they are built-in.@IgKh said in How to remove the area on the left of QTreeView first column?:
nd then only starting Qt 6.7. Qt apps running on Windows 10 still use the older Qt Windows Vista style. However this particular bit might be the same in both, not sure, never could read QStyle code all that well.
No
You can use the windows11 style on windows 10 without any problems. But you have to set it explicitly.
-
@IgKh i tried the styles you have mentioned, on all of them this thing is drawn.
windows11 style is in qwindows11style.cpp and then look for itemview. You will find two places (PE_PanelItemViewRow and CE_ItemViewItem) - somewhere there it is drawn.
I have found where " the indicator for the current row" is drawn.
qtreeview.cpp
style()->drawPrimitive(QStyle::PE_PanelItemViewRow, &opt, painter, this);
I have tested commenting this line and recompiling and its gone.
I'm not relying on any built-in/default style(s).
I'll leave the topic open to see if someone else knows another way to hide/disable it. -
@IgKh i tried the styles you have mentioned, on all of them this thing is drawn.
windows11 style is in qwindows11style.cpp and then look for itemview. You will find two places (PE_PanelItemViewRow and CE_ItemViewItem) - somewhere there it is drawn.
I have found where " the indicator for the current row" is drawn.
qtreeview.cpp
style()->drawPrimitive(QStyle::PE_PanelItemViewRow, &opt, painter, this);
I have tested commenting this line and recompiling and its gone.
I'm not relying on any built-in/default style(s).
I'll leave the topic open to see if someone else knows another way to hide/disable it.@Lailla said in How to remove the area on the left of QTreeView first column?:
I'll leave the topic open to see if someone else knows another way to hide/disable it.
As you already wrote it's this line - so how should it be disabled by any other way than removing this line or using another style?? there is no condition for this line so there is no other way.
-
@IgKh i tried the styles you have mentioned, on all of them this thing is drawn.
windows11 style is in qwindows11style.cpp and then look for itemview. You will find two places (PE_PanelItemViewRow and CE_ItemViewItem) - somewhere there it is drawn.
I have found where " the indicator for the current row" is drawn.
qtreeview.cpp
style()->drawPrimitive(QStyle::PE_PanelItemViewRow, &opt, painter, this);
I have tested commenting this line and recompiling and its gone.
I'm not relying on any built-in/default style(s).
I'll leave the topic open to see if someone else knows another way to hide/disable it.@Lailla said in How to remove the area on the left of QTreeView first column?:
I'm not relying on any built-in/default style(s).
I'll leave the topic open to see if someone else knows another way to hide/disable it.Another way would be your own custom
QStyle
... then it can be switch at run time...
(before you start modifying your Qt source build)Maybe at some point you might find this indicator thing helpful again :)
Or your software is linked by some customer/user with another default version of Qt... then it looks like before again
(unless you have Qt statically linked to your app) -
@IgKh said in How to remove the area on the left of QTreeView first column?:
nd then only starting Qt 6.7. Qt apps running on Windows 10 still use the older Qt Windows Vista style. However this particular bit might be the same in both, not sure, never could read QStyle code all that well.
No
You can use the windows11 style on windows 10 without any problems. But you have to set it explicitly.
@Christian-Ehrlicher said in How to remove the area on the left of QTreeView first column?:
No
You can use the windows11 style on windows 10 without any problems. But you have to set it explicitly.
Indeed this is true now! In my defense, I'll say that this is the case only from 6.8.1, was not mentioned in the changelog for that version and the in-code documentation still claims it doesn't work on Windows 10.