QTableView and custom QHeaderView: header not showing
Edited:
guys,
I want to use this http://qt-apps.org/content/show.php/...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:
Code:
#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;
{
Q_OBJECT
public:
TABLEModel
( QObject* Qparent
= 0 );
TABLEModel
( QObject* Qparent, TABLEData
* tableData
);
~TABLEModel();
void fillHeaderModel();
QVariant headerData
(int section, Qt
::Orientation orientation,
int role = Qt::DisplayRole ) const;
}
mymodel.cpp:
Code:
TABLEModel
::TABLEModel( QObject *parentQ, TABLEData
* tableData
){
setColumnCount( m_tableData->getTable()->getColumnCount());
fillHeaderModel();
}
{
if( role == HierarchicalHeaderView::HorizontalHeaderDataRole )
{
v.
setValue(( QObject* )& m_horizontalHeaderModel
);
return v;
}
if(role==HierarchicalHeaderView::VerticalHeaderDataRole)
{
v.
setValue((QObject*)& m_verticalHeaderModel
);
return v;
}
if( role == Qt::DisplayRole && indexQ.isValid())
{
return QString( m_tableData
->getParameterData
( indexQ.
column(), indexQ.
row()).
c_str());
}
}
void TABLEModel::fillHeaderModel()
{
QList<QStandardItem*> l;
l.push_back(cell);
rootItem->appendColumn(l);
l.clear();
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 )
if( orientation == Qt::Horizontal )
{
QString data
( m_tableData
->getColumns
()[ section
]->getLabel
().
c_str());
return data;
}
}
dialog.h:
Code:
#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:
dialog.cpp:
Code:
DIALOGTable
::DIALOGTable( TABLETable
&table,
QWidget *parentW
) :DIALOGParameterDialog( table, parentW, ), m_table( table )
{
}
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();
}
Code:
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;
};
Re: QTableView and custom QHeaderView: header not showing
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.