Populate a QTableWidget with QTreeViews



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

  • Qt Champions 2016

    Hi
    Sadly the image upload is broken and we cannot see it.
    Please use external site and post links here.


  • Qt Champions 2016

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



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


  • Qt Champions 2016

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



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


  • Qt Champions 2016

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



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


  • Qt Champions 2016

    @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



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


  • Qt Champions 2016

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


  • Qt Champions 2016

    @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 :)



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



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