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 get a query result into LineEdit widget?
Forum Updated to NodeBB v4.3 + New Features

How to get a query result into LineEdit widget?

Scheduled Pinned Locked Moved Solved General and Desktop
25 Posts 7 Posters 2.8k Views 3 Watching
  • 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.
  • JonBJ JonB

    @qtnoob420
    If you are prepared to use a QSqlQueryModel where you presently have a QSqlQuery, you could then hook up a QDataWidgetMapper to map any/all its columns to desired widgets. Especially nice if you have several columns/widgets to map. Up to you whether you find this simpler or more complicated than writing your own code off the QSqlQuery to map to widgets.

    Q Offline
    Q Offline
    qtnoob420
    wrote on last edited by
    #16

    @JonB thanks for the hint. i will look into it.

    1 Reply Last reply
    0
    • Pl45m4P Pl45m4

      @qtnoob420 said in How to get a query result into LineEdit widget?:

      because it always seems that my problems are too specific

      There is nothing specific. This is basic QSql.. stuff usage.

      so i guess that there is nothing in my query?

      You are trying to access index 1, which means, you need to have at least two valid results here, which is unlikely, when picking a specific ID:
      If you expect only one record, you need to pick index 0 (like almost everything in C++ or most programming languages starts at 0)

      Ah made a mistake here.
      The index is the field of your current record. But same rules apply: You need to have at least two fields in your record.

      • https://doc.qt.io/qt-5/qsqlquery.html#value

      Using SELECT * is not recommended because the order of the fields in the query is undefined.

      If your query is correct and your result is not empty, are you sure, that the second field contains a string?

      You could check query.isValid() (https://doc.qt.io/qt-5/qsqlquery.html#isValid) before you continue to process the result.

      Q Offline
      Q Offline
      qtnoob420
      wrote on last edited by
      #17

      @Pl45m4 ok i got it to work now. it atleast gives me the first value. probably i am mistaken here again.

      int i=0;
      while (query.next())
      {
          ui->bem_1->insertPlainText(query.value(i).toString());
          i++;
      }
      

      it does not give other values than the id. i thought, that now all the values from the first row would be put into my plainText

      JonBJ 1 Reply Last reply
      0
      • Q qtnoob420

        @Pl45m4 ok i got it to work now. it atleast gives me the first value. probably i am mistaken here again.

        int i=0;
        while (query.next())
        {
            ui->bem_1->insertPlainText(query.value(i).toString());
            i++;
        }
        

        it does not give other values than the id. i thought, that now all the values from the first row would be put into my plainText

        JonBJ Offline
        JonBJ Offline
        JonB
        wrote on last edited by JonB
        #18

        @qtnoob420
        You are not doing the right thing here. You use i to access a column in the query, yet you increment i for each new row fetched from query.next(). So you ask for:

        row 0, column 0
        row 1, column 1
        row 2 column 2
        

        And if you get only one row back, as per your query WHERE id = 1, you will only get one row and therefore only the first output from above.

        To iterate all columns in rows, you mean something more like:

        while (query.next())    // next *row*
        {
            int col = 0;
            QVariant v = query().value(col);    // first *column*
            while (v.isValid())    // column exists, will return invalid QVariant once `col` goes beyond last column
            {
                ui->bem_1->insertPlainText(v.toString());
                col++;
                v = query().value(col);    // next *column*
            }
        }
        
        Q 1 Reply Last reply
        2
        • SGaistS Offline
          SGaistS Offline
          SGaist
          Lifetime Qt Champion
          wrote on last edited by
          #19

          Hi,

          Did you try to first draw a suitable UI before diving into the SQL parts ?

          As @jsulm noted, it looks like you are trying to implement a UI that might well be really difficult to use.

          Depending on what your user needs to get you might want to consider a light page with only the basic information and then a second one with more details etc. You might even be better served by a simple table.

          Interested in AI ? www.idiap.ch
          Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

          1 Reply Last reply
          1
          • mrjjM Offline
            mrjjM Offline
            mrjj
            Lifetime Qt Champion
            wrote on last edited by
            #20

            @qtnoob420 said in How to get a query result into LineEdit widget?:

            i could provide a picture of my ui if that helps.

            Hi
            Please do as lineEdits and checkboxes can easily be replaced by a table, making it much easier to load
            and edit data and save it back to the database.

            While it is possible to loop the QSqlQuery and assign each column to a given pre-placed widget, its
            also likely to become a burden over time.
            Just saving the data back will be "involving" as the widgets have no idea what column they belong to and
            Qt already contains other data-aware classes to help with these tasks.
            Like as @JonB mentions, using QSqlQueryModel + QDataWidgetMapper

            Also, a QSqlTableModel + TableView would allow to directly edit a table with only a few lines of code, so
            please show us the GUI so we can see if we can find a better way than manually updating and saving
            50 LineEdits and 200 CheckBoxes.

            alt text

            Q 1 Reply Last reply
            1
            • mrjjM mrjj

              @qtnoob420 said in How to get a query result into LineEdit widget?:

              i could provide a picture of my ui if that helps.

              Hi
              Please do as lineEdits and checkboxes can easily be replaced by a table, making it much easier to load
              and edit data and save it back to the database.

              While it is possible to loop the QSqlQuery and assign each column to a given pre-placed widget, its
              also likely to become a burden over time.
              Just saving the data back will be "involving" as the widgets have no idea what column they belong to and
              Qt already contains other data-aware classes to help with these tasks.
              Like as @JonB mentions, using QSqlQueryModel + QDataWidgetMapper

              Also, a QSqlTableModel + TableView would allow to directly edit a table with only a few lines of code, so
              please show us the GUI so we can see if we can find a better way than manually updating and saving
              50 LineEdits and 200 CheckBoxes.

              alt text

              Q Offline
              Q Offline
              qtnoob420
              wrote on last edited by
              #21

              @mrjj @SGaist here the pictures of my ui. https://imgur.com/a/NulxYJe

              1 Reply Last reply
              0
              • JonBJ JonB

                @qtnoob420
                You are not doing the right thing here. You use i to access a column in the query, yet you increment i for each new row fetched from query.next(). So you ask for:

                row 0, column 0
                row 1, column 1
                row 2 column 2
                

                And if you get only one row back, as per your query WHERE id = 1, you will only get one row and therefore only the first output from above.

                To iterate all columns in rows, you mean something more like:

                while (query.next())    // next *row*
                {
                    int col = 0;
                    QVariant v = query().value(col);    // first *column*
                    while (v.isValid())    // column exists, will return invalid QVariant once `col` goes beyond last column
                    {
                        ui->bem_1->insertPlainText(v.toString());
                        col++;
                        v = query().value(col);    // next *column*
                    }
                }
                
                Q Offline
                Q Offline
                qtnoob420
                wrote on last edited by
                #22

                @JonB thanks sir. this is what i needed :)

                JonBJ 1 Reply Last reply
                0
                • Q qtnoob420

                  @JonB thanks sir. this is what i needed :)

                  JonBJ Offline
                  JonBJ Offline
                  JonB
                  wrote on last edited by JonB
                  #23

                  @qtnoob420
                  You are probably referring to the code for correctly reading the value in that column loop. And that's fine, but now that I see your form and how many fields there are I do think you would benefit from changing over to a QSqlTableModel and being able to use a QDataWidgetMapper. It looks like you are going to allow the user to edit the fields (not just display them), and the data widget mapper will allow you to do this very easily, and propagate the changes back to the database. I realize it is something new to learn, you don't have to do it but you might want to move that way if you have the time.

                  Q 1 Reply Last reply
                  0
                  • JonBJ JonB

                    @qtnoob420
                    You are probably referring to the code for correctly reading the value in that column loop. And that's fine, but now that I see your form and how many fields there are I do think you would benefit from changing over to a QSqlTableModel and being able to use a QDataWidgetMapper. It looks like you are going to allow the user to edit the fields (not just display them), and the data widget mapper will allow you to do this very easily, and propagate the changes back to the database. I realize it is something new to learn, you don't have to do it but you might want to move that way if you have the time.

                    Q Offline
                    Q Offline
                    qtnoob420
                    wrote on last edited by
                    #24

                    @JonB i could try this too, if i get everything to work and have enough time left. but i need to finalize it 3 weeks and must write a paper about it too. it is part of my studies

                    JonBJ 1 Reply Last reply
                    1
                    • Q qtnoob420

                      @JonB i could try this too, if i get everything to work and have enough time left. but i need to finalize it 3 weeks and must write a paper about it too. it is part of my studies

                      JonBJ Offline
                      JonBJ Offline
                      JonB
                      wrote on last edited by
                      #25

                      @qtnoob420
                      I quite understand! :)

                      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