You are not logged in.

Dear visitor, welcome to QtForum.org. If this is your first visit here, please read the Help. It explains in detail how this page works. To use all features of this page, you should consider registering. Please use the registration form, to register here or read more information about the registration process. If you are already registered, please login here.

1

Tuesday, March 27th 2012, 12:00pm

how to convert QString into an array of integer type

how do i convert a QString into an array of integers for sorting and output the result? Please help me with this code. tried many ways but there are type conversion errors

2

Tuesday, March 27th 2012, 12:39pm

how is your string formatted?
"1,2,3"
"1:2:3:"
"1 2 3"
??
If you have a problem, CUT and PASTE your code. Do not retype or simplify it. Give a COMPLETE and COMPILABLE example of your problem. Otherwise we are all guessing the problem from a fabrication where relevant details are often missing.

3

Tuesday, March 27th 2012, 12:41pm

anyway, look at qstring split
http://qt-project.org/doc/qt-4.8/qstring.html

and then qstring number
If you have a problem, CUT and PASTE your code. Do not retype or simplify it. Give a COMPLETE and COMPILABLE example of your problem. Otherwise we are all guessing the problem from a fabrication where relevant details are often missing.

4

Tuesday, March 27th 2012, 12:57pm

it is formatted in "1 2 3"
the problem is i dont have a code that works. tried many different ways but no output.
here is one of the many codes i have wrtten

#include "sort.h"
#include "ui_sort.h"
#include<QtGui>
#include<QtCore>
#include<QTextEdit>
#include<QSize>
#include<QString>

sort::sort(QWidget *parent)
{
setupUi(this);
connect(pushButton_sort,SIGNAL(clicked()),this,SLOT(sorting()));
}
void sort::sorting()
{
QString a,o,mid;
a=QString::number(plainTextEdit_array->toPlainText().toInt());
int n,m,i,j,k;
k=0;
n=a.size();
for(m = n/2;m>0;m/=2)
{
for(j = m;j< n;j++)
{
for(i=j-m;i>=0;i-=m)
{
if(a[i+m]<=a)
{
break;
}
else
{
mid[k] = a[i];
a[i] = a[i+m];
a[i+m] = mid[k];
mid[k] = o[i];
o[i] = o[i+m];
o[i+m] = mid[k];
}
}
}
}


plainTextEdit_sort->appendPlainText(o);

}
no errors but output is not coming.

5

Tuesday, March 27th 2012, 2:28pm

Qt provide sort functions for containers
try:

Source code

1
2
3
4
5
6
7
8
9
10
void sort()
{
    struct Comp
    {
        static bool lt( const QString & s0, const QString & s1 ) { return( s0.toInt() < s1.toInt() ); }
    };
    QStringList ints  = plainTextEdit_array->toPlainText().split( QRegExp( "\\s+" ), QString::SkipEmptyParts );
    qSort( ints.begin(), ints.end(), Comp::lt );
    plainTextEdit_sort->setPlainText( ints.join( "\n" ) );
}

6

Tuesday, March 27th 2012, 2:41pm

Thumbs UP!! it works. Thank You. :-)