Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

table multi level header not show labels



  • I added a multi level header like this:
    0_1563811252423_Screen Shot.png
    The multi header is ok when program start up, but when i add another one, it should be "channel 17", it's not show all the labels. The code like this:

    int icols = table->columnCount();
        for (int i = 0; i < 5; ++i)
        {
            table->insertColumn(icols + i);
        }
        hHeader->model()->insertColumns(icols, 5);
        hHeader->setSpan(0, icols, 1, 5);
    
        hHeader->setCellLabel(0, icols, "channel " + QString::number((icols - 2) / 5 + 1));
        hHeader->setCellLabel(1, icols, "A");
        hHeader->setCellLabel(1, icols + 1, "f");
        hHeader->setCellLabel(1, icols + 2, "Ø");
        hHeader->setCellLabel(1, icols + 3, "t1");
        hHeader->setCellLabel(1, icols + 4, "t2");
    
    void GridTableHeaderView::setCellLabel(int row, int column,
                                           const QString &label) {
    //    qDebug() << "set row=" << row << "set col=" << column;
      QModelIndex index =model()->index(row, column);
      model()->setData(index, label, Qt::DisplayRole);
    }
    
    bool GridTableHeaderModel::setData(const QModelIndex &index,
                                       const QVariant &value, int role) {
      if (index.isValid()) {
    //      qDebug() << "index row=" << index.row() << "col=" << index.column();
        TableHeaderItem *item =
            static_cast<TableHeaderItem *>(index.internalPointer());
        if (role == ColumnSpanRole) {
          int col = index.column();
          int span = value.toInt();
          if (span > 0) {
            if (col + span - 1 >= _column)
              span = _column - col;
            item->setData(span, ColumnSpanRole);
          }
        } else if (role == RowSpanRole) {
          int row = index.row();
          int span = value.toInt();
          if (span > 0) {
            if (row + span - 1 > _row)
              span = _column - row;
            item->setData(span, RowSpanRole);
          }
        } else
          item->setData(value, role);
        return true;
      }
      return false;
    }
    
    void TableHeaderItem::setData(const QVariant &data, int role) {
        qDebug() << "role=" << role << "data=" << data;
      m_itemData.insert(role, data);
    }
    
    class TableHeaderItem {
    public:
      TableHeaderItem(TableHeaderItem *parent = 0);
      TableHeaderItem(int row, int column, TableHeaderItem *parent = 0);
    
      TableHeaderItem *insertChild(int row, int col);
      TableHeaderItem *child(int row, int col);
    
      TableHeaderItem *parent();
    
      int row() const;
      int column() const;
    
      void setData(const QVariant &data, int role);
      QVariant data(int role) const;
    
      void clear();
    
      bool removeColumns(int position, int columns);
      bool insertColumns(int position, int columns);
    
    private:
      TableHeaderItem *m_parentItem;
      int _row;
      int _column;
    
      QHash<QPair<int, int>, TableHeaderItem *> m_childItems;
      QHash<int, QVariant> m_itemData;
    };

Log in to reply