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. tableView->setModel() does nothing

tableView->setModel() does nothing

Scheduled Pinned Locked Moved Solved General and Desktop
23 Posts 4 Posters 5.0k Views
  • 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.
  • ? Offline
    ? Offline
    A Former User
    wrote on last edited by
    #1

    Hello,
    I've been trying to set the horizontal header of an existing tableWidget. But for some reason when applying it with setModel() nothing changes.
    However, applying the results of an SQL query works just fine.

        qm = new QSqlQueryModel();
        qm->setHeaderData(0, Qt::Horizontal, tr("Bezeichnung"));
        qm->setHeaderData(1, Qt::Horizontal, tr("Artikelnummer"));
        qm->setHeaderData(2, Qt::Horizontal, tr("Menge"));
        qm->setHeaderData(3, Qt::Horizontal, tr("Einzelpreis"));
        qm->setHeaderData(4, Qt::Horizontal, tr("Gesamtpreis"));
        //ui->tableView->horizontalHeader()->setModel(qm);
        ui->tableView->setModel(qm);
        //ui->tableView->show();
    
    VineelaV 2 Replies Last reply
    0
    • ? A Former User

      Hello,
      I've been trying to set the horizontal header of an existing tableWidget. But for some reason when applying it with setModel() nothing changes.
      However, applying the results of an SQL query works just fine.

          qm = new QSqlQueryModel();
          qm->setHeaderData(0, Qt::Horizontal, tr("Bezeichnung"));
          qm->setHeaderData(1, Qt::Horizontal, tr("Artikelnummer"));
          qm->setHeaderData(2, Qt::Horizontal, tr("Menge"));
          qm->setHeaderData(3, Qt::Horizontal, tr("Einzelpreis"));
          qm->setHeaderData(4, Qt::Horizontal, tr("Gesamtpreis"));
          //ui->tableView->horizontalHeader()->setModel(qm);
          ui->tableView->setModel(qm);
          //ui->tableView->show();
      
      VineelaV Offline
      VineelaV Offline
      Vineela
      wrote on last edited by
      #2

      @SnuggleKat

          QSqlTableModel* qm = new QSqlTableModel(this);
      
           qm->setHeaderData (0,Qt::Horizontal, QObject::tr ("Bezeichnung"));
             qm->setHeaderData (1,Qt::Horizontal, QObject::tr ("Artikelnummer"));
               qm->setHeaderData (2,Qt::Horizontal, QObject::tr ("Menge"));
      
      ui->tableView->setModel(qm);
      
      

      try this.

      ? 1 Reply Last reply
      0
      • VineelaV Vineela

        @SnuggleKat

            QSqlTableModel* qm = new QSqlTableModel(this);
        
             qm->setHeaderData (0,Qt::Horizontal, QObject::tr ("Bezeichnung"));
               qm->setHeaderData (1,Qt::Horizontal, QObject::tr ("Artikelnummer"));
                 qm->setHeaderData (2,Qt::Horizontal, QObject::tr ("Menge"));
        
        ui->tableView->setModel(qm);
        
        

        try this.

        ? Offline
        ? Offline
        A Former User
        wrote on last edited by
        #3

        @Vineela
        Still doesn't work

        VineelaV 1 Reply Last reply
        0
        • ? A Former User

          @Vineela
          Still doesn't work

          VineelaV Offline
          VineelaV Offline
          Vineela
          wrote on last edited by
          #4

          @SnuggleKat is it table view or table widget?

          ? 1 Reply Last reply
          0
          • VineelaV Vineela

            @SnuggleKat is it table view or table widget?

            ? Offline
            ? Offline
            A Former User
            wrote on last edited by
            #5

            @Vineela said in tableView->setModel() does nothing:

            @SnuggleKat is it table view or table widget?

            Table view

            VineelaV 1 Reply Last reply
            0
            • ? A Former User

              Hello,
              I've been trying to set the horizontal header of an existing tableWidget. But for some reason when applying it with setModel() nothing changes.
              However, applying the results of an SQL query works just fine.

                  qm = new QSqlQueryModel();
                  qm->setHeaderData(0, Qt::Horizontal, tr("Bezeichnung"));
                  qm->setHeaderData(1, Qt::Horizontal, tr("Artikelnummer"));
                  qm->setHeaderData(2, Qt::Horizontal, tr("Menge"));
                  qm->setHeaderData(3, Qt::Horizontal, tr("Einzelpreis"));
                  qm->setHeaderData(4, Qt::Horizontal, tr("Gesamtpreis"));
                  //ui->tableView->horizontalHeader()->setModel(qm);
                  ui->tableView->setModel(qm);
                  //ui->tableView->show();
              
              VineelaV Offline
              VineelaV Offline
              Vineela
              wrote on last edited by
              #6

              @SnuggleKat are you fetching it from a particular table?

              ? 1 Reply Last reply
              0
              • VineelaV Vineela

                @SnuggleKat are you fetching it from a particular table?

                ? Offline
                ? Offline
                A Former User
                wrote on last edited by
                #7

                @Vineela said in tableView->setModel() does nothing:

                @SnuggleKat are you fetching it from a particular table?

                Nope. I want to set the table view's header if a certain push button is clicked. There's no dater inside the table

                1 Reply Last reply
                0
                • ? A Former User

                  @Vineela said in tableView->setModel() does nothing:

                  @SnuggleKat is it table view or table widget?

                  Table view

                  VineelaV Offline
                  VineelaV Offline
                  Vineela
                  wrote on last edited by
                  #8

                  @SnuggleKat if yes use this

                   QSqlTableModel* qm = new QSqlTableModel(this);
                       qm->setTable(QStringLiteral("farmer"));
                       qm->setFilter(("anyname = '"+yourvalue+"'"));
                  
                       qm->select();
                       qm->setHeaderData (0,Qt::Horizontal, QObject::tr ("Bezeichnung"));
                         qm->setHeaderData (1,Qt::Horizontal, QObject::tr ("Artikelnummer"));
                           qm->setHeaderData (2,Qt::Horizontal, QObject::tr ("Menge"));
                  
                           ui->tableView->setModel(qm);
                  
                  ? 1 Reply Last reply
                  1
                  • VineelaV Vineela

                    @SnuggleKat if yes use this

                     QSqlTableModel* qm = new QSqlTableModel(this);
                         qm->setTable(QStringLiteral("farmer"));
                         qm->setFilter(("anyname = '"+yourvalue+"'"));
                    
                         qm->select();
                         qm->setHeaderData (0,Qt::Horizontal, QObject::tr ("Bezeichnung"));
                           qm->setHeaderData (1,Qt::Horizontal, QObject::tr ("Artikelnummer"));
                             qm->setHeaderData (2,Qt::Horizontal, QObject::tr ("Menge"));
                    
                             ui->tableView->setModel(qm);
                    
                    ? Offline
                    ? Offline
                    A Former User
                    wrote on last edited by
                    #9

                    @Vineela no success. does it necessarily have to be a QSqlTableModel or QSqlQueryModel?

                    VineelaV 1 Reply Last reply
                    0
                    • ? A Former User

                      @Vineela no success. does it necessarily have to be a QSqlTableModel or QSqlQueryModel?

                      VineelaV Offline
                      VineelaV Offline
                      Vineela
                      wrote on last edited by
                      #10

                      @SnuggleKat yes try QSqlTableModel that worked for me so.

                      ? 2 Replies Last reply
                      0
                      • VineelaV Vineela

                        @SnuggleKat yes try QSqlTableModel that worked for me so.

                        ? Offline
                        ? Offline
                        A Former User
                        wrote on last edited by
                        #11

                        @Vineela said in tableView->setModel() does nothing:

                        s try QSqlTableModel that worked for me so.

                        I see.. Can the setValue parameter be anything?

                        1 Reply Last reply
                        0
                        • VineelaV Vineela

                          @SnuggleKat yes try QSqlTableModel that worked for me so.

                          ? Offline
                          ? Offline
                          A Former User
                          wrote on last edited by A Former User
                          #12

                          @Vineela Well, I have tried using QStandardItem and QStandardItemModel.

                          QStandardItem* d = new QStandardItem("test");
                          QStandardItemModel* im = new QStandardItemModel(this);
                          im->setHorizontalHeaderItem(0, d);
                          ui->tableView->setModel(im);

                          It seems to be working

                          EDIT: Well, Only the first cell of the header takes an actual value where the other 4 remaining cells only show their index number +1.

                              QSqlTableModel* qm = new QSqlTableModel(this);
                              QStringList str_list;
                              str_list << "Bezeichnung" << "Artikelnummer" << "Menge" << "Einzelpreis" << "Gesamtpreis";
                              QStandardItem* hItem = new QStandardItem("Bezeichnung");
                              QStandardItemModel* im = new QStandardItemModel(this);
                          
                              for(int i = 0; i < 5; i++)
                              {
                                  hItem->setData(str_list[i], 0);
                                  im->setHorizontalHeaderItem(i, hItem);
                              }
                              
                              ui->tableView->setModel(im);
                          
                          VineelaV 1 Reply Last reply
                          0
                          • VRoninV Offline
                            VRoninV Offline
                            VRonin
                            wrote on last edited by
                            #13

                            check the basic example in the docs as you can see you need to call at least setTable and select for the model to set up itself

                            "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

                            ? 1 Reply Last reply
                            3
                            • ? A Former User

                              @Vineela Well, I have tried using QStandardItem and QStandardItemModel.

                              QStandardItem* d = new QStandardItem("test");
                              QStandardItemModel* im = new QStandardItemModel(this);
                              im->setHorizontalHeaderItem(0, d);
                              ui->tableView->setModel(im);

                              It seems to be working

                              EDIT: Well, Only the first cell of the header takes an actual value where the other 4 remaining cells only show their index number +1.

                                  QSqlTableModel* qm = new QSqlTableModel(this);
                                  QStringList str_list;
                                  str_list << "Bezeichnung" << "Artikelnummer" << "Menge" << "Einzelpreis" << "Gesamtpreis";
                                  QStandardItem* hItem = new QStandardItem("Bezeichnung");
                                  QStandardItemModel* im = new QStandardItemModel(this);
                              
                                  for(int i = 0; i < 5; i++)
                                  {
                                      hItem->setData(str_list[i], 0);
                                      im->setHorizontalHeaderItem(i, hItem);
                                  }
                                  
                                  ui->tableView->setModel(im);
                              
                              VineelaV Offline
                              VineelaV Offline
                              Vineela
                              wrote on last edited by
                              #14

                              @SnuggleKat if it is solved then mark it as Solved

                              1 Reply Last reply
                              0
                              • VRoninV VRonin

                                check the basic example in the docs as you can see you need to call at least setTable and select for the model to set up itself

                                ? Offline
                                ? Offline
                                A Former User
                                wrote on last edited by
                                #15

                                @VRonin Still doesn't work.
                                This is what I tried:

                                    QSqlTableModel* qm = new QSqlTableModel(this);
                                    qm->setTable(QStringLiteral("table"));
                                    qm->setEditStrategy(QSqlTableModel::OnManualSubmit);
                                    qm->select();
                                    qm->setHeaderData(0, Qt::Horizontal, QObject::tr("Bezeichnung"));
                                    qm->setHeaderData(1, Qt::Horizontal, QObject::tr("Artikelnummer"));
                                    qm->setHeaderData(2, Qt::Horizontal, QObject::tr("Menge"));
                                    qm->setHeaderData(3, Qt::Horizontal, QObject::tr("Einzelpreis"));
                                    qm->setHeaderData(4, Qt::Horizontal, QObject::tr("Gesamtpreis"));
                                    ui->tableView->setModel(qm);
                                    ui->tableView->show(); //don't think this is necessary since the tableView already exists
                                

                                @Vineela The method I have figured out doesn't work completely since it only shows the last header item at the first position

                                VRoninV 1 Reply Last reply
                                0
                                • ? A Former User

                                  @VRonin Still doesn't work.
                                  This is what I tried:

                                      QSqlTableModel* qm = new QSqlTableModel(this);
                                      qm->setTable(QStringLiteral("table"));
                                      qm->setEditStrategy(QSqlTableModel::OnManualSubmit);
                                      qm->select();
                                      qm->setHeaderData(0, Qt::Horizontal, QObject::tr("Bezeichnung"));
                                      qm->setHeaderData(1, Qt::Horizontal, QObject::tr("Artikelnummer"));
                                      qm->setHeaderData(2, Qt::Horizontal, QObject::tr("Menge"));
                                      qm->setHeaderData(3, Qt::Horizontal, QObject::tr("Einzelpreis"));
                                      qm->setHeaderData(4, Qt::Horizontal, QObject::tr("Gesamtpreis"));
                                      ui->tableView->setModel(qm);
                                      ui->tableView->show(); //don't think this is necessary since the tableView already exists
                                  

                                  @Vineela The method I have figured out doesn't work completely since it only shows the last header item at the first position

                                  VRoninV Offline
                                  VRoninV Offline
                                  VRonin
                                  wrote on last edited by
                                  #16

                                  @SnuggleKat said in tableView->setModel() does nothing:

                                  Still doesn't work.

                                  Can you show up how you set up the default database?

                                  "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

                                  ? 1 Reply Last reply
                                  0
                                  • VRoninV VRonin

                                    @SnuggleKat said in tableView->setModel() does nothing:

                                    Still doesn't work.

                                    Can you show up how you set up the default database?

                                    ? Offline
                                    ? Offline
                                    A Former User
                                    wrote on last edited by
                                    #17

                                    @VRonin What do you mean by default database?
                                    I think it's QSqlDatabase::database() since I didn't specify anything but "this" inside the QSqlTableModel constructor.
                                    I have also tried using the connection database as parameter (without executing an SQL query). But it didn't help.
                                    This is the setup:

                                        QString username = ui->lineEdit_username->text();
                                        QString pw_decrypted = ui->lineEdit_pw->text();
                                        QString host = ui->lineEdit_ip->text();
                                    
                                        db = QSqlDatabase::addDatabase("QMYSQL");
                                        db.setHostName(host);
                                        db.setPort(3306);
                                        db.setUserName(username);
                                        db.setPassword(pw_decrypted);
                                        db.setDatabaseName("bestellungen");
                                    
                                        if(db.open())
                                        { ui->label_status->setText("Verbunden über " + host + ":3306 \nals " + username); }
                                         else{ ui->label_status->setText("Verbindung fehlgeschlagen!"); }
                                    
                                    JonBJ 1 Reply Last reply
                                    0
                                    • ? A Former User

                                      @VRonin What do you mean by default database?
                                      I think it's QSqlDatabase::database() since I didn't specify anything but "this" inside the QSqlTableModel constructor.
                                      I have also tried using the connection database as parameter (without executing an SQL query). But it didn't help.
                                      This is the setup:

                                          QString username = ui->lineEdit_username->text();
                                          QString pw_decrypted = ui->lineEdit_pw->text();
                                          QString host = ui->lineEdit_ip->text();
                                      
                                          db = QSqlDatabase::addDatabase("QMYSQL");
                                          db.setHostName(host);
                                          db.setPort(3306);
                                          db.setUserName(username);
                                          db.setPassword(pw_decrypted);
                                          db.setDatabaseName("bestellungen");
                                      
                                          if(db.open())
                                          { ui->label_status->setText("Verbunden über " + host + ":3306 \nals " + username); }
                                           else{ ui->label_status->setText("Verbindung fehlgeschlagen!"); }
                                      
                                      JonBJ Offline
                                      JonBJ Offline
                                      JonB
                                      wrote on last edited by
                                      #18

                                      @SnuggleKat
                                      I'm a little confused by what you're saying/reporting, so please excuse if this is incorrect.

                                      I believe you are saying you get to see the column headers when you use QStandardItemModel but not when you use QSql{Query/Table}Model. Is that right?

                                      Because my thought would be: when you use QStandardItemModel you are in charge of telling in what columns you want it to have. Whether that does or not, when you do a SQL query (Query or Table) it is not up to you to pick what/how many columns there are. That is determined by the result set returned from the query, So what/how many columns are there in the actual underlying SQL table or query you are executing?

                                      ? 1 Reply Last reply
                                      0
                                      • JonBJ JonB

                                        @SnuggleKat
                                        I'm a little confused by what you're saying/reporting, so please excuse if this is incorrect.

                                        I believe you are saying you get to see the column headers when you use QStandardItemModel but not when you use QSql{Query/Table}Model. Is that right?

                                        Because my thought would be: when you use QStandardItemModel you are in charge of telling in what columns you want it to have. Whether that does or not, when you do a SQL query (Query or Table) it is not up to you to pick what/how many columns there are. That is determined by the result set returned from the query, So what/how many columns are there in the actual underlying SQL table or query you are executing?

                                        ? Offline
                                        ? Offline
                                        A Former User
                                        wrote on last edited by A Former User
                                        #19

                                        @JonB said in tableView->setModel() does nothing:

                                        I believe you are saying you get to see the column headers when you use QStandardItemModel but not when you use QSql{Query/Table}Model. Is that right?

                                        Exactly. Even though when using QStandardItemModel I only get the last header item in the first header cell and the remaining header cells just contain numbers.

                                        I'm executing an SQL query from another function and write the results into this tableView. It works just fine and also generates the table head alongside its entries. But I'd also like to use this tableView to create new tables from scratch and save them into my database. This is why I want to initialize it with a table head.

                                        query->setQuery("SELECT * FROM `" + table_name + "`;");
                                            ui->tableView->setModel(query); //works fine
                                        

                                        EDIT:
                                        Also tried the following. Maybe this helps to identify the problem. I tried outputting what is found inside the header cells. It works for the QSqlQueryModel-one.
                                        qDebug() << query->headerData(0, Qt::Horizontal, Qt::DisplayRole);
                                        prints: "QVariant(QString, "Bezeichnung")"

                                        But it doesn't for the QSqlTableModel-one:
                                        qDebug() << qm->headerData(0, Qt::Horizontal, Qt::DisplayRole);
                                        prints: "QVariant(int, 1)"

                                        JonBJ 1 Reply Last reply
                                        0
                                        • ? A Former User

                                          @JonB said in tableView->setModel() does nothing:

                                          I believe you are saying you get to see the column headers when you use QStandardItemModel but not when you use QSql{Query/Table}Model. Is that right?

                                          Exactly. Even though when using QStandardItemModel I only get the last header item in the first header cell and the remaining header cells just contain numbers.

                                          I'm executing an SQL query from another function and write the results into this tableView. It works just fine and also generates the table head alongside its entries. But I'd also like to use this tableView to create new tables from scratch and save them into my database. This is why I want to initialize it with a table head.

                                          query->setQuery("SELECT * FROM `" + table_name + "`;");
                                              ui->tableView->setModel(query); //works fine
                                          

                                          EDIT:
                                          Also tried the following. Maybe this helps to identify the problem. I tried outputting what is found inside the header cells. It works for the QSqlQueryModel-one.
                                          qDebug() << query->headerData(0, Qt::Horizontal, Qt::DisplayRole);
                                          prints: "QVariant(QString, "Bezeichnung")"

                                          But it doesn't for the QSqlTableModel-one:
                                          qDebug() << qm->headerData(0, Qt::Horizontal, Qt::DisplayRole);
                                          prints: "QVariant(int, 1)"

                                          JonBJ Offline
                                          JonBJ Offline
                                          JonB
                                          wrote on last edited by JonB
                                          #20

                                          @SnuggleKat

                                          But I'd also like to use this tableView to create new tables from scratch and save them into my database.

                                          Slightly unusual, but if you say so.... So if you want to use a QTableView to actually create a table which does not yet exist:

                                          • You cannot use a QSqlQuery or similar, as there is nothing in the existing database for the new table.

                                          • You will need to use a QStandardItemModel, no SQL.

                                          • Start with no columns, bind your QTableView to that model.

                                          • Invent a way for the user to specify he wants to insert a new column.

                                          • Depending on your chosen architecture, either store the desired ultimate SQL columns in rows in your QStandardItemModel, or if you want the designer to look more like what the new SQL table will look like use http://doc.qt.io/qt-5/qstandarditemmodel.html#insertColumn to insert new columns into your QStandardItemModel, which will show up as such in the QTableView.

                                          • When the user is happy and finally clicks some Create SQL Table from this button, bundle up the information you have stored in your QStandardItemModel's rows or columns so that you can generate the necessary SQL statement CREATE TABLE ... with the desired column definitons and execute that as a SQL command.

                                          • After that, once the table has been created, you can then attach a QTableView to it and you will get your view onto the new table with its columns.

                                          P.S.
                                          If you are thinking: "I'd like to use a QSqlTableModel to fetch all existing rows & columns from a current table to show to the user, and then I'd like to use http://doc.qt.io/qt-5/qabstractitemmodel.html#insertColumn to cause a new column to be created in the actual SQL table", you won't be able to do that. You need to follow some method along the lines ("CREATE TABLE ..." or "ALTER TABLE ..." SQL statement) I outlined to actually create new columns in the database table.

                                          ? 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