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
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
Hi!
Thank you for your answers.
I´m still working on the problem.
I tried so far:
Qt Code:
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)); in[1]->setText(result1String.setNum(value2)); in[2]->setText(result2String.setNum(value3)); in[3]->setText(result3String.setNum(value4)); in[4]->setText(result4String.setNum(value5)); }To copy to clipboard, switch view to plain text mode
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!
a for loop?
==========================signature=============== ==================
S.O.L.I.D principles (use them!):
https://en.wikipedia.org/wiki/SOLID_...iented_design)
Do you write clean code? - if you are TDD'ing then maybe, if not, your not writing clean code.
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.
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:
Qt Code:
QList<double> list; for( /*something*/ { double number = ... list << number; } qSort(list.begin(), list.end(), qGreater<double>()); // and display: for( /*something*/ ){ }To copy to clipboard, switch view to plain text mode
ewwen (25th May 2011)
Hi, thanks for your reply!
Well even if I fill, it doesn´t work.
Qt Code:
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){ }To copy to clipboard, switch view to plain text mode
I don´t get the values, which I want to display in in[i] again after sorting.
This works better:
but there I still have the problem to display the ordered values...Qt Code:
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>());To copy to clipboard, switch view to plain text mode
Can anyone give me an advice, where to search or what to consider? (I tried several attempts now, but no one worked)
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!
Qt Code:
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){ }To copy to clipboard, switch view to plain text mode
==========================signature=============== ==================
S.O.L.I.D principles (use them!):
https://en.wikipedia.org/wiki/SOLID_...iented_design)
Do you write clean code? - if you are TDD'ing then maybe, if not, your not writing clean code.
ewwen (25th May 2011)
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.
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.
==========================signature=============== ==================
S.O.L.I.D principles (use them!):
https://en.wikipedia.org/wiki/SOLID_...iented_design)
Do you write clean code? - if you are TDD'ing then maybe, if not, your not writing clean code.
Bookmarks