Results 1 to 10 of 10

Thread: Deleting selected row from a QTableView

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Join Date
    Dec 2009
    Location
    New Zealand
    Posts
    54
    Thanks
    23
    Qt products
    Qt4
    Platforms
    Windows

    Default Deleting selected row from a QTableView

    Hi,

    I have successfully made a table where I can add two columns of user specified data, one row at a time. (Please see attachment)

    What I am trying to do now is allow the user to delete a row in case they want to change things,

    I currently have it so that the same row is deleted every time the "Remove Channel" button is clicked by using the following code
    Qt Code:
    1. void Loader::removeChannelFromTable()
    2. {
    3. model->removeRows ( 1, 1);
    4. row = row - 1;
    5. }
    To copy to clipboard, switch view to plain text mode 

    What I would like to do now is let the user select a row (so its highlighted), and then if the "Remove Channel" button is clicked the selected row will removed.


    I have made the following connection so that when my table (channelsView) is clicked, a signal gets emitted

    Qt Code:
    1. QObject::connect(channelsView, SIGNAL(clicked(QModelIndex)),
    2. this, SLOT(clicked(QModelIndex)));
    To copy to clipboard, switch view to plain text mode 


    from here I find I'm not sure what to do next, (or if I am on the right track even?)
    Attached Images Attached Images

  2. #2
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,368
    Thanks
    3
    Thanked 5,017 Times in 4,793 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Deleting selected row from a QTableView

    That's wrong. You are interested not in reacting on clicking a row but on clicking the button. Only then you want to access selected rows (and remember you can select rows without clicking on them too). So do exactly this. There is QItemSelectionModel object associated with every QAbstractItemView, you can ask it for items currently selected.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  3. The following user says thank you to wysota for this useful post:

    Ferric (13th January 2010)

  4. #3
    Join Date
    Dec 2009
    Location
    New Zealand
    Posts
    54
    Thanks
    23
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Deleting selected row from a QTableView

    Okay, I know what you mean by only needing to react when the user clicks "remove channel" button.

    I have tried to write the following code

    Qt Code:
    1. void Loader::removeChannelFromTable()
    2. {
    3. QMessageBox::information(this,tr(""),tr("Slot working?") ); // to make sure slot is being run.
    4. QItemSelectionModel *selected = new QItemSelectionModel(model); // selects my QStandardItemModel()
    5. selected->clearSelection(); // clears the selected index
    6. }
    To copy to clipboard, switch view to plain text mode 

    But this simply does nothing, it compiles and runs, but when I select a row and click remove, nothing happens (apart from the message box).



    Also I don't really know how to do this:
    (and remember you can select rows without clicking on them too)

  5. #4
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,729
    Thanks
    13
    Thanked 1,610 Times in 1,537 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Default Re: Deleting selected row from a QTableView

    Line 4 creates a new selection model rather than fetching the model that is already associated with the view. Line 5 does not do what you think it does: it deselects everything, it does not delete selected data. You want something like this (I haven't tested it so please forgive minor glitches):
    Qt Code:
    1. void Loader::removeChannelFromTable()
    2. {
    3. QMessageBox::information(this,tr(""),tr("Slot working?") ); // to make sure slot is being run.
    4.  
    5. // code to get list of selected rows
    6. QItemSelectionModel *selected = channelsView->selectionModel();
    7. QModelIndexList rowList = selected->selectedRows();
    8.  
    9. // code to delete these model indexes from the model
    10. foreach(QModelIndex rowIndex, rowList) {
    11. model->removeRow(roxIndex.row(), rowIndex.parent());
    12. }
    13. }
    To copy to clipboard, switch view to plain text mode 
    You might also consider using QAbstractItemView::setSelectionBehavior() and QAbstractItemView::setSelectionMode() on the view so the user selects whole rows etc.

  6. The following user says thank you to ChrisW67 for this useful post:

    Ferric (14th January 2010)

  7. #5
    Join Date
    Dec 2009
    Location
    New Zealand
    Posts
    54
    Thanks
    23
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Deleting selected row from a QTableView

    You might also consider using QAbstractItemView::setSelectionBehavior() and QAbstractItemView::setSelectionMode() on the view so the user selects whole rows etc.
    Thanks, yeah I will try and set it up so that if the user clicks anywhere in a row, the entire row is selected.

    The basic function of the remove button is working now, Thanks for the example.

  8. #6
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,368
    Thanks
    3
    Thanked 5,017 Times in 4,793 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Deleting selected row from a QTableView

    Quote Originally Posted by ChrisW67 View Post
    Qt Code:
    1. // code to delete these model indexes from the model
    2. foreach(QModelIndex rowIndex, rowList) {
    3. model->removeRow(roxIndex.row(), rowIndex.parent());
    4. }
    To copy to clipboard, switch view to plain text mode 
    This has a chance of not-working. It would probably be better to remove the rows from the end to the beginning to retain validity of model indexes or to call removeRows() if the selection forms a range without gaps.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  9. #7
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,729
    Thanks
    13
    Thanked 1,610 Times in 1,537 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Default Re: Deleting selected row from a QTableView

    Good point Wysota

  10. #8

    Default Re: Deleting selected row from a QTableView

    This has a chance of not-working. It would probably be better to remove the rows from the end to the beginning to retain validity of model indexes or to call removeRows() if the selection forms a range without gaps.
    yes, it's inconvenient to remove selected rows. You have to sort them by the row index and then remove them, while in QListWidget, you only need to delete the QListWidgetItem.

  11. #9
    Join Date
    Dec 2009
    Location
    New Zealand
    Posts
    54
    Thanks
    23
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Deleting selected row from a QTableView

    This has a chance of not-working.
    Yes, I have noticed some glitches as it is currently set up:

    Qt Code:
    1. void Loader::removeChannelFromTable()
    2.  
    3. {
    4.  
    5. // code to get list of selected rows
    6. QItemSelectionModel *selected = channelsView->selectionModel();
    7. QModelIndexList rowList = selected->selectedRows();
    8. //qDebug() << rowList;
    9.  
    10. //code to delete these model indexes from the model
    11. foreach(QModelIndex rowIndex, rowList) {
    12. //qDebug() << rowIndex.row();
    13. model->removeRow(rowIndex.row(), rowIndex.parent());
    14. row = row - 1; // so that I know where the next channel should be added, i.e at the first empty row.
    15. }
    16. }
    To copy to clipboard, switch view to plain text mode 

    It would probably be better to remove the rows from the end to the beginning to retain validity of model indexes
    From using QDebug, I can see when I select multiple rows they are removed from beginning to the end, what is the easiest was to reverse this? ( all I can propose is putting the rows into a list, then reversing the list, then removing the rows, but this is probably not the most efficient way).

    Thanks

  12. #10
    Join Date
    Jul 2009
    Posts
    139
    Thanks
    13
    Thanked 59 Times in 52 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: Deleting selected row from a QTableView

    You already have a list. Just use a for loop and go backwards through the list.
    Qt Code:
    1. for (int i = rowList.count(); i >= 0; i--)
    2. {
    3. qDebug() << rowList.at(i).row();
    4. }
    To copy to clipboard, switch view to plain text mode 

  13. The following user says thank you to numbat for this useful post:

    Ferric (15th January 2010)

Similar Threads

  1. QTableView get selected row
    By raphaelf in forum Qt Programming
    Replies: 8
    Last Post: 12th July 2013, 03:35
  2. Deleting QSqlTableModel and QTableView
    By waynew in forum Qt Programming
    Replies: 7
    Last Post: 24th December 2009, 00:17
  3. deleting selected headers
    By ru_core in forum Qt Programming
    Replies: 3
    Last Post: 16th April 2008, 07:53
  4. iterating selected rows in a qtableview
    By JeanC in forum Qt Programming
    Replies: 2
    Last Post: 19th January 2008, 14:29
  5. Get list of selected rows from QTableView
    By jnk5y in forum Qt Programming
    Replies: 8
    Last Post: 17th February 2006, 16:59

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Qt is a trademark of The Qt Company.