PDA

View Full Version : Troubles with QGraphicsItem::isSelected/setSelected



fabietto
13th June 2007, 16:31
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>

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

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


Sim_Target *simulationTarget;

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
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);

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()));

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");
}

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... :-(

Bitto
13th June 2007, 17:59
You need to start with posting a compilable example, or a link to one. :-)

wysota
13th June 2007, 18:00
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.

fabietto
13th June 2007, 18:43
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 (http://www.fabioruini.eu/Plymouth/backup_13062007/Archive.zip) (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

Gopala Krishna
13th June 2007, 20:41
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


//Mouse *MAV1 = new Mouse(1);
//Mouse *MAV2 = new Mouse(2);
//Mouse *MAV3 = new Mouse(3);
//Mouse *MAV4 = new Mouse(4);

MAV1 = new Mouse(1);
MAV2 = new Mouse(2);
MAV3 = new Mouse(3);
MAV4 = new Mouse(4);


It should work now! :)

fabietto
13th June 2007, 22:20
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

Shawn
28th July 2007, 03:48
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