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. QTreeWidget with column span
Forum Updated to NodeBB v4.3 + New Features

QTreeWidget with column span

Scheduled Pinned Locked Moved Unsolved General and Desktop
1 Posts 1 Posters 222 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.
  • M Offline
    M Offline
    markus0211
    wrote on last edited by
    #1

    Hi,

    I am trying to create a tree widget with column span. I am using the approach from here: https://stackoverflow.com/questions/26739541/how-to-span-the-columns-of-a-qtreeview-in-c-qt

    So far this is working as expected. However, there is a problem with columns where the resize mode is set to QHeaderView::Stretch. When I resize the window the text in the colum is somehow flickering.

    I prepared a simple example:

    #include <QTreeWidget>
    #include <QStyledItemDelegate>
    
    enum CustomRoles {
        ColumnSpanRole = Qt::UserRole + 1,
        PropertyRole,
    };
    
    class TreeWidgetItemDelegate : public QStyledItemDelegate {
        Q_OBJECT
    public:
        TreeWidgetItemDelegate(QObject* pParent = nullptr) : QStyledItemDelegate(pParent) {
            m_pTreeView = dynamic_cast<QTreeView*>(parent());
        }
        ~TreeWidgetItemDelegate() override = default;
    
        void paint(QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index) const override {
            QStyleOptionViewItem opt = option;
    
            if (index.data(ColumnSpanRole).isValid() && index.data(ColumnSpanRole).canConvert<int>()) {
                auto col_span = index.data(ColumnSpanRole).toInt();
    
                if (col_span == 0)
                    return;
    
                int w = option.rect.width();
                for (auto i = 1; i < col_span; i++)
                    w += m_pTreeView->columnWidth(index.column() + i);
    
                opt.rect.setWidth(w);
            }
    
            QStyledItemDelegate::paint(painter, opt, index);
        }
    
    private:
        QTreeView* m_pTreeView = nullptr;
    };
    
    class TreeWidgetItem : public QTreeWidgetItem {
    public:
        TreeWidgetItem(QTreeWidget* pTreeWidget, const QStringList &strings = {}) : QTreeWidgetItem(pTreeWidget, strings) {}
        TreeWidgetItem(QTreeWidgetItem* pParent, const QStringList &strings = {}) : QTreeWidgetItem(pParent, strings) {}
        ~TreeWidgetItem() override = default;
    
        void setText(int column, const QString &text, int column_span = 1) {
            setData(column, ColumnSpanRole, column_span);
            for (auto i = 1; i < column_span; i++)
                setData(column + i, ColumnSpanRole, 0);
            QTreeWidgetItem::setText(column, text);
        }
    };
    
    class TreeWidget : public QTreeWidget {
        Q_OBJECT
    public:
        TreeWidget(QWidget* pParent = nullptr) : QTreeWidget(pParent) {
            setItemDelegate(new TreeWidgetItemDelegate(this));
        }
        ~TreeWidget() override = default;
    };
    
    int main(int argc, char *argv[])
    {
        QApplication a(argc, argv);
    
        TreeWidget treeWidget;
        treeWidget.setColumnCount(5);
    
        treeWidget.header()->setSectionResizeMode(QHeaderView::Fixed);
    
        treeWidget.setColumnWidth(0, 100);
        treeWidget.setColumnWidth(1, 100);
        treeWidget.header()->setSectionResizeMode(2, QHeaderView::Stretch);
        treeWidget.setColumnWidth(3, 100);
        treeWidget.header()->setSectionResizeMode(4, QHeaderView::Stretch);
    
        TreeWidgetItem item(&treeWidget);
        item.setText(0, "Column 1");
        item.setText(1, "Column 1 & Column 2", 2);
        item.setText(3, "Column 3");
        item.setText(4, "Column 4");
    
        treeWidget.show();
    
        return a.exec();
    }
    

    Maybe someone can look into the code and see if I have done something wrong.
    I am using Qt version 6.7.2.

    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