Results 1 to 12 of 12

Thread: I cannot get QGraphicsRectItem to change colour on mousedoubleclickevent

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Join Date
    Jan 2008
    Location
    Alameda, CA, USA
    Posts
    5,321
    Thanks
    316
    Thanked 871 Times in 858 Posts
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: I cannot get QGraphicsRectItem to change colour on mousedoubleclickevent

    how do I make mousedoubleclick(event) to work on my current QGraphicsRectItems?
    First, remove the inheritance from QGraphicsRectItem that you added to GatewayWidget, and remove the mouseDoubleClickEvent() code too.

    Second, the steps you need to follow are these:

    1. Derive a new class from QGraphicsRectItem and add an event handler for the mouseDoubleClickEvent to it:

    Qt Code:
    1. // .h
    2.  
    3. class GatewayRectItem : public QGraphicsRectItem
    4. {
    5. public:
    6. GatewayRectItem( QGraphicsItem * parent = nullptr ) : QGraphicsRectItem( parent ) {}
    7.  
    8. protected:
    9. void mouseDoubleClickEvent( QGraphicsSceneMouseEvent * event );
    10.  
    11. // If you want to handle other events, add handlers for those here too
    12. }
    13.  
    14. // .cpp
    15.  
    16. // Implement the event handler
    17. void GatewayRectItem::mouseDoubleClickEvent( QGraphicsSceneMouseEvent * event )
    18. {
    19. // You now have the double-click event. The rect that was clicked is "this". What do you want to do?
    20.  
    21. // If you want to also have the QGraphicsRectItem base class process this event, you must call
    22. QGraphicsRectItem::mouseDoubleClickEvent( event );
    23. }
    To copy to clipboard, switch view to plain text mode 

    2 - When you create your rect items, you call the new constructor. You also need to make sure the item is selectable so it responds to mouse clicks:

    Qt Code:
    1. GatewayRectItem * rect = new GatewayRectItem();
    2. rect->setFlags( QGraphicsItem::ItemIsSelectable );
    3. scene->addItem( rect );
    To copy to clipboard, switch view to plain text mode 

    3 - Decide what you want to do with the double-click. In this case, I would have the GatewayRectItem emit a signal that you can handle somewhere else in a slot that can determine the correct color. To do that, you need to also derive GatewayRectItem from QObject so it can send signals:

    Qt Code:
    1. // .h
    2.  
    3. class GatewayRectItem : public QObject, public QGraphicsRectItem
    4. {
    5. Q_OBJECT;
    6.  
    7. public:
    8. GatewayRectItem( QGraphicsItem * parent = nullptr );
    9.  
    10. signals:
    11. void doubleClicked( QGraphicsSceneMouseEvent * event );
    12.  
    13. protected:
    14. void mouseDoubleClickEvent( QGraphicsSceneMouseEvent * event );
    15. }
    16.  
    17. // .cpp
    18.  
    19. GatewayRectItem::GatewayRectItem( QGraphicsItem * parent )
    20. : QGraphicsRectItem( parent )
    21. {
    22. // Can just do this here, so all items have the flag set
    23. setFlags( QGraphicsItem::ItemIsSelectable );
    24. }
    25.  
    26. // Implement the event handler
    27. void GatewayRectItem::mouseDoubleClickEvent( QGraphicsSceneMouseEvent * event )
    28. {
    29. // You now have the double-click event. Emit the signal to let a slot know
    30. emit doubleClicked( event );
    31.  
    32. // If you want to also have the QGraphicsRectItem base class process this event, you must call
    33. QGraphicsRectItem::mouseDoubleClickEvent( event );
    34. }
    To copy to clipboard, switch view to plain text mode 

    4 - Add a slot to handle this signal. I assume you would want to do that in your GatewayWidget class:

    Qt Code:
    1. // .h
    2.  
    3. class GatewayWidget : public VgdWidget
    4. {
    5. // ... all the original code
    6.  
    7. public slots:
    8. void onRectDoubleClicked( QGraphicsSceneMouseEvent * event );
    9.  
    10. // ...
    11. };
    12.  
    13. // .cpp
    14.  
    15. void GatewayWidget::onRectDoubleClicked( QGraphicsSceneMouseEvent * event )
    16. {
    17. // first, get the rect instance that sent the signal
    18. GatewayRectItem * rect = qobject_cast< GatewayRectItem * >( sender() );
    19.  
    20. // Now you have the rect that was double-clicked, and you have the event details if you need them
    21. // Do whatever you need to do to determine what color the rect should be, then set the color
    22.  
    23. rect->setBrush( QBrush( theNewColor ) );
    24. }
    To copy to clipboard, switch view to plain text mode 

    5 - And finally, when you create the rect, you need to connect its signal to the GatewayWidget's slot:

    Qt Code:
    1. GatewayRectItem * rect = new GatewayRectItem();
    2.  
    3. // Don't need this now since it is set in the constructor for GatewayRectItem
    4. // rect->setFlags( QGraphicsItem::ItemIsSelectable );
    5.  
    6. scene->addItem( rect );
    7.  
    8. connect( rect, &GatewayRectItem::doubleClicked, this, &GatewayWidget::onRectDoubleClicked );
    To copy to clipboard, switch view to plain text mode 

    None of this code has been tested - I am just writing it here. There could be typos, so don't just copy and paste without checking it.
    <=== The Great Pumpkin says ===>
    Please use CODE tags when posting source code so it is more readable. Click "Go Advanced" and then the "#" icon to insert the tags. Paste your code between them.

  2. #2
    Join Date
    Jan 2020
    Posts
    13
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: I cannot get QGraphicsRectItem to change colour on mousedoubleclickevent

    Thank you for your help once more, without your help I would not have progressed this far, I do not take it for granted. It is working , it changes color on doubleclick.

    May I please ask another question. Now I want that when I doubleclick on the rect, it must then pop up a widget, but the widget is looking for the index of the rect so that It can pop the widget with the information specific to that rect. I tried it like the code below to get the index of the rect, but it only pops up the widget once(with the correct information). Then nothing after that to any subsequent rect that I click. my question is should I store the indexes of each rect on creation with QLIST[] or how should I store the index of the clicked rect ? i would appreciate your assistance once more


    Qt Code:
    1. void GatewayWidget::onRectDoubleClicked(QGraphicsSceneMouseEvent * event)
    2. {
    3. // first, get the rect instance that sent the signal
    4. GatewayRectItem * rect = qobject_cast< GatewayRectItem * >(sender());
    5. // rect->setBrush(QBrush(Qt::black));
    6.  
    7. QList<QGraphicsItem*> stackOfRect = scene->items();
    8.  
    9. int indexOfShape = stackOfRect.indexOf(rect);// trying to index the clicked rect
    10. if (CORE::castPtrType<C2DM::LinkState>(m_pLinkStateListModel->getTitle(indexOfShape)))// getTitle is looking for the index clicked
    11. {
    12. // create link ctrl widget
    13. //C2DM::LinkState *pState = CORE::castPtrType<C2DM::LinkState>(indexOfShape);
    14. const C2DM::LinkState *pState = CORE::castPtrType<C2DM::LinkState>(m_pLinkStateListModel->getTitle(indexOfShape));
    15. showLinkToolBoxWidget(pState->m_strLinkId);
    16.  
    17. }
    18.  
    19.  
    20.  
    21.  
    22. }
    To copy to clipboard, switch view to plain text mode 

  3. #3
    Join Date
    Jan 2008
    Location
    Alameda, CA, USA
    Posts
    5,321
    Thanks
    316
    Thanked 871 Times in 858 Posts
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: I cannot get QGraphicsRectItem to change colour on mousedoubleclickevent

    if (CORE::castPtrType<C2DM::LinkState>(m_pLinkStateLi stModel->getTitle(indexOfShape)))// getTitle is looking for the index clicked
    Why do you think that the scene index returned in the QList is a constant that won't change? The scene could change the order of items in it based on the current stacking order of things in the scene, and the order can change as you add new items to the scene. And if you have QGraphicsLineItem, QGraphicsTextItem, and QGraphicsRectItem instances in the scene, all of these will have an index, too, and the index will depend on where they are in the hierarchy of scene objects.

    The only way you can guarantee that you can look up something by an index number is if you assign the index number yourself. The easiest way to do this is to use the mechanism you are already using on your rect objects: use another setData() property to assign the index to your rects. If you are using the key "0" already, then use a different key (1) to assign an index when you create the rect. If you are only indexing the rects, then start at index 0 and increment as you add new rects.

    If you ever delete a rect, then you will have to make sure that your model and your rects remain consistent - if you remove a rect with index 42, because you have removed the title with index 42, then you have to make sure that you either re-index everything from 42 up or access title <---> rect relationship in a way that is not affected by missing index numbers. Once again, an std:: map<> would work great for this.
    Last edited by d_stranz; 6th February 2020 at 18:03.
    <=== The Great Pumpkin says ===>
    Please use CODE tags when posting source code so it is more readable. Click "Go Advanced" and then the "#" icon to insert the tags. Paste your code between them.

  4. #4
    Join Date
    Jan 2020
    Posts
    13
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: I cannot get QGraphicsRectItem to change colour on mousedoubleclickevent

    Hello

    I just came to say thank you I followed your advice and it worked beautifully. I also wanted to let you know that I was able to meet my deadline on the 7th of Feb 2020, I was able to demonstrate to my boss of what I have completed so far on the gui and he was happy. I acknowledge that I would not have been able to demonstrate any functionality if not for your guidance from your generous heart, and I say thank you. I thank you for how quick you respond to questions whenever I post. You have taught me a lot and I hope to learn more from you in the future. I thank you! May God bless you!

    I also wanted to ask if there is anywhere else I can find you where you offer courses, like Pluralsight or Udemy?

  5. #5
    Join Date
    Jan 2008
    Location
    Alameda, CA, USA
    Posts
    5,321
    Thanks
    316
    Thanked 871 Times in 858 Posts
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: I cannot get QGraphicsRectItem to change colour on mousedoubleclickevent

    You are welcome. I am glad you made your deadline and that your boss is happy.

    I don't teach any courses. I have been programming in C / C++ for nearly 40 years and using Qt for almost 15 years and in that time I have learned a few useful things, so I am glad I can use this forum to help others from time-to-time.

    I am sure you can search any of the online sites for C++ and Qt courses. Here is one list. KDAB also offers courses in Europe, the USA, and other places on C++ and Qt programming.
    <=== The Great Pumpkin says ===>
    Please use CODE tags when posting source code so it is more readable. Click "Go Advanced" and then the "#" icon to insert the tags. Paste your code between them.

Similar Threads

  1. Replies: 6
    Last Post: 11th January 2012, 10:11
  2. Change the colour of a table view row
    By Splatify in forum Newbie
    Replies: 0
    Last Post: 7th March 2011, 06:31
  3. Change the colour of a QT TextEdit row
    By Splatify in forum Newbie
    Replies: 3
    Last Post: 3rd March 2011, 14:09
  4. Replies: 2
    Last Post: 3rd May 2010, 12:18
  5. Change colour of QString
    By Morea in forum Newbie
    Replies: 9
    Last Post: 10th February 2006, 22:31

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
  •  
Qt is a trademark of The Qt Company.