Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. How to swap the value from two lineEdit fields
QtWS25 Last Chance

How to swap the value from two lineEdit fields

Scheduled Pinned Locked Moved General and Desktop
7 Posts 4 Posters 2.3k Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • F Offline
    F Offline
    fs_tigre
    wrote on last edited by
    #1

    Hi,

    I want to swap the values from two lineEdit fields.

    I have two lineEdit fields, width and length, with an IF statement comparing the values and if width is bigger than length a QMessageBox appears advising the user and everything is working fine, but instead I would like avoid the messageBox and swap the numbers automatically if the user enters a bigger number in the width field.

    In other words if the user enters a bigger number for the width than for the length I want to swap the numbers before any calculations.

    I tried the following code and it works but it's constantly crashing.

    @if(partW.toFloat() > partL.toFloat())
    {
    QString text = ui->lineEdit_PartLength->text();
    QString text2 = ui->lineEdit_PartWidth->text();
    ui->lineEdit_PartWidth->setText(text);
    ui->lineEdit_PartLength->setText(text2);
    }@

    I know my code is poor and there may be a better solution.

    Any idea what would be the best way to add this functionality?

    Thanks a lot.

    1 Reply Last reply
    0
    • K Offline
      K Offline
      koahnig
      wrote on last edited by
      #2

      Assuming that you are not running out of memory the reason for the crash is not in this part of the code. My blind guess is that the problems are with partW and partL.
      As it looks you should have assigned both fields somewhere before. So, if you have pulled already the strings for comparison, there is no need to get them again for the exchange.
      Note: this is a lot of guessing involved. You need to show more code.

      Vote the answer(s) that helped you to solve your issue(s)

      1 Reply Last reply
      0
      • D Offline
        D Offline
        DerManu
        wrote on last edited by
        #3

        You should split the "partW.toFloat() > partL.toFloat()" part into multiple calls in order to be able to check their *ok return value.

        Print debug output inside the block. You might create an infinite loop if this code is inside a slot that gets called when the text edits are changed.

        1 Reply Last reply
        0
        • W Offline
          W Offline
          Wilk
          wrote on last edited by
          #4

          Hello.
          I have a little bit offtopic suggestion: use QDoubleSpinBox instead of QLineEdit. It will make your application more reliable because QDoubleSpinBox automaticaly controlls user input, while QLineEdit doesn't, so you must control it by yourself.

          1 Reply Last reply
          0
          • F Offline
            F Offline
            fs_tigre
            wrote on last edited by
            #5

            [quote author="koahnig" date="1342967180"]Assuming that you are not running out of memory the reason for the crash is not in this part of the code. My blind guess is that the problems are with partW and partL.
            As it looks you should have assigned both fields somewhere before. So, if you have pulled already the strings for comparison, there is no need to get them again for the exchange.
            Note: this is a lot of guessing involved. You need to show more code. [/quote]
            Wow, it's incredible how good you guys are just by looking at a bit of code you can still give good advise. Even though I haven't fixed it I have a filling that your assumptions are good and it's where I'm going to start.

            [quote author="DerManu" date="1342969909"]You should split the "partW.toFloat() > partL.toFloat()" part into multiple calls in order to be able to check their *ok return value.

            Print debug output inside the block. You might create an infinite loop if this code is inside a slot that gets called when the text edits are changed.[/quote]
            Excuse my ignorance but what do you mean with split "partW.toFloat() > partL.toFloat()"? Sorry for the silly question.

            [quote author="Wilk" date="1342974292"]Hello.
            I have a little bit oftopic suggestion: use QDoubleSpinBox instead of QLineEdit. It will make your application more reliable because QDoubleSpinBox automaticaly controlls user input, while QLineEdit doesn't, so you must control it by yourself.[/quote]
            This is actually good advice because validating lineEdits are a pain, and the only reason I didn't use QDoubleSpinBox is because in these two fields I didn't like the way it looked with the spinbox, I have 7 input fields and these are the only ones as lineEdits the rest of them are QDoubleSpinBoxes, but I might re-considere changing them to QdoubleSpinBoxes.

            Thank you all a lot

            1 Reply Last reply
            0
            • W Offline
              W Offline
              Wilk
              wrote on last edited by
              #6

              About splitting “partW.toFloat() > partL.toFloat()”:
              I guess DerManu means something like this:
              @
              bool l_ok = false;
              bool w_ok = false;

              double l = partW.toFloat(&l_ok);
              double w = partW.toFloat(&w_ok)

              if (l_ok && w_ok) {
              if (w > l) {
              QString text = ui->lineEdit_PartLength->text();
              QString text2 = ui->lineEdit_PartWidth->text();
              ui->lineEdit_PartWidth->setText(text);
              ui->lineEdit_PartLength->setText(text2);
              }
              }
              @

              1 Reply Last reply
              0
              • F Offline
                F Offline
                fs_tigre
                wrote on last edited by
                #7

                Thanks a lot for the code.

                1 Reply Last reply
                0

                • Login

                • Login or register to search.
                • First post
                  Last post
                0
                • Categories
                • Recent
                • Tags
                • Popular
                • Users
                • Groups
                • Search
                • Get Qt Extensions
                • Unsolved