Results 1 to 15 of 15

Thread: How copy data from one model view to another

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    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.

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

    breakthecode (5th December 2011)

  3. #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

    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

  4. #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

    You are welcome.

  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

    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 

  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

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

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

    breakthecode (6th 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

    can we remove an added item from the table

  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

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

  10. 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
  •  
Qt is a trademark of The Qt Company.