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. Adding rows to a tableView with QSqlTableModel

Adding rows to a tableView with QSqlTableModel

Scheduled Pinned Locked Moved Solved General and Desktop
5 Posts 2 Posters 2.3k Views 2 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.
  • G Offline
    G Offline
    gabor53
    wrote on last edited by
    #1

    Hi,
    I'm trying to add lines from a database to tableView with QSqlTableModel.

    void MainWindow::Addview()
    {
        connectionNames = db.connectionNames ();
    
        if((connectionNames.contains ("qt_sql_default_connection")) == false)
            {
                db = QSqlDatabase::addDatabase ("QSQLITE");
                db.setDatabaseName (fileQstring);
                qDebug() << "Connection Display created in connection(). ";
            }
    
        qDebug() << "Open connection names in Addview: " << db.connectionNames ();
    
    
        if(!db.open ())
            {
                qDebug() << "The database (db) is NOT open!";
            }
    
        QSqlQuery query_main  ("SELECT ID, Name,Pic, Description FROM Items ORDER BY NAME ASC ");
    
        if(query_main.isActive()==true)
            {
                qDebug() << "The query is active.";
            }
        else
            {
                qDebug() << "The query is NOT active." << query_main.lastError ();
            }
    
    
        QSqlTableModel * DisplayModel = new QSqlTableModel(this);
        DisplayModel->setTable("Items");
        DisplayModel->select ();
    
    //    ui->tableView_Display->setModel(DisplayModel);
    
        for(int row1 = 0; query_main.next (); row1++)
            {
                if(row1 == 0)
                    {
                        const QSqlRecord qRec = query_main.record();
                        qDebug() << "The number of records: " << qRec;
                        DisplayModel->insertColumns (0, qRec.count());
                    }
    
                DisplayModel->insertRecord (row1,query_main.value (0).toString ());
    
            }
    }
    

    Which is the bests way to do it, when each record includes QString fields and a pixmap field?
    The solution I have generates an error message:
    no viable conversion from QString to const QSqlRecord.
    Thank you.

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

      Hi,

      You should create a QSqlRecord with the QSqlField you need and then use insertRecord on your QSqlTableModel.

      You error comes because you are passing a QString to a function that expects a QSqlRecord.

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

      G 1 Reply Last reply
      1
      • SGaistS SGaist

        Hi,

        You should create a QSqlRecord with the QSqlField you need and then use insertRecord on your QSqlTableModel.

        You error comes because you are passing a QString to a function that expects a QSqlRecord.

        G Offline
        G Offline
        gabor53
        wrote on last edited by
        #3

        Hi @SGaist
        Now I have the following:

           QSqlQuery query_main  ("SELECT ID, Name,Pic, Description FROM Items ORDER BY NAME ASC ");
        
            if(query_main.isActive()==true)
                {
                    qDebug() << "The query is active.";
                }
            else
                {
                    qDebug() << "The query is NOT active." << query_main.lastError ();
                }
        
        
            QSqlTableModel * DisplayModel = new QSqlTableModel(this);
        
        
            ui->tableView_Display->setModel(DisplayModel);
        
            for(int row1 = 0; query_main.next (); row1++)
                {
                    if(row1 == 0)
                        {
                            const QSqlRecord qRec = query_main.record();
                            qDebug() << "The number of records: " << qRec;
                            DisplayModel->insertColumns (0, qRec.count());
        
                        }
        
                    DisplayModel->insertRow (row1);
                    DisplayModel->setData (DisplayModel->index (row1,0),query_main.value (0).toString ());
                    DisplayModel->setData (DisplayModel->index (row1,1),query_main.value (1).toString ());
                    Pixmap.loadFromData (query_main.value (2).toByteArray ());
                    Pixmap = Pixmap.scaled (100,100,Qt::KeepAspectRatio);
                    DisplayModel->setData (DisplayModel->index (row1,2),Pixmap,Qt::DecorationRole);
                    DisplayModel->setData (DisplayModel->index (row1,3), query_main.value (3).toString ());
                    ui->tableView_Display->setRowHeight (row1,100);
                }
            ui->tableView_Display->horizontalHeader ()->setStyleSheet ("QHeaderView{font: 14pt Arial; color: blue; font-weight: bold;}");
            ui->tableView_Display->verticalHeader ()->setVisible (false);
            ui->tableView_Display->setAlternatingRowColors (true);
            ui->tableView_Display->setStyleSheet ("alternate-background-color: rgb(224,255,248); background-color: white; font: 14pt Arial");
            ui->tableView_Display->setEditTriggers (QAbstractItemView::NoEditTriggers);
            DisplayModel->setHeaderData (0,Qt::Horizontal, QObject::tr ("ID"));
            DisplayModel->setHeaderData (1,Qt::Horizontal, QObject::tr ("Name"));
            DisplayModel->setHeaderData (2,Qt::Horizontal, QObject::tr ("Picture"));
            DisplayModel->setHeaderData (3,Qt::Horizontal, QObject::tr ("Description"));
            ui->tableView_Display->setColumnWidth (1,1);
            ui->tableView_Display->setColumnWidth (1,200);
            ui->tableView_Display->setColumnWidth (2,90);
            ui->tableView_Display->setColumnWidth (3,340);
            ui->tableView_Display->setWordWrap (true);
        }
        

        It displays the headers in the table but no data. Please tell me what's missing or incorrect.

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

          QSqlTableModel is used to interact with a SQL table. You orignal implementation was correct it that regard. Now it looks like you are trying to use it like a QStandardItemModel.

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

          G 1 Reply Last reply
          2
          • SGaistS SGaist

            QSqlTableModel is used to interact with a SQL table. You orignal implementation was correct it that regard. Now it looks like you are trying to use it like a QStandardItemModel.

            G Offline
            G Offline
            gabor53
            wrote on last edited by
            #5

            @SGaist
            I went back to my original standarditemmodel. Thank you.

            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