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. (Beginner) Passing Treeview into function
Forum Updated to NodeBB v4.3 + New Features

(Beginner) Passing Treeview into function

Scheduled Pinned Locked Moved General and Desktop
5 Posts 3 Posters 1.3k Views 1 Watching
  • 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.
  • X Offline
    X Offline
    Xilanaz
    wrote on last edited by
    #1

    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]

    1 Reply Last reply
    0
    • Chris KawaC Offline
      Chris KawaC Offline
      Chris Kawa
      Lifetime Qt Champion
      wrote on last edited by
      #2

      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.

      1 Reply Last reply
      0
      • SGaistS Offline
        SGaistS Offline
        SGaist
        Lifetime Qt Champion
        wrote on last edited by
        #3

        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

        Interested in AI ? www.idiap.ch
        Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

        1 Reply Last reply
        0
        • X Offline
          X Offline
          Xilanaz
          wrote on last edited by
          #4

          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.

          1 Reply Last reply
          0
          • X Offline
            X Offline
            Xilanaz
            wrote on last edited by
            #5

            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 ;)

            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