Results 1 to 15 of 15

Thread: QGraphicsRectItem signal

  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 23: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
    253
    Thanks
    23
    Thanked 16 Times in 13 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 15:31.

  6. #6
    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().

  7. #7
    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*)'

  8. #8
    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.

  9. #9
    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.

  10. #10
    Join Date
    Mar 2010
    Location
    Heredia, Costa Rica
    Posts
    253
    Thanks
    23
    Thanked 16 Times in 13 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 

  11. #11
    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.

  12. #12
    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 

  13. #13
    Join Date
    Mar 2010
    Location
    Heredia, Costa Rica
    Posts
    253
    Thanks
    23
    Thanked 16 Times in 13 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: QGraphicsRectItem signal

    Can you provide the error message?

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

    Default Re: QGraphicsRectItem signal

    undefined reference to `QMyButton::QMyButton(float, float, float, float, QGraphicsItem*)'
    In this file:

    Qt Code:
    1. // Symbian Foundation Example Code
    2. //
    3. // This software is in the public domain. No copyright is claimed, and you
    4. // may use it for any purpose without license from the Symbian Foundation.
    5. // No warranty for any purpose is expressed or implied by the authors or
    6. // the Symbian Foundation.
    7. #include "graphicsviewdemo.h"
    8.  
    9. #include <QVBoxLayout>
    10.  
    11. #include <QPushButton>
    12.  
    13. #include <QGraphicsView>
    14. #include <QGraphicsScene>
    15. #include <QGraphicsEllipseItem>
    16. #include <QGraphicsRectItem>
    17.  
    18. #include <QGraphicsItemAnimation>
    19. #include <QTimeLine>
    20. #include <qmybutton.h>
    21.  
    22. GraphicsViewDemo::GraphicsViewDemo(QWidget *parent)
    23. : QWidget(parent)
    24. {
    25. QVBoxLayout *layout = new QVBoxLayout( this );
    26.  
    27. view->setBackgroundBrush( QPixmap( ":/images/qt-tile.png" ) );
    28. QPushButton *button = new QPushButton( tr("Animate") );
    29.  
    30. layout->addWidget( view );
    31. layout->addWidget( button );
    32.  
    33. connect( button, SIGNAL(clicked()), this, SLOT(animate()) );
    34.  
    35. QGraphicsScene *scene = new QGraphicsScene( QRect( -50, -50, 100, 100 ) );
    36. m_ellipse = new QGraphicsEllipseItem( 25, -10, 20, 20 );
    37. m_ellipse->setPen( QPen(Qt::darkRed) );
    38. m_ellipse->setBrush( Qt::red );
    39.  
    40. QMyButton *m_rectangle = new QMyButton( 0, 0, 40, 40 ); // <----------------------- In this line
    41. m_rectangle->setPen( QPen(Qt::black) );
    42. m_rectangle->setBrush( QBrush( QColor( 0, 200, 0, 200 ) ) );
    43.  
    44. scene->addItem( m_ellipse );
    45. scene->addItem( m_rectangle );
    46.  
    47. connect(m_rectangle, SIGNAL(myPressSignal), this, SLOT(animate()));
    48.  
    49. view->setScene( scene );
    50. }
    51.  
    52. void GraphicsViewDemo::animate()
    53. {
    54. QTimeLine *timeLine = new QTimeLine(3000);
    55. timeLine->setFrameRange(0, 100);
    56.  
    57. ellipseAnimation->setItem(m_ellipse);
    58. ellipseAnimation->setTimeLine(timeLine);
    59.  
    60. /* QGraphicsItemAnimation *rectAnimation = new QGraphicsItemAnimation();
    61.   rectAnimation->setItem(m_rectangle);
    62.   rectAnimation->setTimeLine(timeLine);
    63.   rectAnimation->setPosAt(2, QPointF(30, 30));*/
    64.  
    65. for (int i = 0; i <= 200; ++i)
    66. {
    67. ellipseAnimation->setRotationAt( i/200.0, 360.0 * i/200.0 );
    68. //rectAnimation->setRotationAt( i/200.0, -720.0 * i/200.0 );
    69. qreal s = i>100?(1.0-((i-100)/100.0)):(i/100.0);
    70. //rectAnimation->setScaleAt( i/200.0, 1.0+s, 1.0+s );
    71. }
    72.  
    73. connect( timeLine, SIGNAL(finished()), timeLine, SLOT(deleteLater()) );
    74. connect( timeLine, SIGNAL(finished()), ellipseAnimation, SLOT(deleteLater()) );
    75. //connect( timeLine, SIGNAL(finished()), rectAnimation, SLOT(deleteLater()) );
    76.  
    77. timeLine->start();
    78. }
    To copy to clipboard, switch view to plain text mode 

  15. #15
    Join Date
    Mar 2010
    Location
    Heredia, Costa Rica
    Posts
    253
    Thanks
    23
    Thanked 16 Times in 13 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: QGraphicsRectItem signal

    Try and see if using local includes fix the error:

    #include "qmybutton.h"

Similar Threads

  1. Take a screenshot of a QGraphicsRectItem
    By yazwas in forum Qt Programming
    Replies: 1
    Last Post: 12th October 2009, 14:14
  2. QGraphicsRectItem with text
    By ttvo in forum Qt Programming
    Replies: 1
    Last Post: 26th August 2009, 20:53
  3. QGraphicsRectItem
    By c_srikanth1984 in forum Qt Programming
    Replies: 1
    Last Post: 29th July 2009, 10:04
  4. QGraphicsWidget vs. QGraphicsRectItem
    By Bill in forum Newbie
    Replies: 3
    Last Post: 27th July 2009, 10:02
  5. How can I use QGraphicsRectItem?
    By learning_qt in forum Qt Programming
    Replies: 3
    Last Post: 14th January 2009, 11: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.