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. How to add data to QTableView?
Forum Updated to NodeBB v4.3 + New Features

How to add data to QTableView?

Scheduled Pinned Locked Moved Unsolved General and Desktop
4 Posts 3 Posters 1.4k Views 1 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.
  • T Offline
    T Offline
    TheCeylann
    wrote on last edited by
    #1

    I created a table and I want to add data to this table but I couldn't figure out how to do it. Can someone help me? I will be grateful.

    Here is my code

    
    
    void MainWindow::on_pushButton_clicked()
    {
        QTableView *view = new QTableView;
        QStandardItemModel *mod = new QStandardItemModel;
        QStandardItem *it = new QStandardItem(QObject::tr("ID"));
        mod->setHorizontalHeaderItem(0,it);
        QStandardItem *it1 = new QStandardItem(QObject::tr("Name"));
        mod->setHorizontalHeaderItem(1,it1);
        QStandardItem *it2 = new QStandardItem(QObject::tr("City"));
         mod->setHorizontalHeaderItem(2,it2);
        QStandardItem *it3 = new QStandardItem(QObject::tr("Country"));
        mod->setHorizontalHeaderItem(3,it3);
    
    
        view->setModel(mod);
        view->show();
    }
    
    1 Reply Last reply
    0
    • Christian EhrlicherC Offline
      Christian EhrlicherC Offline
      Christian Ehrlicher
      Lifetime Qt Champion
      wrote on last edited by
      #2

      You don't add data to a view but to a model and since you're using a QStandardItemModel you can look into it's documentation here: https://doc.qt.io/qt-6/qstandarditemmodel.html#details
      You should also read this: https://doc.qt.io/qt-6/model-view-programming.html

      Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
      Visit the Qt Academy at https://academy.qt.io/catalog

      T 1 Reply Last reply
      2
      • Christian EhrlicherC Christian Ehrlicher

        You don't add data to a view but to a model and since you're using a QStandardItemModel you can look into it's documentation here: https://doc.qt.io/qt-6/qstandarditemmodel.html#details
        You should also read this: https://doc.qt.io/qt-6/model-view-programming.html

        T Offline
        T Offline
        TheCeylann
        wrote on last edited by
        #3

        @Christian-Ehrlicher I store my data in QStringList. I want to show them in a table. What would you recommend for this?

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

          Hi,

          Loop on your list and create the items as needed.

          Note that Qt has also QStringListModel but it's usually used with a QListView.

          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
          1

          • Login

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