Results 1 to 9 of 9

Thread: How to make a list of QGraphicsItems on the scene

  1. #1
    Join Date
    May 2008
    Posts
    19
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default How to make a list of QGraphicsItems on the scene

    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
    Qt Code:
    1. class Container: public QGraphicsRectItem
    2. {
    3. Q_OBJECT
    4. public:
    5. Container(QGraphicsItem* parent=0, int x, int y);
    6. void advance();
    7. private:
    8. QTimeLine * timeline;
    9. };
    To copy to clipboard, switch view to plain text mode 
    Container.cpp
    Qt Code:
    1. #include"container.h"
    2.  
    3. Container::Container(QGraphicsItem* parent, int x=0, int y=0):QGraphicsRectItem(parent)
    4. {QColor color(0,0,100);
    5. Qpen p(color);
    6. p.setWidht(2);
    7. QBrush(brush);
    8. setRect(x,y,100,60);
    9. show();
    10.  
    11. }
    12.  
    13. Container::advance(){
    14.  
    15.  
    16. timeline= new QTimeLine(5000);
    17. anim->setItem(this);
    18. for (qreal i=0.0;i<500.0;i++){
    19. anim->setTranslationAt(i/500.0,0.0,i);
    20. }
    21. anim->setTimeLine(timeline);
    22. timeline->setDirection(QTimeLine::Backward);
    23. timeline->setUpdateInterval(1000/10);
    24. timeline->setLoopCount(1);
    25. timeline->setDuration(50000);
    26.  
    27. }
    To copy to clipboard, switch view to plain text mode 
    MainGui.h
    Qt Code:
    1. class MainGui: public QWidget
    2. {
    3. public:
    4. MainGui(...);
    5. private:
    6. QGraphicsItemGroup* itemGroup;
    7. };
    To copy to clipboard, switch view to plain text mode 
    MainGui.cpp
    Qt Code:
    1. #include "container.h"
    2.  
    3. Container*c1= new Container (0,0,0);
    4. Container*c2= new Container (0,0,0);
    5.  
    6. scene= new Scene(this);
    7. itemGroup= scene->createItemGroup(scene->selectedItems());
    8. c1->advance();
    9. itemGroup->addToGroup(c1);
    10. 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.*/
    11.  
    12. c2->advance();
    13. itemGroup->addToGroup(c2);
    14. c2-> setZValue(400.0);
    To copy to clipboard, switch view to plain text mode 
    Last edited by jpn; 9th June 2008 at 17:05. Reason: missing [code] tags

  2. #2
    Join Date
    May 2006
    Posts
    788
    Thanks
    49
    Thanked 48 Times in 46 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: How to make a list of QGraphicsItems on the scene

    QGraphicsScene::items(); return all item from scene
    and you can sort by your own type if having....
    Qt Code:
    1. static const int ObjectNameEditor = 400; /* normal layer div absolute+relative */
    2.  
    3. QList<QGraphicsItem *> GraphicsScene::l_items()
    4. {
    5. QList<QGraphicsItem *> li;
    6. li.clear();
    7. QList<QGraphicsItem *> listing = QGraphicsScene::items();
    8. for (int o=0;o<listing.size();o++) {
    9. if (listing[o]->data(ObjectNameEditor).toInt() > 0 ) {
    10. li.append(listing[o]);
    11. }
    12. }
    13. return li;
    14. }
    To copy to clipboard, switch view to plain text mode 

  3. #3
    Join Date
    May 2008
    Posts
    19
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: How to make a list of QGraphicsItems on the scene

    I have already used the QAbstractListModel in a QTreeView but it did not work.
    here is how the QAbstractListModel looked like:
    class.h:
    Qt Code:
    1. #include Container.h
    2. ContainerListModel:public QAbstractListModel
    3. {
    4. Q_OBJECT
    5. public:
    6. int rowCount(const QModelIndex &parent=QModelIndex())const;
    7. QVariant headerData(int section, Qt:: Orientation orientation, int role= Qt::DisplayRole)const;
    8. bool insertRows(int position, int rows, const QModelIndex &index= QModelIndex());
    9. bool removeRows(int position, int rows, const QModelIndex &index= QModelIndex());
    10. private:
    11. Container container;
    12. QQueue<Container>list;
    13. };
    To copy to clipboard, switch view to plain text mode 
    class.cpp

    Qt Code:
    1. #include"class.h"
    2. container= new Container(0,0,0);
    3.  
    4. ContainerListModel:rowCount(const QModelIndex &parent)const
    5. {
    6. return list.count();
    7. }
    8.  
    9. QVariant ContainerListModel:: data(const QModelIndex &index, int role)const
    10. {
    11. if (!index.isValid())
    12. return QVariant();
    13. if(index.row()>=list.size())
    14. return QVariant();
    15. if(role== qt::DisplayRole)
    16. return QVariant();
    17. }
    18.  
    19. QVariant ContainerListModel::headerData(int section, Qt::Orientation orientation, int role)const
    20. {
    21.  
    22. if (role!= Qt::DisplayRole)
    23. return QVariant();
    24. if(orientation==Qt::Vertical)
    25. return Qvariant;
    26. }
    27.  
    28. bool ContainerListModel:: insertRows(int position, int rows, const QModelIndex &parent)
    29. {
    30. beginInsertRows(QModelIndex(), position, position+rows-1);
    31. for (int row=0;row<rows; row++)
    32. {
    33. list.insert(position, new Container(0,0,0));
    34. }
    35. endInsertRows();
    36. return true;
    37. }
    38. bool ContainerListModel:: removeRows(int position, int rows, const QModelIndex &parent)
    39. {
    40. beginRemoveRows(QModelIndex(), position, position+rows-1);
    41. for (int row=0;row<rows; row++)
    42. {
    43. list.removeAt(position);
    44. }
    45. endRemovesRows();
    46. return true;
    47. }
    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
    Qt Code:
    1. #include ContainerListModel.h
    2. MainGuipublic QWidget
    3. {
    4. ...
    5. public:
    6. ...
    7. private:
    8. ...
    9. ContainerListModel containers;
    10. QTreeView *treeview;
    11. };
    To copy to clipboard, switch view to plain text mode 
    MainGui.cpp
    Qt Code:
    1. MainGui::MainGui(QWidget *parent):QWidget(parent)
    2. {
    3. scene= newScene(this);
    4. Container *c1=newContainer(0,0,0);
    5. c1->advance();
    6. scene->addItem(c1);
    7. scene->addLine(-5,-5,-5,500);
    8. scene->addLine(105,-5,105, 500);
    9. QGraphicsView*view= new QGraphicsView(scene);
    10. QGridLayout*layout= new QGridLayout();
    11. layout->addWidget(view);
    12.  
    13. QListView*view1= new QListView;
    14. view1->setModel(containers);
    15. view1->show();
    16. }
    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.
    Last edited by jpn; 9th June 2008 at 17:06. Reason: missing [code] tags

  4. #4
    Join Date
    May 2008
    Posts
    19
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: How to make a list of QGraphicsItems on the scene

    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
    Qt Code:
    1. #include<QtGui>
    2. #include"container.h"
    3. class Scene: public QGraphicsScene
    4. {
    5. public:
    6. Scene(QWidget* parent=0);
    7.  
    8. Container* container;
    9. private:
    10. QList<QGraphicsItem*>l_items();
    11. };
    To copy to clipboard, switch view to plain text mode 
    Scene.cpp
    Qt Code:
    1. Scene::Scene(QWidget* parent): QGraphicsScene(parent)
    2. {
    3. QBrush brush(QColor(180,180,220),Qt::Dense4pattern);
    4. setBackgroundBrush(brush);
    5. }
    6. //and as u said
    7. static const int ObjectNameEditor = 400;
    8. QList<QGraphicsItem *> GraphicsScene::l_items(){
    9. QList<QGraphicsItem *> li;
    10. li.clear();
    11. QList<QGraphicsItem *> listing = QGraphicsScene::items();
    12. for (int o=0;o<listing.size();o++) {
    13. if (listing[o]->data(ObjectNameEditor).toInt() > 0 ) {
    14. li.append(listing[o]);
    15. }
    16. }
    17. return li;}
    To copy to clipboard, switch view to plain text mode 
    And in the class MainGui.cpp
    Qt Code:
    1. Container c1= new Container(0,0,0);
    2. Container c2= new Container(0,0,0);
    3. c1->advance();
    4. scene->add(c1);
    5. c2->advance();
    6. scene->add(c2);
    7. //i suppose that it is the way u wanted me to use the method l_items()
    To copy to clipboard, switch view to plain text mode 
    thanks for replying.
    Last edited by jacek; 10th June 2008 at 01:06. Reason: wrapped too long lines

  5. #5
    Join Date
    May 2008
    Posts
    19
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: How to make a list of QGraphicsItems on the scene

    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.

  6. #6
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    5,372
    Thanks
    28
    Thanked 976 Times in 912 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: How to make a list of QGraphicsItems on the scene

    Quote Originally Posted by Holy View Post
    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.

  7. #7
    Join Date
    May 2008
    Posts
    19
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: How to make a list of QGraphicsItems on the scene

    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?

  8. #8
    Join Date
    Oct 2006
    Location
    New Delhi, India
    Posts
    2,467
    Thanks
    8
    Thanked 334 Times in 317 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: How to make a list of QGraphicsItems on the scene

    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?

  9. #9
    Join Date
    May 2008
    Posts
    19
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: How to make a list of QGraphicsItems on the scene

    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)

Similar Threads

  1. make QTableView work as a multi-column list view
    By wesley in forum Qt Programming
    Replies: 1
    Last Post: 11th March 2008, 14:43
  2. Replies: 1
    Last Post: 14th May 2007, 14:55
  3. Compiling with Qmake/Make
    By VireX in forum Newbie
    Replies: 25
    Last Post: 22nd February 2007, 05:57

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.