Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. QStandardItemModel doesn't display data
Qt 6.11 is out! See what's new in the release blog

QStandardItemModel doesn't display data

Scheduled Pinned Locked Moved Solved General and Desktop
23 Posts 5 Posters 23.9k Views 4 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.
  • VRoninV VRonin

    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

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

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

    1 Reply Last reply
    0
    • VRoninV Offline
      VRoninV Offline
      VRonin
      wrote on last edited by VRonin
      #13

      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());
      

      "La mort n'est rien, mais vivre vaincu et sans gloire, c'est mourir tous les jours"
      ~Napoleon Bonaparte

      On a crusade to banish setIndexWidget() from the holy land of Qt

      G 2 Replies Last reply
      0
      • VRoninV VRonin

        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());
        
        G Offline
        G Offline
        gabor53
        wrote on last edited by
        #14

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

        1 Reply Last reply
        0
        • VRoninV VRonin

          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());
          
          G Offline
          G Offline
          gabor53
          wrote on last edited by
          #15

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

          1 Reply Last reply
          0
          • VRoninV Offline
            VRoninV Offline
            VRonin
            wrote on last edited by VRonin
            #16

            do you have any events linked to selection or click?

            "La mort n'est rien, mais vivre vaincu et sans gloire, c'est mourir tous les jours"
            ~Napoleon Bonaparte

            On a crusade to banish setIndexWidget() from the holy land of Qt

            G 2 Replies Last reply
            0
            • VRoninV VRonin

              do you have any events linked to selection or click?

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

              @VRonin

              No. Or at least not yet.

              1 Reply Last reply
              0
              • VRoninV VRonin

                do you have any events linked to selection or click?

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

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

                1 Reply Last reply
                0
                • VRoninV Offline
                  VRoninV Offline
                  VRonin
                  wrote on last edited by VRonin
                  #19

                  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();
                  }
                  

                  "La mort n'est rien, mais vivre vaincu et sans gloire, c'est mourir tous les jours"
                  ~Napoleon Bonaparte

                  On a crusade to banish setIndexWidget() from the holy land of Qt

                  G 2 Replies Last reply
                  3
                  • VRoninV VRonin

                    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();
                    }
                    
                    G Offline
                    G Offline
                    gabor53
                    wrote on last edited by
                    #20

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

                    1 Reply Last reply
                    0
                    • VRoninV VRonin

                      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();
                      }
                      
                      G Offline
                      G Offline
                      gabor53
                      wrote on last edited by
                      #21

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

                      1 Reply Last reply
                      0
                      • VRoninV Offline
                        VRoninV Offline
                        VRonin
                        wrote on last edited by
                        #22

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

                        "La mort n'est rien, mais vivre vaincu et sans gloire, c'est mourir tous les jours"
                        ~Napoleon Bonaparte

                        On a crusade to banish setIndexWidget() from the holy land of Qt

                        G 1 Reply Last reply
                        1
                        • VRoninV VRonin

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

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

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

                          1 Reply Last reply
                          1

                          • Login

                          • Login or register to search.
                          • First post
                            Last post
                          0
                          • Categories
                          • Recent
                          • Tags
                          • Popular
                          • Users
                          • Groups
                          • Search
                          • Get Qt
                          • Unsolved