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.4k 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

    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.
    mrjjM Offline
    mrjjM Offline
    mrjj
    Lifetime Qt Champion
    wrote on last edited by mrjj
    #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 );

    VRoninV 1 Reply Last reply
    0
    • mrjjM mrjj

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

      VRoninV Offline
      VRoninV Offline
      VRonin
      wrote on 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

      mrjjM 1 Reply Last reply
      1
      • VRoninV VRonin

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

        mrjjM Offline
        mrjjM Offline
        mrjj
        Lifetime Qt Champion
        wrote on 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`?

        VRoninV 1 Reply Last reply
        0
        • mrjjM mrjj

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

          VRoninV Offline
          VRoninV Offline
          VRonin
          wrote on 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

          mrjjM 1 Reply Last reply
          0
          • VRoninV VRonin

            @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

            mrjjM Offline
            mrjjM Offline
            mrjj
            Lifetime Qt Champion
            wrote on 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 ?

            VRoninV 1 Reply Last reply
            0
            • mrjjM mrjj

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

              VRoninV Offline
              VRoninV Offline
              VRonin
              wrote on last edited by VRonin
              #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

              mrjjM 1 Reply Last reply
              0
              • VRoninV VRonin

                @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

                mrjjM Offline
                mrjjM Offline
                mrjj
                Lifetime Qt Champion
                wrote on last edited by mrjj
                #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

                VRoninV 1 Reply Last reply
                1
                • mrjjM mrjj

                  @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

                  VRoninV Offline
                  VRoninV Offline
                  VRonin
                  wrote on 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

                  mrjjM 1 Reply Last reply
                  1
                  • VRoninV VRonin

                    @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

                    mrjjM Offline
                    mrjjM Offline
                    mrjj
                    Lifetime Qt Champion
                    wrote on last edited by mrjj
                    #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.

                    mrjjM 1 Reply Last reply
                    0
                    • mrjjM mrjj

                      @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.

                      mrjjM Offline
                      mrjjM Offline
                      mrjj
                      Lifetime Qt Champion
                      wrote on 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 last edited by Patou355
                        #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

                        • Login

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