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. Delete rows in the tableView
Forum Updated to NodeBB v4.3 + New Features

Delete rows in the tableView

Scheduled Pinned Locked Moved General and Desktop
7 Posts 3 Posters 6.5k 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
    Ruzik
    wrote on last edited by
    #1

    Hello? i have the code of deleting rows in the tableView, but it is work wrong
    What i do wrong?
    @ QList<QTableWidgetItem *> itemList = tableWidget->selectedItems();
    int i = itemList.count()/tableWidget->columnCount();
    int start = tableWidget->row(itemList[0]);
    for (int a=start;a<=start+i;a++)
    {
    tableWidget->removeRow(a);
    }@
    Sorry for the bad english and advance thanks for the help!

    1 Reply Last reply
    0
    • A Offline
      A Offline
      andre
      wrote on last edited by
      #2

      Your loop looks like a bit of a mess.
      I am guessing you want to remove all rows in which an item is selected, right?

      There are multiple ways to do this, and this is just one:

      Your first challenge is to find those rows. What you could do is this:

      @
      QSet<int> selectedRows; //we use a set to prevent doubles
      QList<QTableWidgetItem*> itemList = tableWidget->selectedItems();
      foreach(QTableWidgetItem item, itemList)
      selectedRows.insert(item.row());
      @

      Next step, is to actually remove them. Observe that you need to start removing from the bottom to the top, otherwise the row numbers of the rows that you still need to remove later will change, and that gets messy.

      @
      //get a list, and sort it big to small
      QList<int> rows = selectedRows.toList();
      qSort(rows.begin(), rows.end(), qGreater<int>);
      //now actually do the removing:
      foreach(int row, rows)
      tableWidget->removeRow(row);
      @

      1 Reply Last reply
      0
      • R Offline
        R Offline
        Ruzik
        wrote on last edited by
        #3

        I have 2 error when i use your code
        Error 2 error C2275: qGreater <T>: invalid use of this type as an expression c: \ Documents and Settings \ rustam \ My Documents \ Visual Studio 2008 \ Projects \ RizekActionManager \ RizekActionManager \ rizekactionmanager.cpp 253
        Error 1 error C2440: initialization: can not convert 'QTableWidgetItem * const' to 'QTableWidgetItem' c: \ Documents and Settings \ rustam \ My Documents \ Visual Studio 2008 \ Projects \ RizekActionManager \ RizekActionManager \ rizekactionmanager.cpp 249
        Maybe error occurs because the compiler(i use MSVS 2008)

        1 Reply Last reply
        0
        • A Offline
          A Offline
          andre
          wrote on last edited by
          #4

          No, it is very possible there are errors in the code above. I typed it directly in the forum, and I did not test it. The code was merely to illustrate a point, to make clear how to do what you want. I do not claim it will run unmodified.

          1 Reply Last reply
          0
          • R Offline
            R Offline
            Ruzik
            wrote on last edited by
            #5

            If use your code, it does not remove all the even elements
            I changed the code a bit to avoid mistakes, but I do not think that it is strongly influenced
            @ QSet<int> selectedRows; //we use a set to prevent doubles
            QList<QTableWidgetItem*> itemList = tableWidget->selectedItems();
            QTableWidgetItem * item;
            foreach(item, itemList)
            selectedRows.insert(item->row());
            //get a list, and sort it big to small
            QList<int> rows = selectedRows.toList();
            qSort(rows.begin(), rows.end());
            //now actually do the removing:
            foreach(int row, rows)
            tableWidget->removeRow(row);@

            1 Reply Last reply
            0
            • A Offline
              A Offline
              andre
              wrote on last edited by
              #6

              I think you will find that you are now removing the wrong rows...

              1 Reply Last reply
              0
              • G Offline
                G Offline
                goetz
                wrote on last edited by
                #7

                use (spot the parentheses after the <int>).

                @
                qSort(rows.begin(), rows.end(), qGreater<int>());
                @

                http://www.catb.org/~esr/faqs/smart-questions.html

                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