Results 1 to 11 of 11

Thread: Idea for sorting values from QLineEdits?

  1. #1
    Join Date
    May 2011
    Posts
    10
    Thanks
    2
    Qt products
    Qt4
    Platforms
    Windows

    Default Idea for sorting values from QLineEdits?

    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!

  2. #2
    Join Date
    Jan 2006
    Location
    Munich, Germany
    Posts
    4,714
    Thanks
    21
    Thanked 418 Times in 411 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: Idea for sorting values from QLineEdits?

    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.
    ==========================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.

  3. #3
    Join Date
    May 2010
    Location
    Romania
    Posts
    1,021
    Thanks
    62
    Thanked 260 Times in 246 Posts
    Qt products
    Qt5
    Platforms
    MacOS X Unix/X11 Windows Android

    Default Re: Idea for sorting values from 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

  4. #4
    Join Date
    May 2011
    Posts
    10
    Thanks
    2
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Idea for sorting values from QLineEdits?

    Hi!

    Thank you for your answers.

    I´m still working on the problem.

    I tried so far:
    Qt Code:
    1. void MainWindow::sort()
    2. {
    3. bool ok;
    4. double number1 = in[0]->text().toDouble(&ok); // Here I get the values, which works fine.
    5. double number2 = in[1]->text().toDouble(&ok);
    6. double number3 = in[2]->text().toDouble(&ok);
    7. double number4 = in[3]->text().toDouble(&ok);
    8. double number5 = in[4]->text().toDouble(&ok);
    9.  
    10. QList<double> list; // Here I sort them, but I´m not sure if correct.
    11. list << number1 << number2 << number3 << number4 << number5;
    12. qSort(list.begin(), list.end(), qGreater<double>());
    13.  
    14.  
    15. QString resultString = ""; // Here I want to redisplay them in in[0] to in[4], but ordered as value1 to value5.
    16. in[0]->setText(resultString.setNum(value1));
    17. QString result1String = "";
    18. in[1]->setText(result1String.setNum(value2));
    19. QString result2String = "";
    20. in[2]->setText(result2String.setNum(value3));
    21. QString result3String = "";
    22. in[3]->setText(result3String.setNum(value4));
    23. QString result4String = "";
    24. in[4]->setText(result4String.setNum(value5));
    25. }
    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!

  5. #5
    Join Date
    Jan 2006
    Location
    Munich, Germany
    Posts
    4,714
    Thanks
    21
    Thanked 418 Times in 411 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: Idea for sorting values from QLineEdits?

    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.

  6. #6
    Join Date
    May 2011
    Posts
    10
    Thanks
    2
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Idea for sorting values from QLineEdits?

    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.

  7. #7
    Join Date
    Sep 2009
    Location
    Wroclaw, Poland
    Posts
    1,394
    Thanked 342 Times in 324 Posts
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows Android

    Default Re: Idea for sorting values from QLineEdits?

    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:
    1. QList<double> list;
    2. for( /*something*/ {
    3. double number = ...
    4. list << number;
    5. }
    6. qSort(list.begin(), list.end(), qGreater<double>());
    7. // and display:
    8. for( /*something*/ ){
    9. in[/*?*/]->setText( QString::number(/*??*/) );
    10. }
    To copy to clipboard, switch view to plain text mode 

  8. The following user says thank you to stampede for this useful post:

    ewwen (25th May 2011)

  9. #8
    Join Date
    May 2011
    Posts
    10
    Thanks
    2
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Idea for sorting values from QLineEdits?

    Hi, thanks for your reply!

    Well even if I fill, it doesn´t work.
    Qt Code:
    1. QList<double> list;
    2. for(int i = 0; i < Num; ++i) {
    3. double number[i] = in[i]->text().toDouble(&ok);
    4. list << number[0] << number[1] << number[2] << number[3] << number[4];
    5. }
    6.  
    7. qSort(list.begin(), list.end(), qGreater<double>());
    8. for(int i = 0; i < Num; ++i){
    9. in[i]->setText( QString::number());
    10. }
    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:
    Qt Code:
    1. QList<double> list;
    2. bool ok;
    3. double number1 = in[0]->text().toDouble(&ok);
    4. double number2 = in[1]->text().toDouble(&ok);
    5. double number3 = in[2]->text().toDouble(&ok);
    6. double number4 = in[3]->text().toDouble(&ok);
    7. double number5 = in[4]->text().toDouble(&ok);
    8. list << number1 << number2 << number3 << number4 << number5;
    9.  
    10. qSort(list.begin(), list.end(), qGreater<double>());
    To copy to clipboard, switch view to plain text mode 
    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)

  10. #9
    Join Date
    Jan 2006
    Location
    Munich, Germany
    Posts
    4,714
    Thanks
    21
    Thanked 418 Times in 411 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: Idea for sorting values from QLineEdits?

    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:
    1. QList<double> list;
    2. for(int i = 0; i < Num; ++i) {
    3. list << in[i]->text().toDouble(&ok);
    4. }
    5.  
    6. qSort(list.begin(), list.end(), qGreater<double>());
    7. for(int i = 0; i < Num; ++i){
    8. in[i]->setText( QString::number(list.at(i)));
    9. }
    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.

  11. The following user says thank you to high_flyer for this useful post:

    ewwen (25th May 2011)

  12. #10
    Join Date
    May 2011
    Posts
    10
    Thanks
    2
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Idea for sorting values from QLineEdits?

    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.

  13. #11
    Join Date
    Jan 2006
    Location
    Munich, Germany
    Posts
    4,714
    Thanks
    21
    Thanked 418 Times in 411 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: Idea for sorting values from QLineEdits?

    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.

Similar Threads

  1. Replies: 2
    Last Post: 21st December 2010, 20:43
  2. A Hippie Idea
    By qtoptus in forum Qt Programming
    Replies: 9
    Last Post: 17th December 2010, 23:28
  3. Replies: 4
    Last Post: 27th August 2010, 10:27
  4. ???any one have idea about this ???
    By damodharan in forum Qt Programming
    Replies: 1
    Last Post: 29th May 2010, 09:08
  5. UI Idea please
    By munna in forum General Discussion
    Replies: 1
    Last Post: 9th May 2006, 11:14

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.