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 redefine a node expanding in a QTreeWidget?

How to redefine a node expanding in a QTreeWidget?

Scheduled Pinned Locked Moved Solved General and Desktop
3 Posts 2 Posters 463 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.
  • F Offline
    F Offline
    flammmable
    wrote on last edited by
    #1

    As I can understand the qtreeview.cpp the expand method in the QTreeView is responce for expanding nodes. For example it used in the expandOrCollapseItemAtPos method. I try to redefine a node expantion in the QTreeWidget:

    #include <QApplication>
    #include <QWidget>
    #include <QTreeWidget>
    #include <QMessageBox>
    
    class MyTree : public QTreeWidget
    {
    public:
        MyTree(QWidget *parent) : QTreeWidget(parent) {}
        expandItem(const QTreeWidgetItem *item) {
            QMessageBox msg;
            msg.setText("EXPAND ITEM!!");
            msg.exec();
            QTreeWidget::expandItem(item);
        }
        expand(const QModelIndex &index) {
            QMessageBox msg;
            msg.setText("EXPAND!!");
            msg.exec();
            QTreeWidget::expand(index);
        }
    };
    
    int main(int argc, char *argv[])
    {
        QApplication a(argc, argv);
    
        QWidget *mainWidget = new QWidget();
        mainWidget->resize(200,100);
    
        MyTree *myTree = new MyTree(mainWidget);
        myTree->resize(200,100);
    
        QTreeWidgetItem *node, *leaf;
        node = new QTreeWidgetItem(myTree);
        node->setText(0,"node");
        leaf = new QTreeWidgetItem(node);
        leaf->setText(0,"leaf");
    
        mainWidget->show();
        return a.exec();
    }
    

    But there is no any message box when I expand a node. I try to comment QTreeWidget::expandItem(item); and QTreeWidget::expand(index); and expanding still working. How to redefine a node expanding in a QTreeWidget?

    1 Reply Last reply
    0
    • Christian EhrlicherC Online
      Christian EhrlicherC Online
      Christian Ehrlicher
      Lifetime Qt Champion
      wrote on last edited by
      #2

      I don't see where you call expandItem() at all. And since QTreeWidget::expandItem() is not virtual you can't overwrite this function.

      Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
      Visit the Qt Academy at https://academy.qt.io/catalog

      1 Reply Last reply
      3
      • F Offline
        F Offline
        flammmable
        wrote on last edited by
        #3

        Thank you! I will use a slot-signal mechanism:

        connect(this, SIGNAL(expanded(QModelIndex)), this, SLOT(myExpand(QModelIndex)));
        
        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