Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to remove the area on the left of QTreeView first column?

How can I remove this area seen inside the red rectangle?

enter image description here

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 Qt6.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();
}
like image 828
Lillo Avatar asked Dec 17 '25 21:12

Lillo


1 Answers

This may not be a real answer but I have found where this "thing" is drawn.

qtreeview.cpp

style()->drawPrimitive(QStyle::PE_PanelItemViewRow, &opt, painter, this);

I have commented it and recompiled and its gone.

like image 155
Lillo Avatar answered Dec 19 '25 09:12

Lillo



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!