Results 1 to 7 of 7

Thread: Troubles with QGraphicsItem::isSelected/setSelected

Threaded 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

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.