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 set tableView headers with QStandardItemModel ?
Forum Updated to NodeBB v4.3 + New Features

How to set tableView headers with QStandardItemModel ?

Scheduled Pinned Locked Moved Solved General and Desktop
3 Posts 2 Posters 1.2k 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.
  • I Offline
    I Offline
    imene
    wrote on last edited by imene
    #1

    Hello;
    I want to set tableView header with QStandardItemModel ?

    mModel->setColumnCount(3);
        mModel->setHeaderData(0, Qt::Horizontal, "Header 1", Qt::DisplayRole);
        mModel->setHeaderData(1, Qt::Horizontal, "Header 2", Qt::DisplayRole);
        mModel->setHeaderData(2, Qt::Horizontal, "Header 3", Qt::DisplayRole);
        QHeaderView * p = new QHeaderView(Qt::Orientation orientation,(m_ui->Table_CSV));
        p->setModel(mModel);
        m_ui->Table_CSV->setHorizontalHeader(p);
        m_ui->Table_CSV->show();
        m_ui->Table_CSV->horizontalHeader()->setModel(mModel);
    

    The problem is here:

     QHeaderView * p = new QHeaderView(Qt::Orientation orientation,(m_ui->Table_CSV));
    
    1 Reply Last reply
    1
    • C Offline
      C Offline
      ChrisW67
      wrote on last edited by
      #2

      If the model returns data for the headers then the default QTableView will use those and there is no need to create your own QHeaderView, just set the model on the view. In most cases this is adequate.

          QStandardItemModel *model = new QStandardItemModel(3, 3, this);
          model->setHeaderData(0, Qt::Horizontal, "C 1", Qt::DisplayRole);
          model->setHeaderData(1, Qt::Horizontal, "C 2", Qt::DisplayRole);
          model->setHeaderData(2, Qt::Horizontal, "C 3", Qt::DisplayRole);
          model->setHeaderData(0, Qt::Vertical, "Row 1", Qt::DisplayRole);
          model->setHeaderData(1, Qt::Vertical, "Row 2", Qt::DisplayRole);
          model->setHeaderData(2, Qt::Vertical, "Row 3", Qt::DisplayRole);
          for (int r = 0; r < 3; ++r ) {
              for (int c = 0; c < 3; ++c) {
                  model->setData(model->index(r, c), QString("R%0C%1").arg(r).arg(c));
              }
          }
          ui->tableView->setModel(model);
      

      a3b625bb-b9bd-4ded-89d7-0e78dc57f210-image.png

      You may subclass QHeaderView if you need some custom rendering. In that case you just set the customised header view on the table view and it inherits the correct model.

          CustomHeaderView *hv = new CustomHeaderView(Qt::Horizontal, this);
          ui->tableView->setHorizontalHeader(hv);
      
      1 Reply Last reply
      3
      • I Offline
        I Offline
        imene
        wrote on last edited by imene
        #3

        Solved!
        https://forum.qt.io/topic/138611/how-to-get-the-current-file-address-just-opened-in-a-tableview

        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