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 3.0k 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.
  • 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 Online
        JonBJ Online
        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 Online
            JonBJ Online
            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