Results 1 to 7 of 7

Thread: Troubles with QGraphicsItem::isSelected/setSelected

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Join Date
    Jun 2007
    Location
    Plymouth, UK
    Posts
    36
    Thanks
    6
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Unhappy Troubles with QGraphicsItem::isSelected/setSelected

    Hi at all,

    I'm using Qt4.3 under MacOS X, through Xcode. I'm experiencing a problem concerning QGraphicsItem's functions isSelected() and setSelected().

    I created a very simple custom graphical item, defining the following class:

    Qt Code:
    1. #ifndef SIMTARGET_H
    2. #define SIMTARGET_H
    3.  
    4. #include <QGraphicsItem>
    5.  
    6. class Sim_Target : public QObject, public QGraphicsItem {
    7.  
    8. Q_OBJECT
    9.  
    10. public:
    11.  
    12. // Constructor
    13. Sim_Target();
    14.  
    15. // Shape
    16. QRectF boundingRect() const;
    17. void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget);
    18.  
    19. private:
    20. QColor color;
    21.  
    22. };
    23.  
    24. #endif
    To copy to clipboard, switch view to plain text mode 

    Then I have included it between the public members of my QMainWindow class:

    Qt Code:
    1. Sim_Target *simulationTarget;
    To copy to clipboard, switch view to plain text mode 

    Calling a SLOT function (invoked pressing a button) an instance of this object is created. Some some basic properties are set as well and, at last, the object is put it into the scene.

    Qt Code:
    1. Sim_Target *simulationTarget = new Sim_Target;
    2. simulationTarget->setPos(30,40);
    3. // Tooltip
    4. QString str_target_tooltip;
    5. str_target_tooltip = "This is the target";
    6. simulationTarget->setToolTip(str_target_tooltip);
    7. // Flags
    8. QGraphicsItem::GraphicsItemFlags a;
    9. a = QGraphicsItem::ItemIsSelectable;
    10. a |= QGraphicsItem::ItemIsMovable;
    11. simulationTarget->setFlags(a);
    12. // Insert into the scene
    13. graphScene_Scene->addItem(simulationTarget);
    To copy to clipboard, switch view to plain text mode 

    No problems until here. I'm able to see the object inside the scene. What I'd like to do is that when the user clicks on the item, the statusbar will answer with a message. In order to reach this "goal", I connected the QGraphicsScene's selectionChanged SIGNAL to a SLOT function.

    Qt Code:
    1. QObject::connect(graphScene_Scene, SIGNAL(selectionChanged()), this, SLOT(item_selection()));
    To copy to clipboard, switch view to plain text mode 

    The SLOT function I wrote, first of all clears the statusbar from any previous message written on it, then it performs (or, better, it tries to do that) the operation required:

    Qt Code:
    1. // Clear the statusbar
    2. statusbar->clearMessage();
    3.  
    4. if (simulationTarget->isSelected()) {
    5. statusbar->showMessage("MAV: 1");
    6. }
    To copy to clipboard, switch view to plain text mode 

    The problem is that, when I run the program and I select with the mouse's left button an object, I obtain an error which provoke the exiting from the program. I'm not sure about the kind of error I obtain (the Xcode's integrated debugger doesn't tell me that exactly), but sometimes I've found the EXC_BAD_ACCESS message in the debugger console. I made
    some tests and I saw that I obtain the same result when I use the setSelected function.

    Maybe the problem related to the fact that I don't create the object inside the main window constructor?

    I'm not new of C++, but this is the first time I write a program object-oriented and, overall, this is the first time I use Qt. So, any kind of help would be really appreciated! :-)

    Thanks in advance,
    Fabio

    UPDATE: I believe the problem is related to passing the object I'm using to the SLOT function. Every operations I do on this object will results in an application fail. Anyway, I cannot figure out where I'm wrong... :-(
    Last edited by fabietto; 13th June 2007 at 17:24. Reason: updated contents

  2. #2
    Join Date
    Jan 2006
    Location
    Norway
    Posts
    124
    Thanked 38 Times in 30 Posts
    Qt products
    Qt3 Qt4 Qt/Embedded
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: Troubles with QGraphicsItem::isSelected/setSelected

    You need to start with posting a compilable example, or a link to one. :-)
    Bitto / Andreas Aardal Hanssen - andreas dot aardal dot hanssen at nokia
    Nokia Software Manager, Qt Development

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

    Default Re: Troubles with QGraphicsItem::isSelected/setSelected

    I think your slot may be called when the item is not yet created. How about doing it a different way - override the mouseReleaseEvent in the item and emit a signal from there (remember to call the base class implementation if you want the item to remain movable). You can then connect to that signal directly.

  4. #4
    Join Date
    Jun 2007
    Location
    Plymouth, UK
    Posts
    36
    Thanks
    6
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: Troubles with QGraphicsItem::isSelected/setSelected

    Bitto and Wysota,

    first of all, thank you very much for your very quick response.

    I'm not able to post a larger portion of my code here (it exceeds the 10000 characters limits), so I uploaded a zip file containing all the sources and the headers here (please consider that the "mouse" class I'm using is the one provided with the Qt's "collidingmice" example).

    I tried to follow the Wysota's tips, moving the code implementing the SIGNAL/SLOT connection after the object's item creation, but the program still doesn't works. In fact, I'm unable to access the object I created in a slot function from inside another slot function. Since I'm not so expert in OOP, I'm worry about the fact I wrong something when I create my objects.

    They're all declared as members of the Ui_MainWindow class and then created inside the void Ui_MainWindow::start_simulation() function. I also tried to create them inside the "constructor" (the void Ui_MainWindow::setupUi(QMainWindow *MainWindow function), as the other objects belonging to the main window, but in this way they aren't recognised also in the Ui_MainWindow::start_simulation() function.

    In the meanwhile, I try the other suggests provided by Wisota. Thank you very much again for your attention!

    Fabio
    Last edited by fabietto; 13th June 2007 at 18:44. Reason: spelling error

  5. #5
    Join Date
    Aug 2006
    Location
    Bangalore,India
    Posts
    419
    Thanks
    37
    Thanked 53 Times in 40 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11

    Default Re: Troubles with QGraphicsItem::isSelected/setSelected

    There was a bug in your code actually which is c++ related.
    In mainWindow.cpp: void Ui_MainWindow::start_simulation() function you assign the newly create objects to a local pointer not the class members.
    Change your code as follows
    Qt Code:
    1. //Mouse *MAV1 = new Mouse(1);
    2. //Mouse *MAV2 = new Mouse(2);
    3. //Mouse *MAV3 = new Mouse(3);
    4. //Mouse *MAV4 = new Mouse(4);
    5.  
    6. MAV1 = new Mouse(1);
    7. MAV2 = new Mouse(2);
    8. MAV3 = new Mouse(3);
    9. MAV4 = new Mouse(4);
    To copy to clipboard, switch view to plain text mode 

    It should work now!
    The biggest difference between time and space is that you can't reuse time.
    -- Merrick Furst

  6. The following user says thank you to Gopala Krishna for this useful post:

    fabietto (13th June 2007)

  7. #6
    Join Date
    Jun 2007
    Location
    Plymouth, UK
    Posts
    36
    Thanks
    6
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: Troubles with QGraphicsItem::isSelected/setSelected

    Quote Originally Posted by Gopala Krishna View Post
    There was a bug in your code actually which is c++ related.
    In mainWindow.cpp: void Ui_MainWindow::start_simulation() function you assign the newly create objects to a local pointer not the class members.
    Change your code as follows
    [snip]
    You're absolutely right!

    The only thing I can say to you is a very big thanks!!!! :-)

    Fabio

  8. #7
    Join Date
    May 2007
    Posts
    91
    Thanks
    60
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Troubles with QGraphicsItem::isSelected/setSelected

    fabietto, I met a similiar problem as you.
    I am going to catch a signal like (itemselected) kind. I don't think there is such signals in QGraphicsView Framework
    Anyway, since you've solve this problem, I wana see your code, but it's not there. Would you please sent your code by email?
    godspeed1984@gmail.com

Similar Threads

  1. makefile troubles
    By Walsi in forum Qt Programming
    Replies: 6
    Last Post: 12th April 2007, 15:12
  2. libqjpeg troubles
    By TheRonin in forum Installation and Deployment
    Replies: 6
    Last Post: 25th August 2006, 15:48
  3. lupdate *.pro troubles
    By jeff_s in forum Qt Programming
    Replies: 1
    Last Post: 28th July 2006, 10:07
  4. QFtp, QUrlInfo troubles
    By IPFreely in forum Qt Programming
    Replies: 2
    Last Post: 9th May 2006, 11:49
  5. QTextEdit troubles?!
    By Jojo in forum Qt Programming
    Replies: 2
    Last Post: 21st February 2006, 16:54

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.