Results 1 to 4 of 4

Thread: Copy selected rows of a table to a model

  1. #1
    Join Date
    Feb 2017
    Posts
    19
    Thanks
    5
    Qt products
    Qt5

    Default Copy selected rows of a table to a model

    I want to make a new model containing only the selected rows of this table.
    Appending the selected rows to the empty model named "selected_rows_model" (QSqlTableModel).
    Qt Code:
    1. QItemSelectionModel *selectionModel = ui.devices_finder_tbl->selectionModel();
    2. if(selectionModel->hasSelection() ){
    3. //QModelIndexList list = selectionModel->selectedRows();
    4. QModelIndexList list = ui.devices_finder_tbl->selectedIndexes();
    5. for(int i = 0; i < list.size(); i++)
    6. {
    7. QModelIndex index = list.at(i);
    8. selected_rows_model->insertRow(selected_rows_model->rowCount(), index);
    9. }
    10. }
    To copy to clipboard, switch view to plain text mode 
    But it's not working, this row:
    Qt Code:
    1. selected_rows_model->insertRow(selected_rows_model->rowCount(), index);
    To copy to clipboard, switch view to plain text mode 
    is wrong.
    Ho can I fix it?

  2. #2
    Join Date
    Mar 2011
    Location
    Hyderabad, India
    Posts
    1,882
    Thanks
    3
    Thanked 452 Times in 435 Posts
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows
    Wiki edits
    15

    Default Re: Copy selected rows of a table to a model

    QModelIndex is specific to the model, it cannot be copied/supplied to another model. You will have to create another new QModelIndex for the selected_rows_model.
    When you know how to do it then you may do it wrong.
    When you don't know how to do it then it is not that you may do it wrong but you may not do it right.

  3. #3
    Join Date
    Feb 2017
    Posts
    19
    Thanks
    5
    Qt products
    Qt5

    Default Re: Copy selected rows of a table to a model

    How can I do this? Can you give me an example?

  4. #4
    Join Date
    Mar 2011
    Location
    Hyderabad, India
    Posts
    1,882
    Thanks
    3
    Thanked 452 Times in 435 Posts
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows
    Wiki edits
    15

    Default Re: Copy selected rows of a table to a model

    You can create a new QModelIndex using
    Qt Code:
    To copy to clipboard, switch view to plain text mode 

    Here is a working example.
    Qt Code:
    1. class SelectionCopier : public QObject
    2. {
    3. Q_OBJECT
    4. public:
    5. explicit SelectionCopier(const QAbstractItemView * from, QAbstractItemView * to, QObject * parent = 0)
    6. : QObject(parent)
    7. , mFromTable(from)
    8. , mToTable(to)
    9. {
    10. mToTable->setModel(new QStandardItemModel(mToTable));
    11.  
    12. connect(from, SIGNAL(pressed(QModelIndex)), SLOT(copySelection()));
    13. }
    14.  
    15. protected slots:
    16. void copySelection()
    17. {
    18. const QAbstractItemModel * fromModel = mFromTable->model();
    19. QAbstractItemModel * toModel = mToTable->model();
    20.  
    21. toModel->removeRows(0, toModel->rowCount());
    22. toModel->removeColumns(0, toModel->columnCount());
    23.  
    24. QItemSelectionModel * selectionModel = mFromTable->selectionModel();
    25.  
    26. if(selectionModel->hasSelection())
    27. {
    28. toModel->insertColumns(0, fromModel->columnCount());
    29.  
    30. QList<int> rows;
    31.  
    32. foreach(const QModelIndex & modelIndex, selectionModel->selectedIndexes())
    33. {
    34. if(!rows.contains(modelIndex.row()))
    35. {
    36. rows.append(modelIndex.row());
    37. toModel->insertRow(toModel->rowCount());
    38. }
    39.  
    40. const int row = rows.indexOf(modelIndex.row());
    41. const int col = modelIndex.column();
    42.  
    43. const QModelIndex index = toModel->index(row, col);
    44.  
    45. for(int role = 0; role < Qt::UserRole; ++role)
    46. toModel->setData(index, modelIndex.data(role), role);
    47. }
    48. }
    49. }
    50.  
    51. private:
    52. const QAbstractItemView * const mFromTable;
    53. QAbstractItemView * const mToTable;
    54. };
    To copy to clipboard, switch view to plain text mode 
    When you know how to do it then you may do it wrong.
    When you don't know how to do it then it is not that you may do it wrong but you may not do it right.

  5. The following user says thank you to Santosh Reddy for this useful post:

    panoss (23rd February 2017)

Similar Threads

  1. copy selected rows from QTableView to another table
    By Sashabn in forum Qt Programming
    Replies: 3
    Last Post: 24th February 2015, 00:04
  2. Replies: 5
    Last Post: 10th March 2011, 21:06
  3. Model-View: how to get the data from my selected rows
    By viasant in forum Qt Programming
    Replies: 5
    Last Post: 11th August 2008, 03:16
  4. Showing selected rows in a separate table
    By dnnc in forum Qt Programming
    Replies: 3
    Last Post: 21st June 2007, 17:35
  5. Replies: 10
    Last Post: 24th August 2006, 10:35

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
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.