Results 1 to 8 of 8

Thread: How to implement doubleclickevent in QGraphicsScene (Qtcreator)??

  1. #1
    Join Date
    Jan 2011
    Posts
    4
    Thanks
    1
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default How to implement doubleclickevent in QGraphicsScene (Qtcreator)??

    I am just a week older in the field of programming with Qt. I am using QtCreator as IDE.
    I have worked my way around with the basic programming details in Qt but just cant find any useful information of how to implement events. Can someone Please help me. I am stuck with this for 2 days.

    ok. What I want to do is make event handler for mouse double click in QgraphicsScene. I created a subclass graphicsScene which inherits from QgraphicsScene and declared the handler for it. This is all done in MainWindow.h

    and, the definition is given in MainWindow.cpp. There is no error but the event handler just wont get invoked.

    Here's the dummy code : In MainWindow.h

    class graphicsScene : public QGraphicsScene
    {
    public:
    void mouseDoubleClickEvent(QGraphicsSceneMouseEvent *event);
    };

    In MainWindow.cpp

    MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent),
    ui(new Ui::MainWindow)
    {
    ui->setupUi(this);
    scene = new QGraphicsScene(this);
    scene->setSceneRect(0,0,310,310);
    }

    void graphicsScene::mouseDoubleClickEvent(QGraphicsScen eMouseEvent *event)
    {
    QGraphicsScene::mouseDoubleClickEvent(event);
    int x = event->scenePos().x();
    int y = event->scenePos().x();
    qDebug()<<x<<y;
    }


    Please Help.

  2. #2
    Join Date
    Dec 2010
    Location
    US, Washington State
    Posts
    54
    Thanks
    3
    Thanked 7 Times in 7 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: How to implement doubleclickevent in QGraphicsScene (Qtcreator)??

    Could you use code tags, it will make reading and referencing the code easier.
    Anyway, you are making an instance of "QGraphicsScene" when instead it you need the derived class.
    Qt Code:
    1. scene = new QGraphicscene(this);
    To copy to clipboard, switch view to plain text mode 
    should be
    Qt Code:
    1. scene = new graphicsScene(this);
    To copy to clipboard, switch view to plain text mode 

  3. The following user says thank you to pkohut for this useful post:

    codegenie (2nd January 2011)

  4. #3
    Join Date
    Jan 2011
    Posts
    4
    Thanks
    1
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: How to implement doubleclickevent in QGraphicsScene (Qtcreator)??

    I am sorry for the code tags, next time i will surely do.
    Your the best , dude.
    Thanks for the help, it worked. Sorry if this post seems stupid but I cant help , I am a newbie.

  5. #4
    Join Date
    Dec 2010
    Location
    US, Washington State
    Posts
    54
    Thanks
    3
    Thanked 7 Times in 7 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: How to implement doubleclickevent in QGraphicsScene (Qtcreator)??

    Quote Originally Posted by codegenie View Post
    Thanks for the help, it worked. Sorry if this post seems stupid but I cant help , I am a newbie.
    NP, I'm a little over a month full time with Qt and learning the ropes myself.

  6. #5
    Join Date
    Jan 2011
    Posts
    4
    Thanks
    1
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: How to implement doubleclickevent in QGraphicsScene (Qtcreator)??

    Well, I guess I found myself another problem.
    The event handler I just implemented doesn't have the scope to alter the scene or the items inside the scene. Since the scene and the items are implemented in MainWindow, i tried to inherit the graphicsScene from MainWindow. Then the scope problem is resolved but their is no output Window.
    This is the Code in MainWindow.h:
    Qt Code:
    1. class MainWindow : public QMainWindow {
    2. ...
    3. QGraphicsRectItem *obstRect[Xgrid][Ygrid];
    4. graphicsScene *scene;
    5. void toggle(int ,int);
    6. ...
    7. };
    8. class graphicsScene : public QGraphicsScene
    9. {
    10. public:
    11. void mouseDoubleClickEvent(QGraphicsSceneMouseEvent *event);
    12. };
    To copy to clipboard, switch view to plain text mode 

    The Code in MainWindow.cpp is like:

    Qt Code:
    1. MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent),
    2. ui(new Ui::MainWindow)
    3. {
    4. for(int i=0;i<Xgrid;i++)
    5. for(int j=0;j<Ygrid;j++)
    6. { obstRect[i][j] =new QGraphicsRectItem();
    7. obstRect[i][j]->setBrush(Qt::white);
    8. }
    9.  
    10. scene = new graphicsScene();
    11. scene->setSceneRect(0,0,310,310);
    12.  
    13. for(int i=0;i<Xgrid;i++)
    14. for(int j=0;j<Ygrid;j++)
    15. scene->addItem(obstRect[i][j]);
    16.  
    17. ui->graphicsView->setScene(scene);
    18. ...
    19.  
    20. }
    21.  
    22. void graphicsScene::mouseDoubleClickEvent(QGraphicsSceneMouseEvent *event)
    23. {
    24. QGraphicsScene::mouseDoubleClickEvent(event);
    25. ...
    26. //the part were error arise
    27. toggle(x,y);
    28. qDebug()<<x<<y;
    29. ...
    30. }
    31.  
    32. void MainWindow::toggle(int x, int y)
    33. {
    34. ...
    35. }
    To copy to clipboard, switch view to plain text mode 

    please gimme some solutions. Its driving me mad.

  7. #6
    Join Date
    Dec 2010
    Location
    US, Washington State
    Posts
    54
    Thanks
    3
    Thanked 7 Times in 7 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: How to implement doubleclickevent in QGraphicsScene (Qtcreator)??

    Is the background white as well as the graphics rectangle? (ps, just noticed the default constructor for QGraphicsRectItem is used, which means it is a dot on the screen with dimensions of QRect(0, 0, 0, 0))

    It's not clear, but in the double click event what are you trying to toggle? Also, more than likely "obstRect" is not needed (QGraphicsScene will manage the items for you). Just create the QGraphicsRectItem, set its brush and add to the scene. Then in the double click event, take the scene mouse position and call QGraphicsScene::items

    Qt Code:
    1. QList<QGraphicsItem> items = items(event->scenePos());
    2. foreach(QGraphicsItem * pItem, items)
    3. Toggle(pItem);
    To copy to clipboard, switch view to plain text mode 

    If you want QGraphicsRectItem's to do something different than other graphics items then code something like so -
    Qt Code:
    1. QList<QGraphicsItem> items = items(event->scenePos());
    2. foreach(QGraphicsItem * pItem, items) {
    3. if(qobject_cast<QGraphicsRectItem*>(pItem))
    4. DoSomeThingWithTheRectItem(pItem);
    5. else
    6. Toggle(pItem);
    To copy to clipboard, switch view to plain text mode 

  8. #7
    Join Date
    Jan 2011
    Posts
    4
    Thanks
    1
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: How to implement doubleclickevent in QGraphicsScene (Qtcreator)??

    I skipped the rest of the code b'coz i thought it was unimportant. All I wanted to ask is how can i edit the scene or item which is declared in MainWindow from the eventHandler.

    Actually, I tried your code but there were some errors. But I got the idea of "what you were trying to tell me". So I checked the Manuals and saw the method itemAt() that suited my purpose of getting the item(just 1) using screen position as argument.

    I found your reply very helpful and once again you saved me. Thanks.

    Can I ask you from where did u learn Qt. Are there any tutorials online b'coz i can't find any tutorial related to Qtcreator. I need a good base in the the Qtcreator architecture. If there are any good e-books ,please refer that too.

    Regards Manoj.

  9. #8
    Join Date
    Dec 2010
    Location
    US, Washington State
    Posts
    54
    Thanks
    3
    Thanked 7 Times in 7 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: How to implement doubleclickevent in QGraphicsScene (Qtcreator)??

    Quote Originally Posted by codegenie View Post
    Actually, I tried your code but there were some errors.
    Should have mentioned the code was untested and hadn't gone through a compiler.

    Quote Originally Posted by codegenie View Post
    Can I ask you from where did u learn Qt. Are there any tutorials online b'coz i can't find any tutorial related to Qtcreator.
    Still just learning the Qt way of doing things. Found this group about a week ago and started reading past threads and try solving some of the problems presented, like yours.

    Based on "High_flyers" sig, I bought the "Foundations of Qt Development" on New Years Eve. Literally, in the first 2 chapters, most of the issues I'd dealt with during the prior month of "being serious with Qt" were resolved. You got to understand though that I didn't just "get it", as there is almost 20 years of C++ and other framework baggage that I bring with me on my new Qt adventure.

Similar Threads

  1. QtCreator 2.0 crashes application working in QtCreator 1.3
    By been_1990 in forum Qt Programming
    Replies: 2
    Last Post: 21st September 2010, 12:58
  2. Replies: 1
    Last Post: 10th February 2010, 07:28
  3. Trying to implement QMainWindow...
    By winston2020 in forum Qt Programming
    Replies: 2
    Last Post: 23rd October 2008, 19:12
  4. Replies: 2
    Last Post: 1st November 2007, 08:25
  5. how to implement it ?
    By qtopiahooo in forum Qt Programming
    Replies: 3
    Last Post: 25th October 2006, 17:01

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.