Here is my code, I have tried to find a solution for this but could not find any.
can some one suggest a way to get over this?
Mode:
{
}
Model::~Model()
{
}
void Model::setHeaderData(QStringList& head)
{
m_head = head;
}
{
int row = index.row();
if(row%2){
head << " Head1 " << " Head2 " << " Head3 " << " Head4 " << " Head5 " << " Head6 " << " Head7 " << " Head8 " << "head9" ;
}
else{
head << " Head11 " << " Head12 " << " Head13 " << " Head14 " << " Head15 " << " Head16 " << " Head17 " << " Head18 " ;
}
m_head = head;
emit headerDataChanged(Qt::Horizontal, 0, head.size() - 1);
}
QVariant Model
::headerData ( int section, Qt
::Orientation orientation,
int role
) const {
if ( orientation == Qt::Horizontal && role == Qt::DisplayRole )
{
return m_head.at(section);
}
else if ( orientation == Qt::Vertical && role == Qt::DisplayRole ){
return section;
}
}
int Model
::columnCount(const QModelIndex &parent
) const {
return m_head.size();
}
{
if(!parent.isValid()){
return 10000000;
}
if(parent.isValid() && !parent.parent().isValid()){
qDebug() << parent; // This is calling for 20000000 times for each mouse click & causing problem
return 2; // limit to two levels
}
return 0;
}
{
if(!parent.isValid())
{
return createIndex(row, column, -1); // top level item has invalid parent
}
// we only need id of the parent since it identifies the item
return createIndex(row, column, parent.row());
}
{
int id = ind.internalId();
return index(id, 0); // return top-level item
}
{
if(role == Qt::DisplayRole) return index.row()+1;
}
Model::Model(QObject *parent): QAbstractItemModel(parent)
{
}
Model::~Model()
{
}
void Model::setHeaderData(QStringList& head)
{
m_head = head;
}
void Model::headerChanged(QModelIndex index)
{
int row = index.row();
QStringList head;
if(row%2){
head << " Head1 " << " Head2 " << " Head3 " << " Head4 " << " Head5 " << " Head6 " << " Head7 " << " Head8 " << "head9" ;
}
else{
head << " Head11 " << " Head12 " << " Head13 " << " Head14 " << " Head15 " << " Head16 " << " Head17 " << " Head18 " ;
}
m_head = head;
emit headerDataChanged(Qt::Horizontal, 0, head.size() - 1);
}
QVariant Model::headerData ( int section, Qt::Orientation orientation, int role ) const
{
if ( orientation == Qt::Horizontal && role == Qt::DisplayRole )
{
return m_head.at(section);
}
else if ( orientation == Qt::Vertical && role == Qt::DisplayRole ){
return section;
}
return QVariant();
}
int Model::columnCount(const QModelIndex &parent) const
{
return m_head.size();
}
int Model::rowCount(const QModelIndex &parent) const
{
if(!parent.isValid()){
return 10000000;
}
if(parent.isValid() && !parent.parent().isValid()){
qDebug() << parent; // This is calling for 20000000 times for each mouse click & causing problem
return 2; // limit to two levels
}
return 0;
}
QModelIndex Model::index(int row, int column, const QModelIndex &parent) const
{
if(!parent.isValid())
{
return createIndex(row, column, -1); // top level item has invalid parent
}
// we only need id of the parent since it identifies the item
return createIndex(row, column, parent.row());
}
QModelIndex Model::parent(const QModelIndex &ind) const
{
if(!ind.isValid()) return QModelIndex();
int id = ind.internalId();
if( id == -1) return QModelIndex();
return index(id, 0); // return top-level item
}
QVariant Model::data(const QModelIndex &index, int role ) const
{
if(role == Qt::DisplayRole) return index.row()+1;
return QVariant();
}
To copy to clipboard, switch view to plain text mode
View:
treeview::treeview(Model* model)
{
setModel(model);
setUniformRowHeights(true);
}
treeview::~treeview()
{
}
treeview::treeview(Model* model)
{
setModel(model);
connect(this, SIGNAL(clicked(QModelIndex)), model, SLOT( headerChanged(QModelIndex) ));
setUniformRowHeights(true);
}
treeview::~treeview()
{
}
To copy to clipboard, switch view to plain text mode
main:
int main(int argc, char **argv)
{
Model model;
treeview v(&model);
v.setFixedSize(700, 800);
head << " Head1 " << " Head2 " << " Head3 " << " Head4 " << " Head5 " << " Head6 " << " Head7 " << " Head8 " ;
model.setHeaderData(head);
v.setModel(&model);
v.show();
return app.exec();
}
int main(int argc, char **argv)
{
QApplication app(argc, argv);
Model model;
treeview v(&model);
v.setFixedSize(700, 800);
QStringList head;
head << " Head1 " << " Head2 " << " Head3 " << " Head4 " << " Head5 " << " Head6 " << " Head7 " << " Head8 " ;
model.setHeaderData(head);
v.setModel(&model);
v.show();
return app.exec();
}
To copy to clipboard, switch view to plain text mode
Thanks in advance.
Bookmarks