I have already used the QAbstractListModel in a QTreeView but it did not work.
here is how the QAbstractListModel looked like:
class.h:
#include Container.h
{
Q_OBJECT
public:
QVariant headerData
(int section, Qt
:: Orientation orientation,
int role
= Qt
::DisplayRole)const;
private:
Container container;
QQueue<Container>list;
};
#include Container.h
ContainerListModel:public QAbstractListModel
{
Q_OBJECT
public:
int rowCount(const QModelIndex &parent=QModelIndex())const;
QVariant headerData(int section, Qt:: Orientation orientation, int role= Qt::DisplayRole)const;
bool insertRows(int position, int rows, const QModelIndex &index= QModelIndex());
bool removeRows(int position, int rows, const QModelIndex &index= QModelIndex());
private:
Container container;
QQueue<Container>list;
};
To copy to clipboard, switch view to plain text mode
class.cpp
#include"class.h"
container= new Container(0,0,0);
ContainerListModel
:rowCount
(const QModelIndex &parent
)const{
return list.count();
}
{
if (!index.isValid())
if(index.row()>=list.size())
if(role== qt::DisplayRole)
}
QVariant ContainerListModel
::headerData(int section, Qt
::Orientation orientation,
int role
)const {
if (role!= Qt::DisplayRole)
if(orientation==Qt::Vertical)
return Qvariant;
}
bool ContainerListModel
:: insertRows(int position,
int rows,
const QModelIndex &parent
) {
beginInsertRows
(QModelIndex(), position, position
+rows
-1);
for (int row=0;row<rows; row++)
{
list.insert(position, new Container(0,0,0));
}
endInsertRows();
return true;
}
bool ContainerListModel
:: removeRows(int position,
int rows,
const QModelIndex &parent
) {
beginRemoveRows
(QModelIndex(), position, position
+rows
-1);
for (int row=0;row<rows; row++)
{
list.removeAt(position);
}
endRemovesRows();
return true;
}
#include"class.h"
container= new Container(0,0,0);
ContainerListModel:rowCount(const QModelIndex &parent)const
{
return list.count();
}
QVariant ContainerListModel:: data(const QModelIndex &index, int role)const
{
if (!index.isValid())
return QVariant();
if(index.row()>=list.size())
return QVariant();
if(role== qt::DisplayRole)
return QVariant();
}
QVariant ContainerListModel::headerData(int section, Qt::Orientation orientation, int role)const
{
if (role!= Qt::DisplayRole)
return QVariant();
if(orientation==Qt::Vertical)
return Qvariant;
}
bool ContainerListModel:: insertRows(int position, int rows, const QModelIndex &parent)
{
beginInsertRows(QModelIndex(), position, position+rows-1);
for (int row=0;row<rows; row++)
{
list.insert(position, new Container(0,0,0));
}
endInsertRows();
return true;
}
bool ContainerListModel:: removeRows(int position, int rows, const QModelIndex &parent)
{
beginRemoveRows(QModelIndex(), position, position+rows-1);
for (int row=0;row<rows; row++)
{
list.removeAt(position);
}
endRemovesRows();
return true;
}
To copy to clipboard, switch view to plain text mode
and than i added the constructor of the ListModel class in the QTeeView in the Class MainGui.cpp
like this:
MainGui.h
#include ContainerListModel.h
{
...
public:
...
private:
...
ContainerListModel containers;
};
#include ContainerListModel.h
MainGuipublic QWidget
{
...
public:
...
private:
...
ContainerListModel containers;
QTreeView *treeview;
};
To copy to clipboard, switch view to plain text mode
MainGui.cpp
{
scene= newScene(this);
Container *c1=newContainer(0,0,0);
c1->advance();
scene->addItem(c1);
scene->addLine(-5,-5,-5,500);
scene->addLine(105,-5,105, 500);
layout->addWidget(view);
view1->setModel(containers);
view1->show();
}
MainGui::MainGui(QWidget *parent):QWidget(parent)
{
scene= newScene(this);
Container *c1=newContainer(0,0,0);
c1->advance();
scene->addItem(c1);
scene->addLine(-5,-5,-5,500);
scene->addLine(105,-5,105, 500);
QGraphicsView*view= new QGraphicsView(scene);
QGridLayout*layout= new QGridLayout();
layout->addWidget(view);
QListView*view1= new QListView;
view1->setModel(containers);
view1->show();
}
To copy to clipboard, switch view to plain text mode
it was an other way to put the List beside the movingscene ,maybe someone can help me to proceed in both ways.
Bookmarks