Adding rows to a tableView with QSqlTableModel



  • 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.


  • Lifetime Qt Champion

    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.



  • 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.


  • Lifetime Qt Champion

    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.



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


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.