Results 1 to 15 of 15

Thread: How copy data from one model view to another

  1. #1
    Join Date
    Nov 2011
    Location
    India
    Posts
    22
    Thanks
    11
    Qt products
    Qt4 Qt/Embedded
    Platforms
    Unix/X11 Windows Maemo/MeeGo

    Default How copy data from one model view to another

    i am trying to select a data member from a model/view class, here a table view and add that particular data to an another table view. i can copy the data to another table view but only one selected data can add when i click the add button by using my code. can i add more data to table view by modifying the code or is there any other solution

    Qt Code:
    1. void dcsinter::on_pushButton_clicked()
    2. {
    3.  
    4. const QModelIndex index = ui->dirtableView->selectionModel()->currentIndex();
    5. QString selectedtext = index.data(Qt::DisplayRole).toString();
    6.  
    7. list << selectedtext ;
    8. model2->setStringList(list);
    9. ui->listtableView_2->setModel(model2);
    10. ui->listtableView_2->setColumnWidth(0,600);
    11.  
    12.  
    13.  
    14. }
    To copy to clipboard, switch view to plain text mode 

    thank you
    Last edited by breakthecode; 3rd December 2011 at 20:19.

  2. #2
    Join Date
    Nov 2011
    Location
    India
    Posts
    22
    Thanks
    11
    Qt products
    Qt4 Qt/Embedded
    Platforms
    Unix/X11 Windows Maemo/MeeGo

    Default Re: How copy data from one model view to another

    sorry i missed something to say in the above thread, select a data from 1st table and add it to 2nd table it will appear in the 2nd table but when add another data from the first table then in the 2nd table replace the first data with 2nd

  3. #3
    Join Date
    Jan 2006
    Location
    Germany
    Posts
    4,380
    Thanks
    19
    Thanked 1,005 Times in 913 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows Symbian S60
    Wiki edits
    5

    Default Re: How copy data from one model view to another

    Beside that you have a memory leak in your function, you replace the model and do not append the new list. So reuse the old model and append the data by getting the old string list, append the new one and set the new composed list back to the model.

  4. The following user says thank you to Lykurg for this useful post:

    breakthecode (4th December 2011)

  5. #4
    Join Date
    Nov 2011
    Location
    India
    Posts
    22
    Thanks
    11
    Qt products
    Qt4 Qt/Embedded
    Platforms
    Unix/X11 Windows Maemo/MeeGo

    Default Re: How copy data from one model view to another

    i want two table views , one is for tabling files in a dir, second one is for real time adding of selected files. i think i need some modification in here

    QStringListModel *model2 = new QStringListModel();
    QStringList list;
    list << selectedtext ;
    model2->setStringList(list);

    thank you

  6. #5
    Join Date
    Jan 2006
    Location
    Germany
    Posts
    4,380
    Thanks
    19
    Thanked 1,005 Times in 913 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows Symbian S60
    Wiki edits
    5

    Default Re: How copy data from one model view to another

    Well I don't know the architecture of your code, but instead of creating the model each time the function is called do something like that
    Qt Code:
    1. QStringList newData; // or where ever your data is
    2. QStringListModel* model = qobject_cast<QStringListModel*>(ui->listtableView_2->model());
    3. QStringList old = model->stringList();
    4. old << newData;
    5. model->setStringList(old);
    To copy to clipboard, switch view to plain text mode 

  7. The following user says thank you to Lykurg for this useful post:

    breakthecode (4th December 2011)

  8. #6
    Join Date
    Nov 2011
    Location
    India
    Posts
    22
    Thanks
    11
    Qt products
    Qt4 Qt/Embedded
    Platforms
    Unix/X11 Windows Maemo/MeeGo

    Default Re: How copy data from one model view to another

    finally i got an another idea, load the string to a file then read the list model from there but when i writing the strings to the file again hits the same problem, only first the first string is saved others are overwrite on the first one. i want to write the strings one after the another, is there any way to solve that
    Qt Code:
    1. void dcsinter::on_pushButton_clicked()
    2. {
    3.  
    4. const QModelIndex index = ui->dirtableView->selectionModel()->currentIndex();
    5. QString selectedtext = index.data(Qt::DisplayRole).toString()
    6. QFile listfie("list.txt");
    7. listfie.open(QIODevice::WriteOnly | QIODevice::Text);
    8. QTextStream out(&listfie);
    9. out << selectedtext << '\n' ;
    10. }
    To copy to clipboard, switch view to plain text mode 
    imagine if the vale selectedtext string on first click is hdbhdn.avi second click hgsdjsdff.avi
    then file shows like the below

    hgsdjsdff.avi

    i want to like this

    wdbhdn.avi
    hgsdjsdff.avi

    i want a continues writing on the file

    thank you

  9. #7
    Join Date
    Jan 2006
    Location
    Germany
    Posts
    4,380
    Thanks
    19
    Thanked 1,005 Times in 913 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows Symbian S60
    Wiki edits
    5

    Default Re: How copy data from one model view to another

    I doubt if that is the right solution but you must know... Use also QIODevice::Append at QFile::open().

  10. The following user says thank you to Lykurg for this useful post:

    breakthecode (5th December 2011)

  11. #8
    Join Date
    Nov 2011
    Location
    India
    Posts
    22
    Thanks
    11
    Qt products
    Qt4 Qt/Embedded
    Platforms
    Unix/X11 Windows Maemo/MeeGo

    Default Re: How copy data from one model view to another

    you're right it didn't work that i thought, i need a help here do u have any idea that this code
    Qt Code:
    1. QStringListModel* model = qobject_cast<QStringListModel*>(ui->listtableView_2->model());
    To copy to clipboard, switch view to plain text mode 
    crash the program

    i think i struck in this table issue
    Qt Code:
    1. void dcsinter::on_pushButton_clicked()
    2. {
    3.  
    4. const QModelIndex index = ui->dirtableView->selectionModel()->currentIndex();
    5. QString selectedtext = index.data(Qt::DisplayRole).toString();
    6.  
    7. list << selectedtext ;
    8. model->setStringList(list);
    9. ui->listtableView_2->setModel(model);
    10. ui->listtableView_2->setColumnWidth(0,600);
    11. }
    To copy to clipboard, switch view to plain text mode 

    i have a question, is QTableView build for adding column with data
    Last edited by breakthecode; 5th December 2011 at 14:53. Reason: spelling corrections

  12. #9
    Join Date
    Jan 2006
    Location
    Germany
    Posts
    4,380
    Thanks
    19
    Thanked 1,005 Times in 913 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows Symbian S60
    Wiki edits
    5

    Default Re: How copy data from one model view to another

    Have you checked if ui->listtableView_2->model() is returning a null pointer (= the view hasn't any model yet)? You first have to set a model to the view (best in the constructor) and then you can reuse the model in your on_pushButton_clicked function.

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

    breakthecode (5th December 2011)

  14. #10
    Join Date
    Nov 2011
    Location
    India
    Posts
    22
    Thanks
    11
    Qt products
    Qt4 Qt/Embedded
    Platforms
    Unix/X11 Windows Maemo/MeeGo

    Default Re: How copy data from one model view to another

    thanks 1000
    this is what i am looking for 2 days this single piece idea
    you said it in the first day but i can't understand it, i now know it's a basic in c++ pro. but i can't understand it, sorry for waisting your time
    again thanks , now it's working very well.
    Last edited by breakthecode; 5th December 2011 at 18:22. Reason: updated contents

  15. #11
    Join Date
    Jan 2006
    Location
    Germany
    Posts
    4,380
    Thanks
    19
    Thanked 1,005 Times in 913 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows Symbian S60
    Wiki edits
    5

    Default Re: How copy data from one model view to another

    You are welcome.

  16. #12
    Join Date
    Nov 2011
    Location
    India
    Posts
    22
    Thanks
    11
    Qt products
    Qt4 Qt/Embedded
    Platforms
    Unix/X11 Windows Maemo/MeeGo

    Default Re: How copy data from one model view to another

    here i publish the full result of the forum discussion, it's useful for anyone encountering the same problem
    aim is to copy a data from a qtableview and add that data to another qtableview
    first create a constructor
    Qt Code:
    1. void dcsinter::listing()
    2. {
    3. ui->listtableView_2->setModel(model);
    4.  
    5.  
    6. }
    To copy to clipboard, switch view to plain text mode 
    then on click push button
    Qt Code:
    1. void dcsinter::on_pushButton_clicked()
    2. {
    3.  
    4. const QModelIndex index = ui->dirtableView->selectionModel()->currentIndex();
    5. QString selectedtext = index.data(Qt::DisplayRole).toString();
    6.  
    7. QStringListModel* model = qobject_cast<QStringListModel*>(ui->listtableView_2->model());
    8. QStringList list = model->stringList();
    9. list << selectedtext ;
    10. model->setStringList(list);
    11.  
    12. }
    To copy to clipboard, switch view to plain text mode 

  17. #13
    Join Date
    Jan 2006
    Location
    Germany
    Posts
    4,380
    Thanks
    19
    Thanked 1,005 Times in 913 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows Symbian S60
    Wiki edits
    5

    Default Re: How copy data from one model view to another

    Hi, one more improvement to avoid the cast: Make the model a private member of your class an access it directly.

  18. The following user says thank you to Lykurg for this useful post:

    breakthecode (6th December 2011)

  19. #14
    Join Date
    Nov 2011
    Location
    India
    Posts
    22
    Thanks
    11
    Qt products
    Qt4 Qt/Embedded
    Platforms
    Unix/X11 Windows Maemo/MeeGo

    Default Re: How copy data from one model view to another

    can we remove an added item from the table

  20. #15
    Join Date
    Jan 2006
    Location
    Germany
    Posts
    4,380
    Thanks
    19
    Thanked 1,005 Times in 913 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows Symbian S60
    Wiki edits
    5

    Default Re: How copy data from one model view to another

    Yes, Use QStringListModel::removeRows(). If you have a string and want to know which index (row number) is has, use QAbstractItemModel::match().

  21. The following user says thank you to Lykurg for this useful post:

    breakthecode (8th December 2011)

Similar Threads

  1. Qt delegate model data and view
    By giusepped in forum Qt Programming
    Replies: 0
    Last Post: 27th July 2011, 11:30
  2. Replies: 1
    Last Post: 24th February 2011, 05:54
  3. Model / View and data structures
    By laszlo.gosztola in forum Newbie
    Replies: 0
    Last Post: 2nd December 2010, 05:13
  4. How to identify data from the model in the view?
    By csmithmaui in forum Qt Programming
    Replies: 8
    Last Post: 17th April 2010, 08:52
  5. Table view->model->set data
    By tulsi in forum Qt Programming
    Replies: 3
    Last Post: 21st April 2009, 08:36

Tags for this Thread

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.