(Beginner) Passing Treeview into function



  • hi,

    I know my way around Pascal and been dabbling a bit in c++ using QT creator, trying to convert one of my old programs but there is one thing I can't seem to figure out, so gone try to explain it using my pascal code.

    I got a form with 2 treeviews and both can be filled with the same function so I have this:

    @
    function Fillnodes(ATreeview:TTreeview) : Boolean;
    var
    tv: TTreeview;
    begin
    TV:=TTreeview(ATreeview);
    @

    and I can start populating TV.

    I call that function simply with Fillnodes(Treeview1); or Fillnodes(Treeview2); which are the 2 existing treeviews on the form.

    But somehow I can't get this behaviour in C++/Qt5, I probably am missing something simple:

    @
    void Fillnodes(QTreeView ATreeView);

    void MainWindow::Fillnodes(QTreeView ATreeView)
    @

    so far no errors but I can't call Fillnodes for the life of me, how to pass the treeview1 and 2 from the form ?

    thanks for any insights.

    -Xil

    [edit: added code tags - Chris Kawa]


  • Moderators

    Hi, welcome to devnet.

    Yeah, this is a classic c++ migrate struggle - pointers :)

    What you're doing there is passing a function parameter by value, which basically means you are creating a copy. Most Qt types, including everything derived from QWidget or QObject is non-copyable.

    What you need to do is pass either by reference or a pointer:
    @
    //by reference:
    void MainWindow::fillNodes(QTreeView& aTreeView) {
    //access members via .
    aTreeView.doSomething(...
    }

    //or by a pointer:
    void MainWindow::fillNodes(QTreeView* aTreeView) {
    //access members via ->
    aTreeView->doSomething(...
    }
    @

    But to be honest this is not the usual way to "fill" tree views. You should read about Qt's "Model/View":https://qt-project.org/doc/qt-5-snapshot/model-view-programming.html architecture. You would create a single data model and attach it to two different views.

    Also I know Pascal is case insensitive, but C++ is not, and variable and function names are more commonly started with a lower case letter, where upper case usually indicates a class name or a constant.


  • Lifetime Qt Champion

    Hi and welcome to devnet,

    I'm surprised that you don't have any errors, you can't copy QObject derived class and that's what you are currently doing with that function signature.

    However, since you are using a QTreeView, you should have a model set to it. So you should be populating the model and not the QTreeView. You would then need to pass a pointer to the model you'd like to fill to your function.

    Here's a "tutorial":http://doc.qt.io/qt-5/modelview.html from Qt's documentation to help you get started.

    Since you are a C++ beginner, you should also get a good book about it. C++ is a language were it's pretty easy to shoot yourself in the feet if you start by trial and error.

    Hope it helps



  • thank you Chris.

    Pointers, I used to avoid them ;) I will read the link you send but still its good to know how to pass objects, or rather the pointer to them around.



  • SGaist, thanks. just got an error when calling the function. not declaring the function. Yes got c++ books but they a bit dry so I started to do some fun stuff and went faster then the book ;)


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.