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. 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.8k 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.
  • G Offline
    G Offline
    gabor53
    wrote on last edited by
    #1

    Hi,
    I'm using QStandardItemModel to display the contents of a db in TableView. The following code produces no error messages, but doesn't display anything though an other version of the same code works well.

    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);
        ui->tableView_fix->setModel (fixmodel);
    
        for(int row1 = 1; query_fix.next (); row1++)
            {
                if(row1 == 0)
                    {
                        const QSqlRecord qR = query_fix.record ();
                        qDebug() << "The number of records: " << qR;
                        fixmodel->insertColumns (0, qR.count());
                    }
                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 ());
    
                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,5),date);
                fixmodel->setData (fixmodel->index(row1,6),query_fix.value (12).toString ());
                fixmodel->setData (fixmodel->index(row1,7),query_fix.value (7).toString ());
                fixmodel->setData (fixmodel->index(row1,8),query_fix.value (8).toString ());
                fixmodel->setData (fixmodel->index(row1,9),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;}");
        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("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"));
    
        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);
    
    }
    

    Please tell me what's wrong with it. Thank you.

    1 Reply Last reply
    0
    • M Offline
      M Offline
      mjsurette
      wrote on last edited by
      #2

      I don't see any errors, but I do have a suggestion to use QSqlTableModel so you don't have to transfer the data yourself. It will simplify the code.

      In my experience nothing displayed in your view is usually caused by a query gone wrong, but that should be printed in your lastError(). You don't show the output of your qDebug()s so I have to assume everything is normal there.

      Is your default database initialised properly?

      What version of Qt worked and what version doesn't?

      Mike

      G 1 Reply Last reply
      2
      • M mjsurette

        I don't see any errors, but I do have a suggestion to use QSqlTableModel so you don't have to transfer the data yourself. It will simplify the code.

        In my experience nothing displayed in your view is usually caused by a query gone wrong, but that should be printed in your lastError(). You don't show the output of your qDebug()s so I have to assume everything is normal there.

        Is your default database initialised properly?

        What version of Qt worked and what version doesn't?

        Mike

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

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

        1 Reply Last reply
        0
        • Pradeep KumarP Offline
          Pradeep KumarP Offline
          Pradeep Kumar
          wrote on last edited by
          #4

          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,

          Pradeep Kumar
          Qt,QML Developer

          1 Reply Last reply
          2
          • Pradeep KumarP Offline
            Pradeep KumarP Offline
            Pradeep Kumar
            wrote on last edited by
            #5

            Construct the model and then add it to view.

            Pradeep Kumar
            Qt,QML Developer

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

              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.

              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
              2
              • Pradeep KumarP Pradeep Kumar

                Construct the model and then add it to view.

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

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

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

                  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.

                  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
                  2
                  • Pradeep KumarP Offline
                    Pradeep KumarP Offline
                    Pradeep Kumar
                    wrote on last edited by Pradeep Kumar
                    #9

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

                    }

                    Pradeep Kumar
                    Qt,QML Developer

                    1 Reply Last reply
                    1
                    • Pradeep KumarP Offline
                      Pradeep KumarP Offline
                      Pradeep Kumar
                      wrote on last edited by Pradeep Kumar
                      #10

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

                      Pradeep Kumar
                      Qt,QML Developer

                      1 Reply Last reply
                      3
                      • VRoninV Offline
                        VRoninV Offline
                        VRonin
                        wrote on last edited by VRonin
                        #11

                        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

                        "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

                          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

                                          • Login

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