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:
#ifndef SIMTARGET_H
#define SIMTARGET_H
#include <QGraphicsItem>
Q_OBJECT
public:
// Constructor
Sim_Target();
// Shape
private:
};
#endif
#ifndef SIMTARGET_H
#define SIMTARGET_H
#include <QGraphicsItem>
class Sim_Target : public QObject, public QGraphicsItem {
Q_OBJECT
public:
// Constructor
Sim_Target();
// Shape
QRectF boundingRect() const;
void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget);
private:
QColor color;
};
#endif
To copy to clipboard, switch view to plain text mode
Then I have included it between the public members of my QMainWindow class:
Sim_Target *simulationTarget;
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.
Sim_Target *simulationTarget = new Sim_Target;
simulationTarget->setPos(30,40);
// Tooltip
str_target_tooltip = "This is the target";
simulationTarget->setToolTip(str_target_tooltip);
// Flags
simulationTarget->setFlags(a);
// Insert into the scene
graphScene_Scene->addItem(simulationTarget);
Sim_Target *simulationTarget = new Sim_Target;
simulationTarget->setPos(30,40);
// Tooltip
QString str_target_tooltip;
str_target_tooltip = "This is the target";
simulationTarget->setToolTip(str_target_tooltip);
// Flags
QGraphicsItem::GraphicsItemFlags a;
a = QGraphicsItem::ItemIsSelectable;
a |= QGraphicsItem::ItemIsMovable;
simulationTarget->setFlags(a);
// Insert into the scene
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.
QObject::connect(graphScene_Scene,
SIGNAL(selectionChanged
()),
this,
SLOT(item_selection
()));
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:
// Clear the statusbar
statusbar->clearMessage();
if (simulationTarget->isSelected()) {
statusbar->showMessage("MAV: 1");
}
// Clear the statusbar
statusbar->clearMessage();
if (simulationTarget->isSelected()) {
statusbar->showMessage("MAV: 1");
}
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... :-(
Bookmarks