PDA

View Full Version : How could I move the data of QStringList into QTextEdit?



stereoMatching
24th April 2012, 12:03
OS : win7 64bits
compiler : minGW4.6.1(32bits)



QStringList result = /*some QString*/
if(result.isEmpty())
{
textEdit->append(tr("nothing are different"));
return;
}

for(auto &data : result)
{
textEdit->append(data);
}


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

zgulser
24th April 2012, 12:16
textEdit->setText(data);

stereoMatching
24th April 2012, 13:02
Thanks, but setText would have different result


QTextEdit A;
QStringList B;
B << "one" << "two";
for(auto & a : B) A.append(a);

A.show();

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



std::vector<std::string> A;
std::string B("one");
A.push_back(std::move(B)); //move the resource of B into A



How could I move the resource to the QTextEdit?Thanks

Without rvalue reference in the old time, I would do something like this


std::list<std::string> A;
//A.push_back.... blah blah blah
std::vector<std::string> B(A.size());

//for loop
B.swap(A[i]);

Could I apply the same way on QTextEdit?Or there are better way?Thanks

ChrisW67
25th April 2012, 00:25
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:


QStringList result;
// append 50000 strings
...
if (result.isEmpty())
textEdit.append( tr("nothing are different") );
else {
while ( !result.isEmpty() )
textEdit.append( result.takeFirst() );
}

The result list is reduced at each iteration with overheads for list manipulation. (440 ms on my machine)

You could do it this way:


QStringList result;
// append 50000 strings
...
if (result.isEmpty())
textEdit.append( tr("nothing are different") );
else
textEdit.append( result.join("\n") );

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.

stereoMatching
27th April 2012, 12:29
Thank you very much