PDA

View Full Version : How to make a list of QGraphicsItems on the scene



Holy
9th June 2008, 12:58
hi,
I 'm trying to make a queue of QGraphicsRectItems on the scene after have moving each of them forwards .I used the QGraphicsItemGroup but i cannot run it.
may someone help me to solve this problem please?
thanks in advance.
the code looks like this:


Container.h

class Container: public QGraphicsRectItem
{
Q_OBJECT
public:
Container(QGraphicsItem* parent=0, int x, int y);
void advance();
private:
QGraphicsItemAnimation *anim;
QTimeLine * timeline;
};
Container.cpp

#include"container.h"

Container::Container(QGraphicsItem* parent, int x=0, int y=0):QGraphicsRectItem(parent)
{QColor color(0,0,100);
Qpen p(color);
p.setWidht(2);
QBrush(brush);
setRect(x,y,100,60);
show();

}

Container::advance(){


anim= new QGraphicsItemAnimation();
timeline= new QTimeLine(5000);
anim->setItem(this);
for (qreal i=0.0;i<500.0;i++){
anim->setTranslationAt(i/500.0,0.0,i);
}
anim->setTimeLine(timeline);
timeline->setDirection(QTimeLine::Backward);
timeline->setUpdateInterval(1000/10);
timeline->setLoopCount(1);
timeline->setDuration(50000);

}

MainGui.h

class MainGui: public QWidget
{
public:
MainGui(...);
private:
QGraphicsScene *scene;
QGraphicsItemGroup* itemGroup;
};
MainGui.cpp

#include "container.h"

Container*c1= new Container (0,0,0);
Container*c2= new Container (0,0,0);

scene= new Scene(this);
itemGroup= scene->createItemGroup(scene->selectedItems());
c1->advance();
itemGroup->addToGroup(c1);
c1-> setZValue(405.0);/*I'm trying to separate the RectItems so that their do not overlap because i want them to be range in a queue. into the scene.*/

c2->advance();
itemGroup->addToGroup(c2);
c2-> setZValue(400.0);

patrik08
9th June 2008, 13:27
QGraphicsScene::items(); return all item from scene
and you can sort by your own type if having....



static const int ObjectNameEditor = 400; /* normal layer div absolute+relative */

QList<QGraphicsItem *> GraphicsScene::l_items()
{
QList<QGraphicsItem *> li;
li.clear();
QList<QGraphicsItem *> listing = QGraphicsScene::items();
for (int o=0;o<listing.size();o++) {
if (listing[o]->data(ObjectNameEditor).toInt() > 0 ) {
li.append(listing[o]);
}
}
return li;
}

Holy
9th June 2008, 14:50
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
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;
};
class.cpp


#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;
}
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
MainGuipublic QWidget
{
...
public:
...
private:
...
ContainerListModel containers;
QTreeView *treeview;
};

MainGui.cpp

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();
}
it was an other way to put the List beside the movingscene ,maybe someone can help me to proceed in both ways.

Holy
9th June 2008, 16:41
I thank u for your answer dear patrick,
i' ve tried to add the method
QList<QGraphicsItem*>l_items();
to the class Scene that i have implemented and which inherits from QGraphicsScene
like this :
Scene.h

#include<QtGui>
#include"container.h"
class Scene: public QGraphicsScene
{
public:
Scene(QWidget* parent=0);

Container* container;
private:
QList<QGraphicsItem*>l_items();
};

Scene.cpp

Scene::Scene(QWidget* parent): QGraphicsScene(parent)
{
QBrush brush(QColor(180,180,220),Qt::Dense4pattern);
setBackgroundBrush(brush);
}
//and as u said
static const int ObjectNameEditor = 400;
QList<QGraphicsItem *> GraphicsScene::l_items(){
QList<QGraphicsItem *> li;
li.clear();
QList<QGraphicsItem *> listing = QGraphicsScene::items();
for (int o=0;o<listing.size();o++) {
if (listing[o]->data(ObjectNameEditor).toInt() > 0 ) {
li.append(listing[o]);
}
}
return li;}

And in the class MainGui.cpp

Container c1= new Container(0,0,0);
Container c2= new Container(0,0,0);
c1->advance();
scene->add(c1);
c2->advance();
scene->add(c2);
//i suppose that it is the way u wanted me to use the method l_items()
thanks for replying.

Holy
9th June 2008, 20:38
these errors appear in the moc_container.cpp File when i try to compile the project.
Error:`qt_metacall´is not a member of ´QGraphicsRectItem´
Error:`qt_metacast´is not a member of ´QGraphicsRectItem´
Error:`staticMetaObject´is not a member of ´QGraphicsRectItem´
make:***[ debug]Error2
make[1]:***[ debug/moc_container.o]Error1

may someone tell me please what it means?
thanks.

jacek
10th June 2008, 01:05
these errors appear in the moc_container.cpp File when i try to compile the project.
Error:`qt_metacall´is not a member of ´QGraphicsRectItem´
Error:`qt_metacast´is not a member of ´QGraphicsRectItem´
Error:`staticMetaObject´is not a member of ´QGraphicsRectItem´
make:***[ debug]Error2
make[1]:***[ debug/moc_container.o]Error1

may someone tell me please what it means?
QGraphicsItems are not QObjects. If you don't need the signals & slots mechanism in Container class, remove the Q_OBJECT macro from its definition.

Holy
10th June 2008, 08:46
Thanks very much for the answer,it endly compiles but it doesn't do what i expect.
i've added to items c1 and c2 and i've expected both of them to appear on the scene in one queue, but it is not the case i only can see one item, i suppose that c2 overlap c1 how can i get them listed instead?

aamer4yu
10th June 2008, 11:01
Sorry if I am silly in asking this -
What exactly u mean by queue ?? You want the items to be arranged in queue ?
also do u want to move them ?? i was not able to understand what functionality u wanted?

Holy
10th June 2008, 13:43
yes i want them to be added one after another in the queue and should be also removable one after another (The first in -first out algorithm) . The loading and the unloading procedure muss be visible (I try to achieve it with the advance() methode in the Container class)