Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

How to redefine a node expanding in a QTreeWidget?



  • 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?


  • Qt Champions 2019

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



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

    connect(this, SIGNAL(expanded(QModelIndex)), this, SLOT(myExpand(QModelIndex)));
    

Log in to reply