Results 1 to 4 of 4

Thread: copy selected rows from QTableView to another table

  1. #1
    Join Date
    Feb 2015
    Posts
    17
    Qt products
    Qt5
    Platforms
    Unix/X11 Windows Android

    Default copy selected rows from QTableView to another table

    Can u help me with this. I try to copy selected data from one QTableView to another tableview, but when i run this code my program crash.
    Can u help me with this?
    void Dialog:n_pushButton_clicked()
    {

    QAbstractItemModel * model = ui->tableView->model();
    QItemSelectionModel * selection = ui->tableView->selectionModel();
    QModelIndexList indexes = selection->selectedIndexes();


    if(!indexes.isEmpty()){


    foreach (QModelIndex i, indexes) {


    qDebug()<<i.row();
    qDebug()<<i.column();
    ui->tableView_2->model()->setData(ui->tableView_2->model()->index(i.row(),i.column()),ui->tableView->model()->data(i).toString());

    }
    }
    }

  2. #2
    Join Date
    Jan 2008
    Location
    Alameda, CA, USA
    Posts
    5,230
    Thanks
    302
    Thanked 864 Times in 851 Posts
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: copy selected rows from QTableView to another table

    As I count it, you have 8 or 9 pointers being retrieved or used in one method call or another, and not one of them is ever checked to see if it is actually a valid pointer. Nor do you ever check to see if the model indexes you are throwing around are valid. Who knows where it could be failing? Your code certainly doesn't.

  3. #3
    Join Date
    Feb 2015
    Posts
    17
    Qt products
    Qt5
    Platforms
    Unix/X11 Windows Android

    Default Re: copy selected rows from QTableView to another table

    Hi, problem is that i dont set model for second table, and that is the reason why my programs crash.
    On first table i use QSqlQueryModel.
    I need help to make an a empty table and when i click button to copy data selection from one table to another.
    I know how to copy selection data from table, i dont know how to make empty table with model who store data in ram memory.
    Sorry i am big noob and beginner. This is my 37 day of learning c++ and qt......
    Thanks for any help...

  4. #4
    Join Date
    Jan 2008
    Location
    Alameda, CA, USA
    Posts
    5,230
    Thanks
    302
    Thanked 864 Times in 851 Posts
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: copy selected rows from QTableView to another table

    Use QStandardItemModel as the model for your second table. For each item on the list of selected items, create a QStandardItem using the string retrieved from the selected item using its QModelIndex::data() method. Insert this new QStandardItem into the model at the appropriate place. If you assign the QStandardItemModel to your second table view, the view will automatically update as the model is changed.

    You will have to decide whether you want to clear the second model with each new copy and paste, or whether new items are added to the end of the model.

Similar Threads

  1. Moving up and down the selected rows in a QTableview
    By Raghaw in forum Qt Programming
    Replies: 0
    Last Post: 28th January 2013, 11:04
  2. iterating selected rows in a qtableview
    By JeanC in forum Qt Programming
    Replies: 2
    Last Post: 19th January 2008, 14:29
  3. Showing selected rows in a separate table
    By dnnc in forum Qt Programming
    Replies: 3
    Last Post: 21st June 2007, 16:35
  4. Replies: 10
    Last Post: 24th August 2006, 09:35
  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
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.