Results 1 to 3 of 3

Thread: GroupItems in two states

  1. #1
    Join Date
    Feb 2016
    Posts
    17
    Thanks
    3
    Qt products
    Qt5
    Platforms
    Windows

    Default GroupItems in two states

    Hello,

    the following code draws 3 ellipses and one rectangle.
    These 4 object are grouped in a QGraphicsItemGroup together.

    What i want to do is to implement 2 states:

    First state:
    If the rectangles moves, the ellipses move too, like the code shows.


    Second state:
    This is where i do not find a solution.
    I need a state where I make the ellipses moveable inside the rectangle , for example with a double click on the rectangle.
    And if i click back outsite the rectangle, the ellipses fix back to the rectangle. (first state).
    How i could do this?

    Thx Andre

    mainwindow.h
    Qt Code:
    1. #include <QMainWindow>
    2.  
    3. #include "customitemellipse.h"
    4. #include "customitemrect.h"
    5.  
    6. namespace Ui {
    7. class MainWindow;
    8. }
    9.  
    10. class MainWindow : public QMainWindow
    11. {
    12. Q_OBJECT
    13.  
    14. public:
    15. explicit MainWindow(QWidget *parent = 0);
    16. ~MainWindow();
    17.  
    18. private:
    19. Ui::MainWindow *ui;
    20. };
    To copy to clipboard, switch view to plain text mode 

    mainwindow.cpp
    Qt Code:
    1. #include "mainwindow.h"
    2. #include "ui_mainwindow.h"
    3.  
    4. MainWindow::MainWindow(QWidget *parent) :
    5. QMainWindow(parent),
    6. ui(new Ui::MainWindow)
    7. {
    8. ui->setupUi(this);
    9. scene = new QGraphicsScene(this);
    10. ui->graphicsView->setScene(scene);
    11.  
    12. group = scene->createItemGroup(scene->selectedItems());
    13. group->setFlags(QGraphicsItem::ItemIsSelectable | QGraphicsItem::ItemIsMovable);
    14.  
    15.  
    16. CustomItemEllipse *ellipse1 = new CustomItemEllipse();
    17. scene->addItem(ellipse1);
    18. group->addToGroup(ellipse1);
    19.  
    20. CustomItemEllipse *ellipse2 = new CustomItemEllipse();
    21. ellipse2->setPos(mapToParent(QPoint(300,0)));
    22. scene->addItem(ellipse2);
    23. group->addToGroup(ellipse2);
    24.  
    25. CustomItemEllipse *ellipse3 = new CustomItemEllipse();
    26. ellipse3->setPos(mapToParent(QPoint(0,300)));
    27. scene->addItem(ellipse3);
    28. group->addToGroup(ellipse3);
    29.  
    30. CustomItemRect *rect = new CustomItemRect();
    31. scene->addItem(rect);
    32. group->addToGroup(rect);
    33. }
    To copy to clipboard, switch view to plain text mode 


    customitemellipse.h
    Qt Code:
    1. #include <QGraphicsItem>
    2. #include <QPainter>
    3.  
    4. class CustomItemEllipse : public QGraphicsItem
    5. {
    6. public:
    7. QRectF ellipse;
    8.  
    9. public:
    10. CustomItemEllipse();
    11. QRectF boundingRect() const;
    12. void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget);
    13.  
    14. };
    To copy to clipboard, switch view to plain text mode 



    customitemellipse.cpp
    Qt Code:
    1. #include "customitemellipse.h"
    2.  
    3. CustomItemEllipse::CustomItemEllipse()
    4. {
    5. ellipse = boundingRect();
    6. setFlag(ItemIsMovable);
    7. setZValue(10);
    8. }
    9.  
    10.  
    11. QRectF CustomItemEllipse::boundingRect() const
    12. {
    13. return QRectF(0,0,75,75);
    14. }
    15.  
    16.  
    17. void CustomItemEllipse::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget)
    18. {
    19. painter->setBrush(Qt::gray);
    20. QPen pen(Qt::black, 3);
    21. painter->setPen(pen);
    22. painter->drawEllipse(ellipse);
    23. }
    To copy to clipboard, switch view to plain text mode 

    customitemrect.h
    Qt Code:
    1. #include <QGraphicsItem>
    2. #include <QPainter>
    3.  
    4. class CustomItemRect : public QGraphicsItem
    5. {
    6. public:
    7. QRectF rect;
    8.  
    9. public:
    10. CustomItemRect();
    11. QRectF boundingRect() const;
    12. void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget);
    13. };
    To copy to clipboard, switch view to plain text mode 


    customitemrect.cpp
    Qt Code:
    1. #include "customitemrect.h"
    2.  
    3. CustomItemRect::CustomItemRect()
    4. {
    5. rect = boundingRect();
    6. setFlag(ItemIsMovable);
    7. setScale(5);
    8. }
    9.  
    10. QRectF CustomItemRect::boundingRect() const
    11. {
    12. return QRectF(0,0,75,75);
    13. }
    14.  
    15. void CustomItemRect::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget)
    16. {
    17. painter->setBrush(Qt::white);
    18. QPen pen(Qt::black, 0.5);
    19. painter->setPen(pen);
    20. painter->drawRoundRect(rect);
    21. }
    To copy to clipboard, switch view to plain text mode 
    Last edited by Andre008; 9th March 2016 at 23:10.

  2. #2
    Join Date
    Feb 2016
    Posts
    17
    Thanks
    3
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: GroupItems in two states

    Has anyone an idee, if there there is a helpful functionality form the QT-Framework, or it should this implements by hand.

    Thx Andre

  3. #3
    Join Date
    Feb 2016
    Posts
    17
    Thanks
    3
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: GroupItems in two states

    Hello,

    i have a semi solution, but a i can not release the grouped items :

    mainwindow.cpp
    Qt Code:
    1. MainWindow::MainWindow(QWidget *parent) :
    2. QMainWindow(parent),
    3. ui(new Ui::MainWindow)
    4. {
    5. .....
    6.  
    7. CustomItemRect *rect = new CustomItemRect();
    8. scene->addItem(rect);
    9. group->addToGroup(rect);
    10.  
    11. //add this line
    12. rect->setGroup(group);
    13.  
    14. ....
    15. }
    To copy to clipboard, switch view to plain text mode 

    customitemrect.h
    Qt Code:
    1. class CustomItemRect : public QGraphicsItem
    2. {
    3. public:
    4. // add this line
    5. void setGroup(QGraphicsItemGroup *group);
    6. };
    To copy to clipboard, switch view to plain text mode 

    customitemrect.cpp
    Qt Code:
    1. CustomItemRect::CustomItemRect()
    2. {
    3. rect = boundingRect();
    4. setFlag(ItemIsMovable);
    5. setScale(5);
    6.  
    7. // add this line
    8. isGroup = false;
    9. }
    10.  
    11. // add this functions
    12.  
    13. void CustomItemRect::mousePressEvent(QGraphicsSceneMouseEvent *event)
    14. {
    15. qDebug()<<"Rect";
    16. }
    17.  
    18. void CustomItemRect::mouseDoubleClickEvent(QGraphicsSceneMouseEvent* e )
    19. {
    20. isGroup = !isGroup;
    21. customItemGroup->setHandlesChildEvents(isGroup);
    22. }
    23.  
    24. void CustomItemRect::setGroup(QGraphicsItemGroup *group)
    25. {
    26. customItemGroup = group;
    27. }
    To copy to clipboard, switch view to plain text mode 


    But how i could send a mouse event on QGraphicsItemGroup item to trigger the doouble click function?

    Thx
    Last edited by Andre008; 11th March 2016 at 12:54.

Similar Threads

  1. Parallel states question
    By zephod in forum Qt Programming
    Replies: 1
    Last Post: 2nd December 2014, 14:08
  2. QML animations based on states
    By mvbhavsar in forum Newbie
    Replies: 4
    Last Post: 3rd September 2014, 09:54
  3. QPushButton with two states and colours
    By hildebrand in forum Newbie
    Replies: 3
    Last Post: 5th June 2013, 08:22
  4. Problem with QAbstractSocket and states.
    By nomadscarecrow in forum Qt Programming
    Replies: 0
    Last Post: 17th March 2010, 15:55
  5. setIcon() - different QIcon states?
    By AaronMK in forum Qt Programming
    Replies: 1
    Last Post: 29th January 2008, 03:10

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.