PDA

View Full Version : QTableView and custom QHeaderView: header not showing



xyfix
8th September 2010, 17:14
Edited:

guys,

I want to use this http://qt-apps.org/content/show.php/HierarchicalHeaderView?content=103154 in my app. So I included the files and placed some code in my model class ( the code that's actually in example1.cpp ). But I no header is shown. And when I don't use QHeaderView but only the reimplemented headerData function from QAbstractTableModel I get my header. This is my code:

mymodel.h:


#ifndef TABLEMODEL_H
#define TABLEMODEL_H

#include "tabledata.h"

#include <QAbstractTableModel>
#include <QModelIndex>
#include <QStandardItemModel>
#include <QVariant>

typedef std::list< std::pair < HeaderData, std::vector< std::string > > > dataMap;

class TABLEModel : public QAbstractTableModel
{
Q_OBJECT

public:
TABLEModel( QObject* Qparent = 0 );
TABLEModel( QObject* Qparent, TABLEData* tableData );

~TABLEModel();
void fillHeaderModel();
QVariant data( const QModelIndex &indexQ, int role ) const;
QVariant headerData(int section, Qt::Orientation orientation,
int role = Qt::DisplayRole ) const;

}

mymodel.cpp:


TABLEModel::TABLEModel( QObject *parentQ, TABLEData* tableData )
: QAbstractTableModel( parentQ ), m_tableData( tableData )
{
setColumnCount( m_tableData->getTable()->getColumnCount());
fillHeaderModel();
}

QVariant TABLEModel::data( const QModelIndex &indexQ, int role ) const
{
if( role == HierarchicalHeaderView::HorizontalHeaderDataRole )
{
QVariant v;
v.setValue(( QObject* )& m_horizontalHeaderModel );
return v;
}

if(role==HierarchicalHeaderView::VerticalHeaderDat aRole)
{
QVariant v;
v.setValue((QObject*)& m_verticalHeaderModel);
return v;
}

if( role == Qt::DisplayRole && indexQ.isValid())
{
return QString( m_tableData->getParameterData( indexQ.column(), indexQ.row()).c_str());
}

return QVariant();
}

void TABLEModel::fillHeaderModel()
{
QStandardItem* rootItem = new QStandardItem("root");
QList<QStandardItem*> l;

QStandardItem* cell=new QStandardItem("level 2");
l.push_back(cell);
rootItem->appendColumn(l);

l.clear();

l.push_back(new QStandardItem("level 2"));
rootItem->appendColumn(l);

l.clear();

m_horizontalHeaderModel.setItem( 0, 0, rootItem );
}

QVariant TABLEModel::headerData(int section, Qt::Orientation orientation, int role ) const
{
if( role != Qt::DisplayRole )
return QVariant();

if( orientation == Qt::Horizontal )
{
QString data( m_tableData->getColumns()[ section ]->getLabel().c_str());

return data;
}

return QVariant();

}


dialog.h:


#ifndef DIALOGTABLE_H
#define DIALOGTABLE_H

#include "tabledata.h"
#include "tabletable.h"

#include "dialogparameterdialog.h"

#include <QDialog>
#include <QTableView>

class TABLEModel;

class DIALOGTable : public DIALOGParameterDialog
{
Q_OBJECT

public:
DIALOGTable( TABLETable &table, QWidget *parent = 0 );

private slots:
void slotCreateTable();

private:
QTableView* m_view;


dialog.cpp:


DIALOGTable::DIALOGTable( TABLETable &table, QWidget *parentW )
:DIALOGParameterDialog( table, parentW, ), m_table( table )
{
m_view = new QTableView( this );
}

void DIALOGTable::slotCreateTable()
{
HierarchicalHeaderView* hv = new HierarchicalHeaderView( Qt::Horizontal, m_view );
hv->setHighlightSections( true );
hv->setClickable( true );
hv->setMovable(true);
m_view->setHorizontalHeader( hv );

TABLEModel* model = new TABLEModel( this, m_tableData );
model->setRowCount( 0 ); //this is because I only want to show the header at first

m_view->setModel( model );
m_view->resizeColumnsToContents();
m_view->resizeRowsToContents();
m_view->show();
}




class HeaderData
{
public:
HeaderData(){};
~HeaderData(){};

public:
const std::string&
getLabel() const
{
return m_label;
}

void
setLabel( const std::string& label )
{
m_label = label;
}

const std::string&
getUnit() const
{
return m_unit;
}

void
setUnit( const std::string& unit )
{
m_unit = unit;
}

const std::vector< std::string >&
getSubLabel() const
{
return m_subLabel;
}

void
setSubLabel( const std::vector< std::string >& subLabel )
{
m_subLabel = subLabel;
}

private:
std::string m_label;
std::string m_unit;
std::vector< std::string > m_subLabel;
};

xyfix
8th September 2010, 21:38
when I comment out the setting of the custom headerview in the dialog class I can see the header. And when I just add a simple QHeaderView instead of the HierarchicalHeaderView I don't get a header.