Results 1 to 15 of 15

Thread: QGraphicsRectItem signal

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Join Date
    Sep 2010
    Posts
    6
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows Symbian S60

    Default QGraphicsRectItem signal

    Hello!

    How can I use signals of QGraphicsRectItem?

    I tried this:

    m_rectangle = new QGraphicsRectItem( 0, 0, 40, 40 );
    m_rectangle->setPen( QPen(Qt::black) );
    m_rectangle->setBrush( QBrush( QColor( 0, 200, 0, 200 ) ) );

    connect(m_rectangle, SIGNAL(MousePressEvent), this, SLOT(animate()));

    but I get this:
    no matching function for call to 'GraphicsViewDemo::connect(QGraphicsRectItem*&, const char*, GraphicsViewDemo* const, const char*)'

  2. #2
    Join Date
    Jan 2006
    Location
    Germany
    Posts
    4,380
    Thanks
    19
    Thanked 1,005 Times in 913 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows Symbian S60
    Wiki edits
    5

    Default Re: QGraphicsRectItem signal

    Where does a QGraphicsRectItem has a signal called MousePressEvent or any other signal at all? And items do not inherit QObject which is needed for connections.

    So you have to do it yourself. Just create a custom item which inherits QObject.
    Qt Code:
    1. class MyItem : public QObject, public QGraphicsRectItem
    To copy to clipboard, switch view to plain text mode 

  3. #3
    Join Date
    May 2009
    Location
    Canada
    Posts
    163
    Thanks
    7
    Thanked 20 Times in 20 Posts
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Windows Android

    Default Re: QGraphicsRectItem signal

    That is one way to do it.

    Or you could sidestep connections altogether and write a custom class that inherits from QGraphicsRectItem only, re-implementing mousePressEvent().
    Last edited by Urthas; 30th September 2010 at 22:07. Reason: updated contents

  4. #4
    Join Date
    Sep 2010
    Posts
    6
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows Symbian S60

    Default Re: QGraphicsRectItem signal

    Sorry for my easy questions, but I'm new in qt. So if I have a class inherit QObject and QGraphicsRectItem why has it mousePressEvent signal? QObject and QGraphicsRectItem haven't mousePressEvent signal, have they?

  5. #5
    Join Date
    Mar 2010
    Location
    Heredia, Costa Rica
    Posts
    257
    Thanks
    24
    Thanked 17 Times in 14 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: QGraphicsRectItem signal

    Hi,

    QGraphicsRectItem does not have a signal MousePressEvent. It has the protected function virtual void mousePressEvent ( QGraphicsSceneMouseEvent * event ).

    If you want to emit a signal when mousePressEvent is called, then you need to subclass your m_rectangle from QObject and QGraphicsRectItem.

    class MyItem : public QObject, public QGraphicsRectItem

    For example:
    myrectangle.h
    Qt Code:
    1. #ifndef MYRECTANGLE_H
    2. #define MYRECTANGLE_H
    3.  
    4. #include <QtGui>
    5. #include <QGraphicsRectItem>
    6.  
    7. class myrectangle : public QObject, public QGraphicsRectItem
    8. {
    9. Q_OBJECT
    10. public:
    11. myrectangle();
    12.  
    13. signals:
    14. void myPressSignal();
    15. protected:
    16. void mousePressEvent(QGraphicsSceneMouseEvent * event);
    17. };
    18.  
    19. #endif // MYRECTANGLE_H
    To copy to clipboard, switch view to plain text mode 

    myrectangle.cpp
    Qt Code:
    1. #include "myrectangle.h"
    2.  
    3. myrectangle::myrectangle()
    4. {
    5. }
    6.  
    7.  
    8. void myrectangle::mousePressEvent(QGraphicsSceneMouseEvent * event)
    9. {
    10. QGraphicsItem::mousePressEvent(event); //Call the ancertor
    11.  
    12. emit myPressSignal(); //Emits the signal
    13. }
    To copy to clipboard, switch view to plain text mode 
    Last edited by qlands; 1st October 2010 at 14:31.

  6. #6
    Join Date
    Sep 2010
    Posts
    6
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows Symbian S60

    Default Re: QGraphicsRectItem signal

    Thank you. Now I have just one problem:
    I created my QGraphicsRectItem int this way: m_rectangle = new QGraphicsRectItem( 0, 0, 40, 40 );
    and I tried this in my class: QMyButton::QMyButton(qreal x, qreal y, qreal w, qreal h, QGraphicsItem *parent) : QGraphicsRectItem(x, y, w, h, parent)
    but I got this error: undefined reference to `QMyButton::QMyButton(float, float, float, float, QGraphicsItem*)'

  7. #7
    Join Date
    Jan 2006
    Location
    Germany
    Posts
    4,380
    Thanks
    19
    Thanked 1,005 Times in 913 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows Symbian S60
    Wiki edits
    5

    Default Re: QGraphicsRectItem signal

    Well you have to declare that constructor as well or use the standard constructor and set the geometry with a member method.

  8. #8
    Join Date
    Sep 2010
    Posts
    6
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows Symbian S60

    Default Re: QGraphicsRectItem signal

    It's my qmybutton.h

    Qt Code:
    1. #ifndef QMYBUTTON_H
    2. #define QMYBUTTON_H
    3.  
    4. #include <QGraphicsRectItem>
    5.  
    6. class QMyButton : public QObject, public QGraphicsRectItem
    7. {
    8. Q_OBJECT
    9. public:
    10. QMyButton(qreal x, qreal y, qreal w, qreal h, QGraphicsItem *parent = 0);
    11.  
    12. signals:
    13. void myPressSignal();
    14.  
    15. protected:
    16. void mousePressEvent(QGraphicsSceneMouseEvent * event);
    17. };
    18.  
    19. #endif // QMYBUTTON_H
    To copy to clipboard, switch view to plain text mode 

    This line declare the constructor: QMyButton(qreal x, qreal y, qreal w, qreal h, QGraphicsItem *parent = 0); ?
    I already have this.

  9. #9
    Join Date
    Mar 2010
    Location
    Heredia, Costa Rica
    Posts
    257
    Thanks
    24
    Thanked 17 Times in 14 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: QGraphicsRectItem signal

    So the qmybutton.cpp should be something like:

    Qt Code:
    1. QMyButton::QMyButton(qreal x, qreal y, qreal w, qreal h, QGraphicsItem *parent)
    2. {
    3.  
    4. }
    5.  
    6.  
    7. void QMyButton::mousePressEvent(QGraphicsSceneMouseEvent * event)
    8. {
    9. QGraphicsItem::mousePressEvent(event); //Call the ancertor
    10.  
    11. emit myPressSignal(); //Emits the signal
    12. }
    To copy to clipboard, switch view to plain text mode 

    Why do you need this constructor?

    You can also have a simple constructor like: QMyButton();

    then:
    Qt Code:
    1. //Create the button
    2. QMyButton mybutton = new QMyButton();
    3. //Sets the rect
    4. mybutton->setRect(0,0,100,100);
    5. //Add it the scene.
    6. myScene->addItem(mybutton);
    7. //Or add it to a parent item:
    8. mybutton->setParentItem(myParent);
    To copy to clipboard, switch view to plain text mode 

  10. #10
    Join Date
    Jan 2006
    Location
    Germany
    Posts
    4,380
    Thanks
    19
    Thanked 1,005 Times in 913 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows Symbian S60
    Wiki edits
    5

    Default Re: QGraphicsRectItem signal

    Maybe you need to rerun qmake.

  11. #11
    Join Date
    Sep 2010
    Posts
    6
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows Symbian S60

    Default Re: QGraphicsRectItem signal

    I know I can use simple constructor, but I used this constructor of QGraphicsRectItem: QGraphicsRectItem(qreal x, qreal y, qreal w, qreal h, QGraphicsItem *parent = 0)
    and I want create a constructor like: QMyButton::QMyButton(qreal x, qreal y, qreal w, qreal h, QGraphicsItem *parent)
    And I want to know why it doesn't work.

    I use Qt Creator, so if I'm right qmake run ever when I build the project.

    this is my qmybutton.h:
    Qt Code:
    1. #ifndef QMYBUTTON_H
    2. #define QMYBUTTON_H
    3.  
    4. #include <QGraphicsRectItem>
    5.  
    6. class QMyButton : public QObject, public QGraphicsRectItem
    7. {
    8. Q_OBJECT
    9. public:
    10. QMyButton();
    11. QMyButton(qreal x, qreal y, qreal w, qreal h, QGraphicsItem *parent = 0);
    12.  
    13. signals:
    14. void myPressSignal();
    15.  
    16. protected:
    17. void mousePressEvent(QGraphicsSceneMouseEvent * event);
    18. };
    19.  
    20. #endif // QMYBUTTON_H
    To copy to clipboard, switch view to plain text mode 

    and this is my qmybutton.cpp:

    Qt Code:
    1. #include "qmybutton.h"
    2.  
    3. QMyButton::QMyButton()
    4. {
    5. }
    6.  
    7. QMyButton::QMyButton(qreal x, qreal y, qreal w, qreal h, QGraphicsItem *parent) :
    8. QGraphicsRectItem(x, y, w, h, parent)
    9. {
    10. }
    11.  
    12. void QMyButton::mousePressEvent(QGraphicsSceneMouseEvent * event)
    13. {
    14. QGraphicsItem::mousePressEvent(event); //Call the ancertor
    15.  
    16. emit myPressSignal(); //Emits the signal
    17. }
    To copy to clipboard, switch view to plain text mode 

  12. #12
    Join Date
    Mar 2010
    Location
    Heredia, Costa Rica
    Posts
    257
    Thanks
    24
    Thanked 17 Times in 14 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: QGraphicsRectItem signal

    Can you provide the error message?

  13. #13
    Join Date
    Jan 2006
    Location
    Germany
    Posts
    4,380
    Thanks
    19
    Thanked 1,005 Times in 913 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows Symbian S60
    Wiki edits
    5

    Default Re: QGraphicsRectItem signal

    Quote Originally Posted by kovacadam View Post
    Sorry for my easy questions, but I'm new in qt.
    Then why don't you use the Newbie section? (-> moved)

    Do you want only a "mouse click on item" signal or other signals as well. If you only need to be notified if a item was clicked then maybe following is a better solution than subclassing the item:
    • Make your items selectable and use QGraphicsScene::selectionChanged() to get notified.
    • Reimp QGraphicsScene::mousePressEvent() and detect the underlaying item with QGraphicsScene::itemAt().

Similar Threads

  1. Take a screenshot of a QGraphicsRectItem
    By yazwas in forum Qt Programming
    Replies: 1
    Last Post: 12th October 2009, 13:14
  2. QGraphicsRectItem with text
    By ttvo in forum Qt Programming
    Replies: 1
    Last Post: 26th August 2009, 19:53
  3. QGraphicsRectItem
    By c_srikanth1984 in forum Qt Programming
    Replies: 1
    Last Post: 29th July 2009, 09:04
  4. QGraphicsWidget vs. QGraphicsRectItem
    By Bill in forum Newbie
    Replies: 3
    Last Post: 27th July 2009, 09:02
  5. How can I use QGraphicsRectItem?
    By learning_qt in forum Qt Programming
    Replies: 3
    Last Post: 14th January 2009, 10:00

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.