Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

QStandardItemModel doesn't display data



  • @mjsurette
    I initialize the db when the program starts in main.cpp like this:

        QSqlDatabase db;
        QString fileQstring = "C:/Programming/Projects/FolkFriends_1_0/db.db";
    
        db = QSqlDatabase::addDatabase ("QSQLITE");
        db.setDatabaseName (fileQstring);
        qDebug() << "Connection Display created in main.cpp. ";
    
        bool OK = db.open ();
    
        if(OK == true)
            {
                qDebug() << "The db (MainWindow) is open!";
            }
        else
            {
                qDebug() << "The db (MainWindow) is not open!";
            }
    
        if(!db.open ())
            {
                qDebug() << "The database (db) is NOT open!" << db.lastError ();
    
            }
    

    The connection works at other places.
    Last error displays no error. I used the same Qt version (Qt 5.7) throughout the program.



  • Hi,

    can u check few things,

    1. Is db opened succesfully.
    2. values are present in dB of the respective tables.
    3. while retreiving provide qDebug() statements, so u can verify the data.
    4. Check the table name which u have provided,is it correct?.

    Thanks,



  • Construct the model and then add it to view.


  • Lifetime Qt Champion

    Hi,

    You're trying to open your database twice which is wrong.

    Did you check that you are indeed passing in your loop ? Note that your first test will never pass since row1 is initialized at 1.



  • @Pradeep-Kumar
    Hi,
    It has data. I added

     qDebug() << "ID in fix: " << query_fix.value (0).toString ();
    

    and I got the following:
    ID in fix: "4"
    ID in fix: "1"
    ID in fix: "3"
    ID in fix: "2"
    which is exactly what supposed to be there. It just doesn't show up in the table.


  • Lifetime Qt Champion

    You don't set any column count. That's something you have to set either at construction time or before you start adding rows/data.



  • void FixDb::correctFriend()
    {
    qDebug() << "Entered correctFriend!";

    QSqlQuery query_fix  ("SELECT * FROM Items ORDER BY NAME ASC ");
    
    if(query_fix.isActive()==true)
        {
            qDebug() << "The query_fix is active.";
        }
    else
        {
            qDebug() << "The query_fix is NOT active." << query_fix.lastError ();
        }
    
    
    
    QStandardItemModel *fixmodel = new QStandardItemModel(this);
    fixmodel->setHeaderData (0,Qt::Horizontal, QObject::tr("ID"));
    fixmodel->setHeaderData (1,Qt::Horizontal, QObject::tr("Name"));
    fixmodel->setHeaderData (2,Qt::Horizontal, QObject::tr("What is the Friend?"));
    fixmodel->setHeaderData (3,Qt::Horizontal, QObject::tr("The Friend's Picture"));
    fixmodel->setHeaderData (4,Qt::Horizontal, QObject::tr("Material"));
    fixmodel->setHeaderData (5,Qt::Horizontal, QObject::tr("Color"));
    fixmodel->setHeaderData (6,Qt::Horizontal, QObject::tr("Description"));
    fixmodel->setHeaderData (7,Qt::Horizontal, QObject::tr("Adoption Date"));
    fixmodel->setHeaderData (8,Qt::Horizontal, QObject::tr("Signed by"));
    fixmodel->setHeaderData (9,Qt::Horizontal, QObject::tr("History"));
    fixmodel->setHeaderData (10,Qt::Horizontal, QObject::tr("Age"));
    fixmodel->setHeaderData (11,Qt::Horizontal, QObject::tr("Notes"));
    fixmodel->setColumnCount(12);		// as per your column header its 11							
    	
    ui->tableView_fix->horizontalHeader ()->setStyleSheet ("QHeaderView{font: 14pt Arial; color: blue; font-weight: bold;}");
    ui->tableView_fix->verticalHeader ()->setVisible (false);
    ui->tableView_fix->setAlternatingRowColors (true);
    ui->tableView_fix->setStyleSheet ("alternate-background-color: rgb(224,255,248); background-color: white; font: 14pt Arial");
    ui->tableView_fix->setEditTriggers (QAbstractItemView::CurrentChanged);
    
    ui->tableView_fix->setColumnWidth (0,100);
    ui->tableView_fix->setColumnWidth (1,200);
    ui->tableView_fix->setColumnWidth (2,200);
    ui->tableView_fix->setColumnWidth (3,90);
    ui->tableView_fix->setColumnWidth (4,200);
    ui->tableView_fix->setColumnWidth (5,200);
    ui->tableView_fix->setColumnWidth (6,350);
    ui->tableView_fix->setColumnWidth (7,200);
    ui->tableView_fix->setColumnWidth (8,200);
    ui->tableView_fix->setColumnWidth (9,350);
    ui->tableView_fix->setColumnWidth (10,200);
    ui->tableView_fix->setColumnWidth (11,350);
    ui->tableView_fix->setWordWrap (true);
    
    
    for(int row1 = 0; query_fix.next(); ++row1)
        {
     fixmodel->setRowCount(row1);
     fixmodel->setData (fixmodel->index(row1,0),query_fix.value (0).toString());			// oth column value
     fixmodel->setData (fixmodel->index(row1,1),query_fix.value (1).toString ());			// 1th column value	
    fixmodel->setData (fixmodel->index(row1,2),query_fix.value (13).toString ());			// 13th column value
    
     fixPixmap.loadFromData (query_fix.value (2).toByteArray ());					// 2th column value	
     fixPixmap = fixPixmap.scaled (100,100,Qt::KeepAspectRatio);
    
     fixmodel->setData (fixmodel->index (row1,3),fixPixmap,Qt::DecorationRole);
    fixmodel->setData (fixmodel->index(row1,4),query_fix.value (11).toString ());			// 11th column value	
    fixmodel->setData (fixmodel->index(row1,5),query_fix.value (10).toString ());			// 10th column value	
     fixmodel->setData (fixmodel->index(row1,6),query_fix.value (3).toString ());			// 3th column value
    
            monthf = query_fix.value (4).toString ();								// 4th column value
            dayf = query_fix.value (5).toString ();								// 5th column value	
            yearf = query_fix.value (6).toString ();								// 6th column value
            date = monthf + "/" + dayf + "/" + yearf;	
    
    fixmodel->setData (fixmodel->index(row1,5),date);
    fixmodel->setData (fixmodel->index(row1,6),query_fix.value (12).toString ());			// 12th column value
    fixmodel->setData (fixmodel->index(row1,7),query_fix.value (7).toString ());			// 7th column value
    fixmodel->setData (fixmodel->index(row1,8),query_fix.value (8).toString ());			// 8th column value	
    fixmodel->setData (fixmodel->index(row1,9),query_fix.value (9).toString ());			// 9th column value
    
     ui->tableView_fix->setRowHeight (row1,100);
        }
    
    
    ui->tableView_fix->setModel (fixmodel);
    

    }



  • As @SGaist said u need to specify Columncount and rowcount.

    fixmodel->setData (fixmodel->index(row1,5),query_fix.value (10).toString ());

    fixmodel->setData (fixmodel->index(row1,5),date);

    Are u trying to insert different values in same column?.



  • EDIT, sorry, this is a duplicate of @SGaist 's answer

    you are never adding the columns...

    for(int row1 = 1; query_fix.next (); row1++)
            {
                if(row1 == 0)
    

    row1 is never 0



  • @VRonin
    Hi,
    I rewrote a few things. As a result it displays data:

    void FixDb::correctFriend()
    {
        qDebug() << "Entered correctFriend!";
    
        QSqlQuery query_fix  ("SELECT * FROM Items ORDER BY NAME ASC ");
    
        if(query_fix.isActive()==true)
            {
                qDebug() << "The query_fix is active.";
            }
        else
            {
                qDebug() << "The query_fix is NOT active." << query_fix.lastError ();
            }
    
        QStandardItemModel *fixmodel = new QStandardItemModel(this);
    
    
        const QSqlRecord qR = query_fix.record ();
        qDebug() << "The number of records: " << qR;
        fixmodel->insertColumns (0, qR.count());
    
        for(int row1 = 0; query_fix.next (); row1++)
            {
                fixmodel->insertRow (row1);
                fixmodel->setData (fixmodel->index(row1,0),query_fix.value (0).toString ());
                fixmodel->setData (fixmodel->index(row1,1),query_fix.value (1).toString ());
                fixmodel->setData (fixmodel->index(row1,2),query_fix.value (13).toString ());
    
                qDebug() << "ID in fix: " << query_fix.value (0).toString ();
    
                fixPixmap.loadFromData (query_fix.value (2).toByteArray ());
                fixPixmap = fixPixmap.scaled (100,100,Qt::KeepAspectRatio);
    
                fixmodel->setData (fixmodel->index (row1,3),fixPixmap,Qt::DecorationRole);
                fixmodel->setData (fixmodel->index(row1,4),query_fix.value (11).toString ());
                fixmodel->setData (fixmodel->index(row1,5),query_fix.value (10).toString ());
                fixmodel->setData (fixmodel->index(row1,6),query_fix.value (3).toString ());
    
                monthf = query_fix.value (4).toString ();
                dayf = query_fix.value (5).toString ();
                yearf = query_fix.value (6).toString ();
                date = monthf + "/" + dayf + "/" + yearf;
    
                fixmodel->setData (fixmodel->index(row1,7),date);
                fixmodel->setData (fixmodel->index(row1,8),query_fix.value (12).toString ());
                fixmodel->setData (fixmodel->index(row1,9),query_fix.value (7).toString ());
                fixmodel->setData (fixmodel->index(row1,10),query_fix.value (8).toString ());
                fixmodel->setData (fixmodel->index(row1,11),query_fix.value (9).toString ());
                ui->tableView_fix->setRowHeight (row1,100);
    
                ui->tableView_fix->horizontalHeader ()->setStyleSheet ("QHeaderView{font: 14pt Arial; color: blue; font-weight: bold;text-decoration: underline;}");
                ui->tableView_fix->verticalHeader ()->setVisible (false);
                ui->tableView_fix->setAlternatingRowColors (true);
                ui->tableView_fix->setStyleSheet ("alternate-background-color: rgb(224,255,248); background-color: white; font: 14pt Arial");
                ui->tableView_fix->setEditTriggers (QAbstractItemView::CurrentChanged);
                fixmodel->setHeaderData (0,Qt::Horizontal, QObject::tr("ID"));
                fixmodel->setHeaderData (1,Qt::Horizontal, QObject::tr("Name"));
                fixmodel->setHeaderData (2,Qt::Horizontal, QObject::tr("What is the Friend?"));
                fixmodel->setHeaderData (3,Qt::Horizontal, QObject::tr("Picture"));
                fixmodel->setHeaderData (4,Qt::Horizontal, QObject::tr("Material"));
                fixmodel->setHeaderData (5,Qt::Horizontal, QObject::tr("Color"));
                fixmodel->setHeaderData (6,Qt::Horizontal, QObject::tr("Description"));
                fixmodel->setHeaderData (7,Qt::Horizontal, QObject::tr("Adoption Date"));
                fixmodel->setHeaderData (8,Qt::Horizontal, QObject::tr("Signed by"));
                fixmodel->setHeaderData (9,Qt::Horizontal, QObject::tr("History"));
                fixmodel->setHeaderData (10,Qt::Horizontal, QObject::tr("Age"));
                fixmodel->setHeaderData (11,Qt::Horizontal, QObject::tr("Notes"));
    
                ui->tableView_fix->setColumnWidth (0,1);
                ui->tableView_fix->setColumnWidth (1,200);
                ui->tableView_fix->setColumnWidth (2,200);
                ui->tableView_fix->setColumnWidth (3,90);
                ui->tableView_fix->setColumnWidth (4,200);
                ui->tableView_fix->setColumnWidth (5,200);
                ui->tableView_fix->setColumnWidth (6,350);
                ui->tableView_fix->setColumnWidth (7,200);
                ui->tableView_fix->setColumnWidth (8,200);
                ui->tableView_fix->setColumnWidth (9,350);
                ui->tableView_fix->setColumnWidth (10,200);
                ui->tableView_fix->setColumnWidth (11,350);
    
                ui->tableView_fix->setWordWrap (true);
    
                ui->tableView_fix->setModel (fixmodel);
            }
    }
    

    The only problems I found:
    1.) The first row's height is much less than the rest of the rows.
    2.) The ID column is completely missing.
    Is this the model's issue or i cut the first column off somewhere?
    Thank you for all your help.



  • fixmodel->setData (fixmodel->index(row1,0),query_fix.value (0).toString ());

    don't call toString, setdata wants a QVariant, no need to convert anything

    The problems you mentioned look a lot like view issues rather than model issues? are you using a custom view or delegate?

    this

    monthf = query_fix.value (4).toString ();
                dayf = query_fix.value (5).toString ();
                yearf = query_fix.value (6).toString ();
                date = monthf + "/" + dayf + "/" + yearf;
    fixmodel->setData (fixmodel->index(row1,7),date);
    

    is bad on so many levels.

    use

    fixmodel->setData (fixmodel->index(row1,7),QDate(query_fix.value (6).toInt(),query_fix.value (4).toInt(),query_fix.value (5).toInt());
    


  • @VRonin said in QStandardItemModel doesn't display data:

    fixmodel->setData (fixmodel->index(row1,7),QDate(query_fix.value (6).toInt(),query_fix.value (4).toInt(),query_fix.value (5).toInt());

    I did the changes you recommended. The change is that in the Name column in the first row I have the ID. If I click on anywhere on the table, the ID from the first row disappears, replaced by the Name and the ID columns is still completely missing.



  • @VRonin
    As it looks It displays the database from column 2.



  • do you have any events linked to selection or click?



  • @VRonin

    No. Or at least not yet.



  • @VRonin
    The following code displays the ID column:

    void FixDb::correctFriend()
    {
        qDebug() << "Entered correctFriend!";
    
        QSqlQuery query_fix  ("SELECT ID FROM Items ORDER BY NAME ASC ");
    
        if(query_fix.isActive()==true)
            {
                qDebug() << "The query_fix is active.";
            }
        else
            {
                qDebug() << "The query_fix is NOT active." << query_fix.lastError ();
            }
    
        while(query_fix.next ())
            {
                QString tempID = query_fix.value (0).toString ();
                qDebug() << "TempID: " << tempID;
            }
    
        QStandardItemModel *fixmodel = new QStandardItemModel(this);
        ui->tableView_fix->setModel (fixmodel);
    
        const QSqlRecord qR = query_fix.record ();
        qDebug() << "The number of records: " << qR;
        fixmodel->insertColumns (0,qR.count ());
    
        for(int row1 = 0; query_fix.next (); row1++)
            {
                fixmodel->insertRow (row1);
            }
    
        ui->tableView_fix->horizontalHeader ()->setStyleSheet ("QHeaderView{font: 14pt Arial; color: blue; font-weight: bold;text-decoration: underline;}");
        ui->tableView_fix->setAlternatingRowColors (true);
        ui->tableView_fix->setStyleSheet ("alternate-background-color: rgb(224,255,248); background-color: white; font: 14pt Arial");
    
        fixmodel->setHeaderData (0,Qt::Horizontal, QObject::tr("ID"));
    
        ui->tableView_fix->setColumnWidth (0,100);
    }
    
    

    but when I add

     fixmodel->setData (fixmodel->index(row1,0),query_fix.value (0));
    

    after insertRow the column header disappears and the tableview is empty.



  • I have a strong suspect the problem lies in the view, could you try the code below and see if the column is displayed correctly?

    void FixDb::correctFriend()
    {
        qDebug() << "Entered correctFriend!";
    
        QSqlQuery query_fix  ("SELECT ID FROM Items ORDER BY NAME ASC ");
    
        if(query_fix.isActive()==true)
            {
                qDebug() << "The query_fix is active.";
            }
        else
            {
                qDebug() << "The query_fix is NOT active." << query_fix.lastError ();
            }
    
        while(query_fix.next ())
            {
                QString tempID = query_fix.value (0).toString ();
                qDebug() << "TempID: " << tempID;
            }
    
        QStandardItemModel *fixmodel = new QStandardItemModel(this);
    QTableView* tempView=new QTableView;
    tempView->setAttribute( Qt::WA_DeleteOnClose ); // To prevent memory leak
        tempView->setModel (fixmodel);
    
        const QSqlRecord qR = query_fix.record ();
        qDebug() << "The number of records: " << qR;
        fixmodel->insertColumns (0,qR.count ());
    
        for(int row1 = 0; query_fix.next (); row1++)
            {
                fixmodel->insertRow (row1);
    fixmodel->setData (fixmodel->index(row1,0),query_fix.value (0));
            }
    
       
    
        fixmodel->setHeaderData (0,Qt::Horizontal, QObject::tr("ID"));
    
        tempView->show();
    }
    


  • @VRonin
    It displays only the original tableview from the ui. I can't see tempView .



  • @VRonin
    I figured it out, but the new table shows only the header row with the title ID.



  • could you qDebug() << "Rows: " << fixmodel->rowCount() << " Columns: " << fixmodel->columnCount() just before tempView->show();?



  • @VRonin
    I redid the whole class and it works now. I think there was something wrong with QTableView in Editor. Thank you for your help.


Log in to reply