Results 1 to 5 of 5

Thread: How could I move the data of QStringList into QTextEdit?

  1. #1
    Join Date
    Jan 2011
    Posts
    127
    Thanks
    42
    Thanked 4 Times in 4 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default How could I move the data of QStringList into QTextEdit?

    OS : win7 64bits
    compiler : minGW4.6.1(32bits)

    Qt Code:
    1. QStringList result = /*some QString*/
    2. if(result.isEmpty())
    3. {
    4. textEdit->append(tr("nothing are different"));
    5. return;
    6. }
    7.  
    8. for(auto &data : result)
    9. {
    10. textEdit->append(data);
    11. }
    To copy to clipboard, switch view to plain text mode 

    Since the "result" would not be used after append to the "textEdit"
    I would like to move the QString of "result" into the "textEdit"
    How could I do that?Thanks a lot

  2. #2
    Join Date
    Dec 2008
    Location
    Istanbul, TURKEY
    Posts
    537
    Thanks
    14
    Thanked 13 Times in 13 Posts
    Qt products
    Qt4
    Platforms
    Windows Android

    Default Re: How could I move the data of QStringList into QTextEdit?

    Qt Code:
    1. textEdit->setText(data);
    To copy to clipboard, switch view to plain text mode 

  3. #3
    Join Date
    Jan 2011
    Posts
    127
    Thanks
    42
    Thanked 4 Times in 4 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: How could I move the data of QStringList into QTextEdit?

    Thanks, but setText would have different result
    Qt Code:
    1. B << "one" << "two";
    2. for(auto & a : B) A.append(a);
    3.  
    4. A.show();
    To copy to clipboard, switch view to plain text mode 
    will show
    "one"
    "two"

    but A.setText will show
    "two"

    Besides, I would like to move the data of B into A but not copy
    something similar to
    Qt Code:
    1. std::vector<std::string> A;
    2. std::string B("one");
    3. A.push_back(std::move(B)); //move the resource of B into A
    To copy to clipboard, switch view to plain text mode 

    How could I move the resource to the QTextEdit?Thanks

    Without rvalue reference in the old time, I would do something like this
    Qt Code:
    1. std::list<std::string> A;
    2. //A.push_back.... blah blah blah
    3. std::vector<std::string> B(A.size());
    4.  
    5. //for loop
    6. B.swap(A[i]);
    To copy to clipboard, switch view to plain text mode 
    Could I apply the same way on QTextEdit?Or there are better way?Thanks
    Last edited by stereoMatching; 24th April 2012 at 13:08.

  4. #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: How could I move the data of QStringList into QTextEdit?

    You cannot "move" the QString entries from the QStringList into the QTextEdit content because that content is not a collection of QStrings. The content of the QString will be copied one way or another.

    You could do it this way:
    Qt Code:
    1. QStringList result;
    2. // append 50000 strings
    3. ...
    4. if (result.isEmpty())
    5. textEdit.append( tr("nothing are different") );
    6. else {
    7. while ( !result.isEmpty() )
    8. textEdit.append( result.takeFirst() );
    9. }
    To copy to clipboard, switch view to plain text mode 
    The result list is reduced at each iteration with overheads for list manipulation. (440 ms on my machine)

    You could do it this way:
    Qt Code:
    1. QStringList result;
    2. // append 50000 strings
    3. ...
    4. if (result.isEmpty())
    5. textEdit.append( tr("nothing are different") );
    6. else
    7. textEdit.append( result.join("\n") );
    To copy to clipboard, switch view to plain text mode 
    There is only one call to append() rather than one per string (important if there are 50000 strings). You still need to build the joined string but compiler optimisation will probably remove the copy of the temporary result of join(). (130 ms on my machine)

    Regardless of how you do it, the majority of the unneeded QStringList resources can be freed by simply calling clear(). When the QStringList goes out of scope all of its resources will be freed anyway.

  5. #5
    Join Date
    Jan 2011
    Posts
    127
    Thanks
    42
    Thanked 4 Times in 4 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: How could I move the data of QStringList into QTextEdit?

    Thank you very much

Similar Threads

  1. Matching Data from QStringList
    By prophet0 in forum Qt Programming
    Replies: 3
    Last Post: 31st March 2012, 06:52
  2. Exists any method to move data into STL::MAPS ???
    By tonnot in forum General Programming
    Replies: 2
    Last Post: 18th April 2011, 17:22
  3. Problem in Move Move Event Handler.
    By redgoof in forum Qt Programming
    Replies: 0
    Last Post: 7th April 2010, 11:45
  4. attaching user data pointer to images in QTextEdit
    By Workeml in forum Qt Programming
    Replies: 0
    Last Post: 7th November 2008, 20:06
  5. Replies: 7
    Last Post: 2nd June 2006, 12:48

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.