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. QTableView blank rows when hiding elements
Forum Updated to NodeBB v4.3 + New Features

QTableView blank rows when hiding elements

Scheduled Pinned Locked Moved Solved General and Desktop
qtableview
7 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.
  • R Offline
    R Offline
    RekTekk249
    wrote on last edited by
    #1

    My QTableView has a few hundred elements. However, I only want some of them shown when a certain checkbox is selected. So, I have a method that hides and shows certain rows, based on said checkboxes. However, even though I only end seeing about 10 elements, I see hundreds of empty rows. The scrollbar to the right is very small due to this. Another problem is, when I scroll to the very bottom, I get an exception:

    ASSERT failure in QBitArray::QBitArray: "Size must be greater than or equal to 0"
    

    I'm not sure what caused this, since I have no code anywhere that touches the scrolling, not even close. All I'm doing is populating the QTableView, then hiding/showing based on the boxes. Note that checking the "everything" checkbox, supposed to show everything, still contains blank lines at the end, so it might not be due to hiding. Here are some relevant lines creating and populating as well as hiding the rows.

    model = new QStandardItemModel(0, 6);
    model -> setColumnCount(7);
    ui->boxTableView->setItemDelegate(new FormattingStyledItemDelegate(this));
    ui->boxTableView->setModel(model);
    ui -> boxTableView -> horizontalHeader() -> setSectionResizeMode(QHeaderView::Stretch);
    model -> setHorizontalHeaderLabels(QStringList() << "1" << "2" << "3" << "4" << "5" << "6" << "7");
    foreach(Item i, items){
        model -> appendRow(QList < QStandardItem * > () <<
                        new QStandardItem(item.one) <<
                        new QStandardItem(item.two) <<
                        new QStandardItem(item.three) <<
                        new QStandardItem(item.four) <<
                        new QStandardItem(item.five) <<
                        new QStandardItem(item.six) <<
                        new QStandardItem(item.seven));
    }
    

    Then the hiding:

    //Reset everything
    for (int i = 0; i < modelSize; i++) {
        ui->boxTableView->hideRow(i);
    }
    //One for each checkbox
    for (int i = 0; i < modelSize ; i++) {
        if(ui->checkBoxOne->isChecked()){
            ui->boxTableView->showRow(i);
        }
    }
    //...
    

    Screenshots:
    eab4e90a-9436-48d8-ac01-1648bbd16bf5-image.png

    c68dbc92-7ac7-440a-b110-987df9950f81-image.png

    Thanks in advance.

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

      Hi,

      What happens if you do not use your delegate ?

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

      R 1 Reply Last reply
      0
      • SGaistS SGaist

        Hi,

        What happens if you do not use your delegate ?

        R Offline
        R Offline
        RekTekk249
        wrote on last edited by
        #3

        @SGaist Nothing changes. My delegate only has a custom displayText() to display the numbers in a certain format.

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

          Which version of Qt is it ?
          Can you provide a minimal compilable example that shows that issue ?

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

          R 1 Reply Last reply
          0
          • SGaistS SGaist

            Which version of Qt is it ?
            Can you provide a minimal compilable example that shows that issue ?

            R Offline
            R Offline
            RekTekk249
            wrote on last edited by RekTekk249
            #5

            @SGaist Qt 6.0.2 using MSVC 2019

            Of course!

            Here it is:

            void MyClass::Init() {
                QStandardItemModel *model;
                QFutureWatcher<void> watcher;
                model = new QStandardItemModel(0, 1);
                model -> setColumnCount(1);
                ui->boxTableView->setModel(model);
                model -> setHorizontalHeaderLabels(QStringList() << "ex");
            
                connect( & watcher, & QFutureWatcher < void > ::finished, [ = ]() {
                    ui->boxTableView->setHidden(false);
                });
                QFuture < void > future = QtConcurrent::run([ = ]() {
                    for (int i = 0; i < 30 ; i++) {
                        model -> appendRow(QList < QStandardItem * > () <<
                            new QStandardItem("testdata"));
                    }
                });
                watcher.setFuture(future);
            }
            

            I basically removed everything, but it was adding the QFuture/Watcher that made it act like this. This reproduces both bugs, the extra lines and the crash when scrolling. The crash doesn't occur in Release, but the line bug does.

            1 Reply Last reply
            0
            • VRoninV Offline
              VRoninV Offline
              VRonin
              wrote on last edited by
              #6
              QtConcurrent::run([ = ]() {
                      for (int i = 0; i < 30 ; i++) {
                          model -> appendRow(QList < QStandardItem * > () <<
                              new QStandardItem("testdata"));
                      }
              

              This is a race condition, the model belongs to the main thread, you can't just edit it in a secondary thread like it's nothing.

              "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

              R 1 Reply Last reply
              2
              • VRoninV VRonin
                QtConcurrent::run([ = ]() {
                        for (int i = 0; i < 30 ; i++) {
                            model -> appendRow(QList < QStandardItem * > () <<
                                new QStandardItem("testdata"));
                        }
                

                This is a race condition, the model belongs to the main thread, you can't just edit it in a secondary thread like it's nothing.

                R Offline
                R Offline
                RekTekk249
                wrote on last edited by
                #7

                @VRonin You're right, that was it... I am quite new to concurrency, so I thank you for your insights. I ended up loading the rows in the thread and adding them in the finished slot instead, which fixed both problems.

                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