Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. QML and Qt Quick
  4. How in QML get data with QSqlQueryModel
Qt 6.11 is out! See what's new in the release blog

How in QML get data with QSqlQueryModel

Scheduled Pinned Locked Moved Unsolved QML and Qt Quick
11 Posts 3 Posters 1.3k 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.
  • M Offline
    M Offline
    Mihaill
    wrote on last edited by
    #1

    Hi!
    I have model QSqlQueryModel.
    Me need get data with model in QML.
    This code not work:

    console.log("printFormatModel: " + printFormatModel.data(printFormatModel.index(1,1),1))
    

    Debug return:

    qml: printFormatModel: undefined
    

    С++ have metod toInt(), but QML not have this metod.

    1 Reply Last reply
    0
    • SGaistS Offline
      SGaistS Offline
      SGaist
      Lifetime Qt Champion
      wrote on last edited by
      #2

      Hi,

      It's explained in the Using C++ Models with Qt Quick Views chapter of Qt's documentation.

      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
      0
      • M Offline
        M Offline
        Mihaill
        wrote on last edited by
        #3

        The example is not what I need. I need to go through the entire model and get the data from the right cells. The model is reflected only in the Listview, not all columns of the model are reflected.

        1 Reply Last reply
        0
        • IntruderExcluderI Offline
          IntruderExcluderI Offline
          IntruderExcluder
          wrote on last edited by
          #4

          Second parameter of data method is model role. According to your example you are trying to get DecorationRole. Maybe role is simply set wrong?

          1 Reply Last reply
          1
          • M Offline
            M Offline
            Mihaill
            wrote on last edited by
            #5

            It's too not works:

            printFormatModel.data(printFormatModel.index(1,1),"idRole")
            

            It's my model:

            #include "printformatmodel.h"
            
            PrintFormatModel::PrintFormatModel(QObject *parent) : QSqlQueryModel (parent)
            {
            
            }
            
            QVariant PrintFormatModel::data(const QModelIndex &index, int role) const
            {
                int columnId = role - Qt::UserRole - 1;
                // Создаём индекс с помощью новоиспечённого ID колонки
                QModelIndex modelIndex = this->index(index.row(), columnId);
            
                /* И с помощью уже метода data() базового класса
                 * вытаскиваем данные для таблицы из модели
                 * */
                return QSqlQueryModel::data(modelIndex, Qt::DisplayRole);
            }
            
            QVariantMap PrintFormatModel::get(int idx) const
            {
                QVariantMap map;
                foreach(int k, roleNames().keys()) {
                    map[roleNames().value(k)] = data(index(idx, 0), k);
                }
                return map;
            }
            
            QHash<int, QByteArray> PrintFormatModel::roleNames() const
            {
                QHash<int, QByteArray> roles;
                roles[idRole]="id";
                roles[productIdColumn]="product_id";
                roles[printFormatId]="print_format_id";
                roles[x1]="x1";
                roles[y1]="y1";
                roles[fontSize]="font_size";
                roles[printFormatKey]="print_format_key";
                roles[printFormatText]="print_format_text";
                roles[boolPrintFormatKey1]="bool_print_format_key_1";
                roles[boolPrintFormatKey2]="bool_print_format_key_2";
                roles[boolPrintFormatKey3]="bool_print_format_key_3";
                roles[boolPrintFormatKey4]="bool_print_format_key_4";
                roles[boolPrintFormatKey1Text]="bool_print_format_key_4_text";
                return roles;
            }
            
            void PrintFormatModel::setId(int idNumber)
            {
                productId = idNumber;
            }
            
            void PrintFormatModel::updateModel(int id)
            {
                this->setQuery("select id, product_id, print_format_id, x1, y1, font_size, print_format_key,"
                               " print_format_text, bool_print_format_key_1, bool_print_format_key_2,"
                               " bool_print_format_key_3, bool_print_format_key_4, bool_print_format_key_4_text"
                               " from ProductConfigForPrint WHERE product_id = " + QString::number(id));
            }
            
            int PrintFormatModel::getId(int row)
            {
                return this->data(this->index(row, 0), idRole).toInt();
            }
            
            
            1 Reply Last reply
            0
            • IntruderExcluderI Offline
              IntruderExcluderI Offline
              IntruderExcluder
              wrote on last edited by
              #6

              Role parameter should be integer, not a string. Look at method definition. You can at least try to print what id you get and result of your columnId, make sure they are valid.

              1 Reply Last reply
              0
              • M Offline
                M Offline
                Mihaill
                wrote on last edited by
                #7

                I do not understand you. I tried to point out the numbers, but it did not help me.

                1 Reply Last reply
                0
                • IntruderExcluderI Offline
                  IntruderExcluderI Offline
                  IntruderExcluder
                  wrote on last edited by
                  #8

                  You cannot write

                  printFormatModel.data(printFormatModel.index(1,1),"idRole")
                  

                  Second parametr in this case for method data would be "idRole" which is string, while model waits for some integer value. When you use it like

                  printFormatModel.data(printFormatModel.index(1,1),1)
                  

                  Your second parameter would be 1. But according to your code modelIndex at PrintFormatModel::data would be invalid, since Qt::UserRole is equal to 256, you will get 1 - 256 - 1 for columnId.

                  1 Reply Last reply
                  0
                  • M Offline
                    M Offline
                    Mihaill
                    wrote on last edited by
                    #9

                    So what to do?

                    1 Reply Last reply
                    0
                    • IntruderExcluderI Offline
                      IntruderExcluderI Offline
                      IntruderExcluder
                      wrote on last edited by IntruderExcluder
                      #10

                      New thing named "read docs"? It is your code, use debugger, we do not know what are you trying to achieve.

                      1 Reply Last reply
                      0
                      • M Offline
                        M Offline
                        Mihaill
                        wrote on last edited by
                        #11

                        I did not find the necessary information in the documentation. I want to get data from the model from the cell I want.

                        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