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:
#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;
}
#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;
}
To copy to clipboard, switch view to plain text mode
mymodel.cpp:
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;
}
}
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::VerticalHeaderDataRole)
{
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();
}
To copy to clipboard, switch view to plain text mode
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:
#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;
To copy to clipboard, switch view to plain text mode
dialog.cpp:
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();
}
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();
}
To copy to clipboard, switch view to plain text mode
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;
};
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;
};
To copy to clipboard, switch view to plain text mode
Bookmarks