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. Populate a QTableWidget with QTreeViews
Forum Updated to NodeBB v4.3 + New Features

Populate a QTableWidget with QTreeViews

Scheduled Pinned Locked Moved Solved General and Desktop
qtreeviewqtablewidgetsetcellwidgetqvector
13 Posts 3 Posters 4.1k 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.
  • P Patou355
    2 Mar 2017, 12:08

    I have QTreeViews contained in a QVector and I want them to populate a QTableWidget using the method QTableWidget::setCellWidget:

    //create the TreeViews and their associate models
    int i;
    for (i = 0 ; i < treeViewAdresses.size() ; ++i) {
        forest << new QTreeView();
        models << new TreeModel(foo);
        forest[i]->setVisible(true);
        forest[i]->setModel(models.at(i));
        forest[i]->setDragEnabled(true);
        forest[i]->setAcceptDrops(true);
        _manageOrderedTable->setCellWidget(0, treeViewAdresses.at(i), forest[i]);
    }
    

    The TreeViews are successfully created but the problem is that they stay outside the table. See below:

    treeViewAdresses is a vector of integers containing the position (I checked, they are good)
    I tried to:

    • replace the forest[i] with forest.at(i).
    • tried to insert a simple QWidget in the table instead.
      Same problem in both cases.
    M Offline
    M Offline
    mrjj
    Lifetime Qt Champion
    wrote on 2 Mar 2017, 16:33 last edited by mrjj 3 Feb 2017, 16:34
    #3

    Oh
    When they become windows it means you did not give them a parent.
    So do that in constructor
    or via -> setParent

    new QTreeView( PARENTPTR );

    V 1 Reply Last reply 2 Mar 2017, 17:11
    0
    • M mrjj
      2 Mar 2017, 16:33

      Oh
      When they become windows it means you did not give them a parent.
      So do that in constructor
      or via -> setParent

      new QTreeView( PARENTPTR );

      V Offline
      V Offline
      VRonin
      wrote on 2 Mar 2017, 17:11 last edited by
      #4

      @mrjj Unfortunately setCellWidget takes ownership of the widget so it can't be because of the parent thing.

      very strange that even with a base QWidget does not work.

      Can you try with a clean project and see if it happens the same?

      "La mort n'est rien, mais vivre vaincu et sans gloire, c'est mourir tous les jours"
      ~Napoleon Bonaparte

      On a crusade to banish setIndexWidget() from the holy land of Qt

      M 1 Reply Last reply 2 Mar 2017, 17:21
      1
      • V VRonin
        2 Mar 2017, 17:11

        @mrjj Unfortunately setCellWidget takes ownership of the widget so it can't be because of the parent thing.

        very strange that even with a base QWidget does not work.

        Can you try with a clean project and see if it happens the same?

        M Offline
        M Offline
        mrjj
        Lifetime Qt Champion
        wrote on 2 Mar 2017, 17:21 last edited by
        #5

        @VRonin
        " setCellWidget takes ownership of the widget"
        Hi, thx for adding :)
        yes 100% agree,
        but what else would make them windows`?

        V 1 Reply Last reply 2 Mar 2017, 17:43
        0
        • M mrjj
          2 Mar 2017, 17:21

          @VRonin
          " setCellWidget takes ownership of the widget"
          Hi, thx for adding :)
          yes 100% agree,
          but what else would make them windows`?

          V Offline
          V Offline
          VRonin
          wrote on 2 Mar 2017, 17:43 last edited by
          #6

          @mrjj said in Populate a QTableWidget with QTreeViews:

          but what else would make them windows`?

          my only guess would be the cell(0, treeViewAdresses.at(i)) is null so since the cell does not exist the widget is not added. I'm just 10% confident on this one though

          "La mort n'est rien, mais vivre vaincu et sans gloire, c'est mourir tous les jours"
          ~Napoleon Bonaparte

          On a crusade to banish setIndexWidget() from the holy land of Qt

          M 1 Reply Last reply 2 Mar 2017, 18:13
          0
          • V VRonin
            2 Mar 2017, 17:43

            @mrjj said in Populate a QTableWidget with QTreeViews:

            but what else would make them windows`?

            my only guess would be the cell(0, treeViewAdresses.at(i)) is null so since the cell does not exist the widget is not added. I'm just 10% confident on this one though

            M Offline
            M Offline
            mrjj
            Lifetime Qt Champion
            wrote on 2 Mar 2017, 18:13 last edited by
            #7

            @VRonin
            I also have a 10% question ;)
            The at() returns a const ref
            Will it then be able to take ownership ?

            V 1 Reply Last reply 2 Mar 2017, 18:36
            0
            • M mrjj
              2 Mar 2017, 18:13

              @VRonin
              I also have a 10% question ;)
              The at() returns a const ref
              Will it then be able to take ownership ?

              V Offline
              V Offline
              VRonin
              wrote on 2 Mar 2017, 18:36 last edited by VRonin 3 Feb 2017, 18:36
              #8

              @mrjj I think the actual widget is forest[i] which should return a non-const ref.

              Anyway even at() will return a const reference to a non const pointer so it can take ownership as the content of the pointer is non const, just like QWidget* const

              "La mort n'est rien, mais vivre vaincu et sans gloire, c'est mourir tous les jours"
              ~Napoleon Bonaparte

              On a crusade to banish setIndexWidget() from the holy land of Qt

              M 1 Reply Last reply 2 Mar 2017, 18:52
              0
              • V VRonin
                2 Mar 2017, 18:36

                @mrjj I think the actual widget is forest[i] which should return a non-const ref.

                Anyway even at() will return a const reference to a non const pointer so it can take ownership as the content of the pointer is non const, just like QWidget* const

                M Offline
                M Offline
                mrjj
                Lifetime Qt Champion
                wrote on 2 Mar 2017, 18:52 last edited by mrjj 3 Feb 2017, 18:55
                #9

                @VRonin
                Yeah, just thinking out loud.

                Well test is always the best

                
                    for (int i = 0 ; i < 10 ; ++i) {
                        QTreeView* forest = new QTreeView(ui->tableWidget);
                        forest->setVisible(true);
                        ui->tableWidget->setCellWidget(i, 0, forest);
                    }
                

                This puts them inside

                But if you change
                QTreeView* forest = new QTreeView(ui->tableWidget);
                -->
                QTreeView* forest = new QTreeView();

                I get windows too.

                So it was the parent !?!?!

                runable test project
                https://www.dropbox.com/s/8l2an24l7vp0x0l/treesintable.zip?dl=0

                V 1 Reply Last reply 2 Mar 2017, 18:57
                1
                • M mrjj
                  2 Mar 2017, 18:52

                  @VRonin
                  Yeah, just thinking out loud.

                  Well test is always the best

                  
                      for (int i = 0 ; i < 10 ; ++i) {
                          QTreeView* forest = new QTreeView(ui->tableWidget);
                          forest->setVisible(true);
                          ui->tableWidget->setCellWidget(i, 0, forest);
                      }
                  

                  This puts them inside

                  But if you change
                  QTreeView* forest = new QTreeView(ui->tableWidget);
                  -->
                  QTreeView* forest = new QTreeView();

                  I get windows too.

                  So it was the parent !?!?!

                  runable test project
                  https://www.dropbox.com/s/8l2an24l7vp0x0l/treesintable.zip?dl=0

                  V Offline
                  V Offline
                  VRonin
                  wrote on 2 Mar 2017, 18:57 last edited by
                  #10

                  @mrjj Big up then. typical example of

                  It works... why?

                  Probably here it's the doc's fault as I suspect "ownership" doesn't mean parent-child ownership in this case

                  "La mort n'est rien, mais vivre vaincu et sans gloire, c'est mourir tous les jours"
                  ~Napoleon Bonaparte

                  On a crusade to banish setIndexWidget() from the holy land of Qt

                  M 1 Reply Last reply 2 Mar 2017, 19:03
                  1
                  • V VRonin
                    2 Mar 2017, 18:57

                    @mrjj Big up then. typical example of

                    It works... why?

                    Probably here it's the doc's fault as I suspect "ownership" doesn't mean parent-child ownership in this case

                    M Offline
                    M Offline
                    mrjj
                    Lifetime Qt Champion
                    wrote on 2 Mar 2017, 19:03 last edited by mrjj 3 Feb 2017, 19:09
                    #11

                    @VRonin
                    Hehe. And after that came the rule. If it works dont mess with it :)

                    Well normally when reparenting a widget, all win flags are stripped so it
                    can be inside. Maybe in this case that part is missing?
                    Using a QLabel just works.

                    M 1 Reply Last reply 2 Mar 2017, 19:22
                    0
                    • M mrjj
                      2 Mar 2017, 19:03

                      @VRonin
                      Hehe. And after that came the rule. If it works dont mess with it :)

                      Well normally when reparenting a widget, all win flags are stripped so it
                      can be inside. Maybe in this case that part is missing?
                      Using a QLabel just works.

                      M Offline
                      M Offline
                      mrjj
                      Lifetime Qt Champion
                      wrote on 2 Mar 2017, 19:22 last edited by
                      #12

                      @mrjj

                      Update:
                      Suddenly it works without setting parent. ( i rebooted. )

                      If i move show() to be last, its perfect as it wont show as window for a brief moment

                       for (int i = 0 ; i < 10 ; ++i) {
                              QTreeView* forest = new QTreeView();
                              ui->tableWidget->setCellWidget(i, 0, forest);
                              forest->setVisible(true);
                          }
                      

                      This suddenly works.

                      So my other test must be flawed and the parent is not important as
                      setCellWidget does as we think.

                      Meh :)

                      1 Reply Last reply
                      0
                      • P Offline
                        P Offline
                        Patou355
                        wrote on 3 Mar 2017, 12:45 last edited by Patou355 3 Mar 2017, 12:45
                        #13

                        Hello guys. Thanks for your answerS.
                        The "error" was simple, stupid. In fact the _manageOrderedTable is an instance of a class called ManageOrderedTable which inherits from QTableWidget.
                        In this class, there's a method called fillAll and I called setColumnCount() from there. Bad idea.
                        When I call this method from upon the for loop, everything is fine. I don't understand why.
                        I could understand if setColumnCount() were called too late, but in this case I should have a segfault, trying to call a cell that doesn't exist.
                        Well, I don't really understand why but now it works...

                        Tank you for your help, Patrick.

                        1 Reply Last reply
                        0

                        12/13

                        2 Mar 2017, 19:22

                        • Login

                        • Login or register to search.
                        12 out of 13
                        • First post
                          12/13
                          Last post
                        0
                        • Categories
                        • Recent
                        • Tags
                        • Popular
                        • Users
                        • Groups
                        • Search
                        • Get Qt Extensions
                        • Unsolved