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. Not capturing hidden rows in QTableWidget
Forum Updated to NodeBB v4.3 + New Features

Not capturing hidden rows in QTableWidget

Scheduled Pinned Locked Moved Solved General and Desktop
12 Posts 3 Posters 1.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.
  • L leinad

    Hi,
    I have a method that looks at a QTableWidget and copies its contents to a clipboard which I can use later for a c/p function. The method works fine but if I hide certain rows in the table, the method still reads them and copies them to the clipboard. Below is the code. Would anyone know how I can check if a row is hidden and not copy it to the clipboard?

    Thank you!

    void copyTextFromTableWidgetToClipBoard()
    {
    QAbstractItemModel *tableModel = tableWidget_->model();
    QItemSelectionModel *selectionModel = tableWidget_->selectionModel();
    QModelIndexList selectedCells = selectionModel->selectedIndexes();

    std::sort(selectedCells.begin(), selectedCells.end()); 
    
    if(selectedCells.size() < 1)
        return;
    
    QString copy_table;
    QModelIndex previous = selectedCells.first();
    
    selectedCells.removeFirst();
    
    for (int cell = 0; cell < selectedCells.size(); cell++)
    {
        QVariant data = tableModel->data(previous);
        QString text = data.toString();
    
        QModelIndex index = selectedCells.at(cell);
        copy_table.append(text);
    
        if(index.row() != previous.row())
        {
            copy_table.append('\n');
        }
        else
        {
            copy_table.append('\t');
        }
    
        previous = index;
    }
    
    copy_table.append(tableModel->data(selectedCells.last()).toString());
    copy_table.append('\n');
    
    QClipboard *clipboard = QApplication::clipboard();
    clipboard->setText(copy_table);
    

    }

    JonBJ Online
    JonBJ Online
    JonB
    wrote on last edited by JonB
    #2

    @leinad
    And how do you hide the rows? QTableView::setRowHidden()? Which has a corresponding QTableView::isRowHidden()?

    If you have selected cells, as per your code, I have no idea but did you test whether any hidden cells are even included in those selected? I think I would have expected not.

    1 Reply Last reply
    0
    • L Offline
      L Offline
      leinad
      wrote on last edited by
      #3

      Thank you and yes, but I use QModelIndex. How can I convert that to get the row number?

      JonBJ 1 Reply Last reply
      0
      • L leinad

        Thank you and yes, but I use QModelIndex. How can I convert that to get the row number?

        JonBJ Online
        JonBJ Online
        JonB
        wrote on last edited by JonB
        #4

        @leinad
        bool QTableView::isIndexHidden(const QModelIndex &index)?
        bool QTableView::isRowHidden(int row) passing index.row()?

        But did you verify whether your selectedCells() actually includes hidden rows/cells anyway?

        1 Reply Last reply
        0
        • L Offline
          L Offline
          leinad
          wrote on last edited by
          #5

          Yes, selected cells do include hidden rows which I was unaware that it did.
          I was going to try this:
          QModelIndex rowNumber;
          QModelIndexList selectedCells = selectionModel->selectedIndexes();
          for (int cell = 0; cell < selectedCells.size(); cell++)
          {
          rowNumber = selectedCells[cell];
          if(ui->tableWidget->isRowHidden(rowNumber.row()))
          continue;
          }

          1 Reply Last reply
          0
          • L Offline
            L Offline
            leinad
            wrote on last edited by
            #6

            You are addressing the question with QTableView but I'm using QTableWidget.

            JonBJ 1 Reply Last reply
            0
            • L leinad

              You are addressing the question with QTableView but I'm using QTableWidget.

              JonBJ Online
              JonBJ Online
              JonB
              wrote on last edited by
              #7

              @leinad said in Not capturing hidden rows in QTableWidget:

              You are addressing the question with QTableView but I'm using QTableWidget.

              QTableWidget Class

              Inherits: QTableView

              And in any case I previously asked you:

              And how do you hide the rows? QTableView::setRowHidden()? Which has a corresponding QTableView::isRowHidden()?

              So why not answer that? Together with

              But did you verify whether your selectedCells() actually includes hidden rows/cells anyway?

              1 Reply Last reply
              1
              • L Offline
                L Offline
                leinad
                wrote on last edited by
                #8

                Yes, I use
                ui->tableWidget->setRowHidden( row, true );

                JonBJ C 2 Replies Last reply
                0
                • L leinad

                  Yes, I use
                  ui->tableWidget->setRowHidden( row, true );

                  JonBJ Online
                  JonBJ Online
                  JonB
                  wrote on last edited by
                  #9

                  @leinad
                  But if you go to the QTableWidget Class page it does not have its own setRowHidden(), does it? It inherits QTableView::setRowHidden().

                  In any case the answers are above.

                  1 Reply Last reply
                  0
                  • L leinad

                    Yes, I use
                    ui->tableWidget->setRowHidden( row, true );

                    C Offline
                    C Offline
                    CPPUIX
                    wrote on last edited by CPPUIX
                    #10

                    @leinad Just to verify, print out the contents of the row when you hide it, and when you skip it.
                    here:
                    @leinad said in Not capturing hidden rows in QTableWidget:

                    ui->tableWidget->setRowHidden( row, true );

                    and here:

                    @leinad said in Not capturing hidden rows in QTableWidget:

                    if(ui->tableWidget->isRowHidden(rowNumber.row()))
                    continue;
                    }

                    Just to see if it's hiding and skipping the correct rows.

                    1 Reply Last reply
                    0
                    • L Offline
                      L Offline
                      leinad
                      wrote on last edited by
                      #11

                      I did q quick test and it appears to be working, but your suggest sounds good.

                      C 1 Reply Last reply
                      0
                      • L leinad

                        I did q quick test and it appears to be working, but your suggest sounds good.

                        C Offline
                        C Offline
                        CPPUIX
                        wrote on last edited by
                        #12

                        @leinad I'm trying to replicate what you have on my end, can I just fill my tableWidget with random text?

                        1 Reply Last reply
                        1
                        • L leinad has marked this topic as solved on

                        • Login

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