Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. Qt Creator and other tools
  4. Return items (in QString form) from a QSqlTableModel
Forum Updated to NodeBB v4.3 + New Features

Return items (in QString form) from a QSqlTableModel

Scheduled Pinned Locked Moved Qt Creator and other tools
14 Posts 6 Posters 10.0k Views 1 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.
  • F Offline
    F Offline
    freecamellia
    wrote on last edited by
    #1

    Hi,

    I want return one item (in QString form) from a QSqlTableModel that I maked, how can I do this?
    is the QSqlQueryModel class useful?

    1 Reply Last reply
    0
    • Q Offline
      Q Offline
      qxoz
      wrote on last edited by
      #2

      You can get items from QSqlTableModel by QSqlRecord, then convert it as you like.

      1 Reply Last reply
      0
      • F Offline
        F Offline
        freecamellia
        wrote on last edited by
        #3

        Hi,

        I found that The QSqlRecord class cant retrieve data from QSqlTableModel object, it just write/update. In the other hand I think that QSqlQueryModel can do this.

        Show me how it works with QSqlRecord if you never use it.

        1 Reply Last reply
        0
        • A Offline
          A Offline
          andre
          wrote on last edited by
          #4

          I am really not sure what you want to achieve. Could you try, perhaps with some pseudo-code, what you are after?

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

            I want retrieve records or fields from an QSqlTableModel object, but not directly from the database through the QSqlQueryModel class.

            I want this because, in addition to insert a QSqlTableModel object it in a view, I want retrieve/display one field from this QSqlTableModel once I click in the corresponding field in the view.

            Since the model is there, I prefer exploit it; but tell me if my purpose is logic or not?

            All that is for defining a slot of the Widget:

            @void Widget::itemSelected(QModelIndex i)
            {
            int r = i.row();
            QSqlQueryModel *queryModel = new QSqlQueryModel;
            queryModel->setQuery("SELECT * FROM table");
            QString text = queryModel->record(r).value("text").toString();
            ui->textText->setPlainText(text);
            }@

            1 Reply Last reply
            0
            • G Offline
              G Offline
              goetz
              wrote on last edited by
              #6

              Why don't you use the model's data() method?

              http://www.catb.org/~esr/faqs/smart-questions.html

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

                I don't understand well what you are trying to do. The selection that you use to point one row in your qsqlmodel comes from another widget? And is it possible that it does not tell you something that can help filtering the query? I mean, you are doing a select * to get a single row, and worst, a single column. At least change your query to reflect the column you need and the row you need. Moreover, I would load data once into qsqlquerymodel and use such model to populate all the widgets in your view(s) without having to redo a query against the database (unless you need to get refreshed data).

                1 Reply Last reply
                0
                • F Offline
                  F Offline
                  freecamellia
                  wrote on last edited by
                  #8

                  Hi,
                  What I need is:

                  I want display a listView of one sorted/filtered(commanded by a lineEdit) column from a database table.

                  And then when an item of the this view is selected, a textEdit displays an other corresponding field (in the same record as the selected item) ; so the content of the textEdit must change according to the selected item.

                  So what are the convenient model classes to use and steps to establish this?

                  I hope its clear now!
                  regards!

                  1 Reply Last reply
                  0
                  • G Offline
                    G Offline
                    goetz
                    wrote on last edited by
                    #9
                    • Have your model contain all relevant data.
                    • Set the desired column to display with QListView's setModelColumn() method
                    • get the text for the textEdit with the data() method from the model

                    http://www.catb.org/~esr/faqs/smart-questions.html

                    1 Reply Last reply
                    0
                    • F Offline
                      F Offline
                      fluca1978
                      wrote on last edited by
                      #10

                      [quote author="Volker" date="1322785621"]* Have your model contain all relevant data.

                      • Set the desired column to display with QListView's setModelColumn() method
                      • get the text for the textEdit with the data() method from the model[/quote]

                      Agree! You have to load data once and to display single parts as you want in the widgets you want.

                      1 Reply Last reply
                      0
                      • A Offline
                        A Offline
                        andre
                        wrote on last edited by
                        #11

                        And for the filtering/sorting, you use a [[doc:QSortFilterProxyModel]]. You do that by setting your SQL model as the source model of the proxy model, and then setting the proxy model as the source for the view.

                        1 Reply Last reply
                        0
                        • Q Offline
                          Q Offline
                          qxoz
                          wrote on last edited by
                          #12

                          [quote author="freecamellia" date="1322756692"]Hi,

                          I found that The QSqlRecord class cant retrieve data from QSqlTableModel object, it just write/update. In the other hand I think that QSqlQueryModel can do this.

                          Show me how it works with QSqlRecord if you never use it.[/quote]

                          Maybe i didn't understand you but i meant this:
                          @ QSqlTableModel tab;
                          ...
                          QSqlRecord rec = tab.record(rowIndex);
                          QString str = rec.value(columnName).toString()@

                          1 Reply Last reply
                          0
                          • Q Offline
                            Q Offline
                            qxoz
                            wrote on last edited by
                            #13

                            In your case
                            [quote author="Volker" date="1322785621"]

                            • Have your model contain all relevant data.
                            • Set the desired column to display with QListView's setModelColumn() method
                            • get the text for the textEdit with the data() method from the model[/quote]

                            is better solution.

                            1 Reply Last reply
                            0
                            • F Offline
                              F Offline
                              fguimaraes
                              wrote on last edited by
                              #14

                              [quote author="Volker" date="1322785621"]* Have your model contain all relevant data.

                              • Set the desired column to display with QListView's setModelColumn() method
                              • get the text for the textEdit with the data() method from the model[/quote]

                              Voker is right.
                              You should try something like this:
                              @
                              textEdit->setText(listView->currentIndex().data(Qt::DisplayRole).toString());
                              @

                              Birth, death, rebirth, though, and constantly progress, that is the law.

                              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