PDA

View Full Version : Idea for sorting values from QLineEdits?



ewwen
24th May 2011, 14:26
Hi!

I´m using 5 QLineEdits and I´ve added a butten with "Sort values", that the values get sorted internal and redisplayed in the 5 QLineEdits.

I read several approaches about it, but I´m still undecided. An easy way seems to use a spreadsheet. But that´s my point, I´m interested just to use the 5 QLineEdits and redisplay the values in them, "sorted descending".

I´d like to use double-values.

Has anyone an experience with this task?

If you have any ideas of an easy solution, please give me a hint... Thanks in advance!

high_flyer
24th May 2011, 14:31
Just manage the values in any object that knows how to sort, like a list for example, and when you sort, you let the the value container sort the values for you, and then just update your edit lines from that container.

Zlatomir
24th May 2011, 14:32
Read the values in a QList, sort the QList and then use the values in any order you like to put them again in the QLineEdits

ewwen
25th May 2011, 11:17
Hi!

Thank you for your answers.

I´m still working on the problem.

I tried so far:


void MainWindow::sort()
{
bool ok;
double number1 = in[0]->text().toDouble(&ok); // Here I get the values, which works fine.
double number2 = in[1]->text().toDouble(&ok);
double number3 = in[2]->text().toDouble(&ok);
double number4 = in[3]->text().toDouble(&ok);
double number5 = in[4]->text().toDouble(&ok);

QList<double> list; // Here I sort them, but I´m not sure if correct.
list << number1 << number2 << number3 << number4 << number5;
qSort(list.begin(), list.end(), qGreater<double>());


QString resultString = ""; // Here I want to redisplay them in in[0] to in[4], but ordered as value1 to value5.
in[0]->setText(resultString.setNum(value1));
QString result1String = "";
in[1]->setText(result1String.setNum(value2));
QString result2String = "";
in[2]->setText(result2String.setNum(value3));
QString result3String = "";
in[3]->setText(result3String.setNum(value4));
QString result4String = "";
in[4]->setText(result4String.setNum(value5));
}


I know I´m missing some index-assignment that value1 is the greatest, value2 the second... do you have a suggestion?

If you see a mistake, please tell me!

high_flyer
25th May 2011, 11:33
a for loop?

ewwen
25th May 2011, 12:51
What do you mean with "a for loop"? Sorry I don´t understand.

I just want to sort 5 values, which where inserted into 5 QLineEdits and to redisplay them, sorted, in it.

stampede
25th May 2011, 13:02
You are sorting the list, but you don't use it anymore later. Just display the numbers from this list to line edits, using a for loop like high_flyer suggested. You can use QString::number() method, no need for temporary QString objects here.
I'll give you some hints about the "for" loop:


QList<double> list;
for( /*something*/ {
double number = ...
list << number;
}
qSort(list.begin(), list.end(), qGreater<double>());
// and display:
for( /*something*/ ){
in[/*?*/]->setText( QString::number(/*??*/) );
}

ewwen
25th May 2011, 16:36
Hi, thanks for your reply!

Well even if I fill, it doesn´t work.


QList<double> list;
for(int i = 0; i < Num; ++i) {
double number[i] = in[i]->text().toDouble(&ok);
list << number[0] << number[1] << number[2] << number[3] << number[4];
}

qSort(list.begin(), list.end(), qGreater<double>());
for(int i = 0; i < Num; ++i){
in[i]->setText( QString::number());
}


I don´t get the values, which I want to display in in[i] again after sorting.

This works better:


QList<double> list;
bool ok;
double number1 = in[0]->text().toDouble(&ok);
double number2 = in[1]->text().toDouble(&ok);
double number3 = in[2]->text().toDouble(&ok);
double number4 = in[3]->text().toDouble(&ok);
double number5 = in[4]->text().toDouble(&ok);
list << number1 << number2 << number3 << number4 << number5;

qSort(list.begin(), list.end(), qGreater<double>());

but there I still have the problem to display the ordered values...

Can anyone give me an advice, where to search or what to consider? (I tried several attempts now, but no one worked)

high_flyer
25th May 2011, 16:50
You are confusing all kinds of stuff, and you clearly have no understanding of how basic variable deceleration works.
You should really first learn basic programming concepts, and C syntax before you start programming with C++ and Qt.

your original code could not even compile!



QList<double> list;
for(int i = 0; i < Num; ++i) {
list << in[i]->text().toDouble(&ok);
}

qSort(list.begin(), list.end(), qGreater<double>());
for(int i = 0; i < Num; ++i){
in[i]->setText( QString::number(list.at(i)));
}

ewwen
25th May 2011, 17:01
Hi high_flyer, thanks for your help! Now everything runs and I finally understood the problem...

I´m still learning to programme and not everything I try, I find in books or tutorials. You are a professional and I am a beginner.

high_flyer
26th May 2011, 08:45
ewwen, that is ok to learn, everyone was a beginner once.
But you are tackling Qt, which prerequisites C++ knowledge, where as you are still in your first steps with C/C++ syntax and principals, thus doing anything with Qt will only make it more difficult.
You should first concentrate on C/C++, and once you are comfortable with the language and basic concepts, you will find Qt very helpful.
Otherwise you will find Qt hard confusing and frustrating.