Results 1 to 4 of 4

Thread: itemAt - does not select rect

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Join Date
    Jul 2008
    Location
    St. Louis, MO
    Posts
    10
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Windows

    Default itemAt - does not select rect

    I am a relative newcomer to Qt, and am developing a process management tool using Qt.
    The tool will display rectangles (tasks) with links between tasks.

    I am using QGraphicsScene and QGraphicsItem with QGraphicsView as the widget to display.

    I want to use the mouse buttons in traditional ways to add, view, or modify tasks and links.

    I have reviewed the examples in Qt help plus I have a very similar example from the book "C++ GUI Programming with Qt4" by Blanchette and Summerfield, and am copying and modifying snippets from the Diagram application in this book.

    So, when I use the right-mouse button I want to invoke one menu when the mouse is not over a task, and invoke a different menu when the mouse is over a task (should be simple, right?).

    To keep it simple, I'm only working on tasks now; I'll get to links later.

    I have a bug in my code so that when I right-mouse click the same menu appears, regardless of whether I am over a task or not.

    The issue appears to be in the coordinates that I use to know where the mouse is and whether or not it is over a task.

    The code that does this is

    QGraphicsItem* selectedItem = this->itemAt(mouseEvent->scenePos());
    in the method
    void ProcessScene::mousePressEvent(QGraphicsSceneMouseE vent* mouseEvent)
    where ProcessScene is a subclass of QGraphicsScene.

    However, when I test this, knowing that the mouse is over an existing task in the view, the method "itemAt" does not return an item.

    I'm stumped on where to look to debug this.

    By the way, the help text, at least to me, seems ambiguous when discussing the coordinate systems returned for a QRectF. It isn't clear to me how to obtain coordinates that are consistent with the scene position returned by mouseEvent->scenePos(). It would be most helpful during debugging. I would be grateful for any suggestions on where to look for clarification.

    Furthermore, on a separate note, a "task" object is a subclass of QGraphicsRectItem (per the example in the book). It requires reimplemening the paint function to add text.

    Is there a better way? For example, is it preferred to use a QGraphicsWidget (such as QLabel) or maybe a combination of QGraphicsRectItem with a QGraphicsTextItem?

    The reason I ask is that we will have many tasks displayed at once, and I want to minimize memory and processing.

    Thanks in advance for the assistance.

    Regards,

    George

  2. #2
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: itemAt - does not select rect

    Quote Originally Posted by gjshannon View Post
    However, when I test this, knowing that the mouse is over an existing task in the view, the method "itemAt" does not return an item.
    Check if the coordinates are correct and if they are within the item's sceneRect(). Also check if you implemented shape() properly, maybe you're just hitting a transparent area.

    By the way, the help text, at least to me, seems ambiguous when discussing the coordinate systems returned for a QRectF. It isn't clear to me how to obtain coordinates that are consistent with the scene position returned by mouseEvent->scenePos(). It would be most helpful during debugging. I would be grateful for any suggestions on where to look for clarification.
    Search the forum. I recently posted a verbose explanation of the GV coordinate system.

    Furthermore, on a separate note, a "task" object is a subclass of QGraphicsRectItem (per the example in the book). It requires reimplemening the paint function to add text.
    Consider adding a QGraphicsTextItem as a child of the rect item, you might get additional features (like inline editing) for free.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  3. #3
    Join Date
    Jul 2008
    Location
    St. Louis, MO
    Posts
    10
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: itemAt - does not select rect

    Thanks wysota.

    I expermented with adding just a QGraphicsRectItem to make sure my code for finding items under the mouse worked, and it did just fine.

    A defect occurs when using the example from the book I referenced, probably in the paint method or perhaps in the boundingRect method reimplemented in my subclass of QGraphicsRectItem.

    I'll try your approach. Thanks.

  4. #4
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: itemAt - does not select rect

    Without knowing what you did in the reimplementation it is hard to find a solution. If I had to guess, I would say you implemented boundingRect() in a wrong way. If you show us the code, maybe we can spot the problem right away (you could have done that yesterday, you know...).
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


Similar Threads

  1. QGraphicsSvgItem, problem with scaling (invalid bounding rect)
    By Józef Paczenko in forum Qt Programming
    Replies: 3
    Last Post: 8th June 2010, 08:58
  2. How to get Rect or Size by QStyle?
    By nifei in forum Qt Programming
    Replies: 8
    Last Post: 21st November 2008, 09:09
  3. QTableWidget row select
    By nowire75 in forum Newbie
    Replies: 4
    Last Post: 23rd December 2007, 18:59

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.