Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. How to set viewport margins on a QTreeView?

How to set viewport margins on a QTreeView?

Scheduled Pinned Locked Moved Unsolved General and Desktop
1 Posts 1 Posters 79 Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • L Offline
    L Offline
    Lailla
    wrote on last edited by
    #1

    I'm trying to set a margin on the top side of a QTreeView viewport.

    I tried setViewportMargins but it doesn't change anything.

    I also tried adjust the painting rect directly at drawRow: opt.rect.moveTop(opt.rect.y()+20);

    It does added the space:

    QtWidgetsApplication1_3E4EkfEy5y.png

    But the view glitches, when an item is hovered it disappear, and clicking also doesnt seems correct.

    The same issue happens trying to modify the rect using QStyledItemDelegate.

    How i could set a top margin on the viewport of QTreeView?

    #define qq qDebug().noquote().nospace()
    
    class TreeDelegate : public QStyledItemDelegate
    {
    public:
        TreeDelegate(QObject* parent) : QStyledItemDelegate(parent) {}
    
    	void initStyleOption(QStyleOptionViewItem* option, const QModelIndex& index) const override
    	{
            //option->rect.moveTop(option->rect.y() + 20);
    		QStyledItemDelegate::initStyleOption(option, index);
    	}
    
        void paint(QPainter* painter, const QStyleOptionViewItem& option, const QModelIndex& index) const override
        {
            QStyledItemDelegate::paint(painter, option, index);
        }
    };
    
    class MyTreeView : public QTreeView
    {
    public:
        MyTreeView(QWidget* parent = nullptr) : QTreeView(parent)
        {
            QStandardItemModel* model = new QStandardItemModel(this);
            setModel(model);
            setIndentation(0);
            setRootIsDecorated(false);
      
            model->setHorizontalHeaderItem(0, new QStandardItem("col1"));
    		model->setHorizontalHeaderItem(1, new QStandardItem("col2"));
            for (int i =0; i< 22; i++)
            {
                QList<QStandardItem*> row;
                for (int j = 0; j < 2; j++)
                {
                    row.append(new QStandardItem(QString::number(i)));
                }
                model->appendRow(row);
            }
    
            setViewportMargins(0, 20, 0, 0);
            setItemDelegate(new TreeDelegate(this));
    
            connect(new QShortcut(QKeySequence(Qt::Key_F4), this), &QShortcut::activated, [&]
            {
    			setViewportMargins(0, 20, 0, 0);
            });
            connect(new QShortcut(QKeySequence(Qt::Key_F6), this), &QShortcut::activated, [&]
            {
                qq << "\nviewportMargins: " << viewportMargins();
            });
        }
    
        void drawRow(QPainter* painter, const QStyleOptionViewItem& option, const QModelIndex& index) const override
        {
            QStyleOptionViewItem opt = option;
    
    		opt.rect.moveTop(opt.rect.y()+20);
            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();
    }
    
    1 Reply Last reply
    0

    • Login

    • Login or register to search.
    • First post
      Last post
    0
    • Categories
    • Recent
    • Tags
    • Popular
    • Users
    • Groups
    • Search
    • Get Qt Extensions
    • Unsolved