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

Add Data with a QPushButton to QTreeView



  • Hi,

    I have two QTreeViews. The first one is filled with Data. Now I want to select data from this view and add it by clicking on a QPushButton to a second QTreeView.

    I know that I have to connect the QPushButton to a method that is adding the data. My problem is that I don't know how the method should look like.

    Thanks for help.



  • Simply connect your pusshButton to the slot:
    @
    ....
    connect(this->btn_copy, SIGNAL(clicked()), this, SLOT(copy_clicked()));
    ...
    @

    in this slot just copy item from one treeview to another:
    @
    void YourClass::copy_clicked()
    {
    this->tree_view_right->addItem(this->tree_view_left->itemAt(this->tree_vew_left->currentIndex()));
    }
    @



  • Thanks for your reply.

    I connected my button and wrote the copy_clicked() method but when I start my program I get this error:

    Object::connect: No such slot MyClass::copy_clicked()
    Object::connect: (sender name: 'btn_copy')
    Object::connect: (receiver name: 'Dialog')

    What did I do wrong?



  • Weird, I placed the method in front of the connect command and the errors don't appear anymore. BUT nothing else is happening.



  • have you declared your slot in class declaration? Like:
    @
    private slots:
    void copy_clicked();
    @



  • Have you read about signals and slots already? "Signals and slots":http://doc.qt.digia.com/qt/signalsandslots.html



  • I'm a beginner with Qt and programming with Python and PyQt. Therefore it's a bit complicated to rethink the c++ code in python and how to do everything right.

    Because I became desperate with this problem I posted it here and thought that I can solve this problem. I'll read the "Signals and slots" article. Thanks.



  • I tried something else. And now by starting the program it says

    'QTreeView' object has no attribute addItem

    Do I have to implement the addItem() method by myself?



  • Yes, addItem was just from my head... use right method to insert element to the treeview...



  • If you use QTreeWidget instead of QTreeView this method will already exist... (addItem, insertItem, etc...)


Log in to reply