PDA

View Full Version : How to implement doubleclickevent in QGraphicsScene (Qtcreator)??



codegenie
2nd January 2011, 07:49
I am just a week older in the field of programming with Qt. I am using QtCreator as IDE.
I have worked my way around with the basic programming details in Qt but just cant find any useful information of how to implement events. Can someone Please help me. I am stuck with this for 2 days.

ok. What I want to do is make event handler for mouse double click in QgraphicsScene. I created a subclass graphicsScene which inherits from QgraphicsScene and declared the handler for it. This is all done in MainWindow.h

and, the definition is given in MainWindow.cpp. There is no error but the event handler just wont get invoked.

Here's the dummy code : In MainWindow.h

class graphicsScene : public QGraphicsScene
{
public:
void mouseDoubleClickEvent(QGraphicsSceneMouseEvent *event);
};

In MainWindow.cpp

MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
scene = new QGraphicsScene(this);
scene->setSceneRect(0,0,310,310);
}

void graphicsScene::mouseDoubleClickEvent(QGraphicsScen eMouseEvent *event)
{
QGraphicsScene::mouseDoubleClickEvent(event);
int x = event->scenePos().x();
int y = event->scenePos().x();
qDebug()<<x<<y;
}


Please Help.

pkohut
2nd January 2011, 09:11
Could you use code tags, it will make reading and referencing the code easier.
Anyway, you are making an instance of "QGraphicsScene" when instead it you need the derived class.

scene = new QGraphicscene(this);
should be

scene = new graphicsScene(this);

codegenie
2nd January 2011, 09:25
I am sorry for the code tags, next time i will surely do.
Your the best , dude.
Thanks for the help, it worked. Sorry if this post seems stupid but I cant help , I am a newbie.
:):)

pkohut
2nd January 2011, 09:27
Thanks for the help, it worked. Sorry if this post seems stupid but I cant help , I am a newbie.
:):)

NP, I'm a little over a month full time with Qt and learning the ropes myself.

codegenie
2nd January 2011, 11:55
Well, I guess I found myself another problem.
The event handler I just implemented doesn't have the scope to alter the scene or the items inside the scene. Since the scene and the items are implemented in MainWindow, i tried to inherit the graphicsScene from MainWindow. Then the scope problem is resolved but their is no output Window.
This is the Code in MainWindow.h:


class MainWindow : public QMainWindow {
...
QGraphicsRectItem *obstRect[Xgrid][Ygrid];
graphicsScene *scene;
void toggle(int ,int);
...
};
class graphicsScene : public QGraphicsScene
{
public:
void mouseDoubleClickEvent(QGraphicsSceneMouseEvent *event);
};


The Code in MainWindow.cpp is like:



MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent),
ui(new Ui::MainWindow)
{
for(int i=0;i<Xgrid;i++)
for(int j=0;j<Ygrid;j++)
{ obstRect[i][j] =new QGraphicsRectItem();
obstRect[i][j]->setBrush(Qt::white);
}

scene = new graphicsScene();
scene->setSceneRect(0,0,310,310);

for(int i=0;i<Xgrid;i++)
for(int j=0;j<Ygrid;j++)
scene->addItem(obstRect[i][j]);

ui->graphicsView->setScene(scene);
...

}

void graphicsScene::mouseDoubleClickEvent(QGraphicsScen eMouseEvent *event)
{
QGraphicsScene::mouseDoubleClickEvent(event);
...
//the part were error arise
toggle(x,y);
qDebug()<<x<<y;
...
}

void MainWindow::toggle(int x, int y)
{
...
}



please gimme some solutions. Its driving me mad. :(

pkohut
2nd January 2011, 12:40
Is the background white as well as the graphics rectangle? (ps, just noticed the default constructor for QGraphicsRectItem is used, which means it is a dot on the screen with dimensions of QRect(0, 0, 0, 0))

It's not clear, but in the double click event what are you trying to toggle? Also, more than likely "obstRect" is not needed (QGraphicsScene will manage the items for you). Just create the QGraphicsRectItem, set its brush and add to the scene. Then in the double click event, take the scene mouse position and call QGraphicsScene::items


QList<QGraphicsItem> items = items(event->scenePos());
foreach(QGraphicsItem * pItem, items)
Toggle(pItem);

If you want QGraphicsRectItem's to do something different than other graphics items then code something like so -

QList<QGraphicsItem> items = items(event->scenePos());
foreach(QGraphicsItem * pItem, items) {
if(qobject_cast<QGraphicsRectItem*>(pItem))
DoSomeThingWithTheRectItem(pItem);
else
Toggle(pItem);

codegenie
2nd January 2011, 13:37
I skipped the rest of the code b'coz i thought it was unimportant. All I wanted to ask is how can i edit the scene or item which is declared in MainWindow from the eventHandler.

Actually, I tried your code but there were some errors. But I got the idea of "what you were trying to tell me". So I checked the Manuals and saw the method itemAt() that suited my purpose of getting the item(just 1) using screen position as argument.

I found your reply very helpful and once again you saved me.:) Thanks.

Can I ask you from where did u learn Qt. Are there any tutorials online b'coz i can't find any tutorial related to Qtcreator. I need a good base in the the Qtcreator architecture. If there are any good e-books ,please refer that too.

Regards Manoj.

pkohut
2nd January 2011, 14:16
Actually, I tried your code but there were some errors.

Should have mentioned the code was untested and hadn't gone through a compiler.



Can I ask you from where did u learn Qt. Are there any tutorials online b'coz i can't find any tutorial related to Qtcreator.


Still just learning the Qt way of doing things. Found this group about a week ago and started reading past threads and try solving some of the problems presented, like yours.

Based on "High_flyers" sig, I bought the "Foundations of Qt Development" on New Years Eve. Literally, in the first 2 chapters, most of the issues I'd dealt with during the prior month of "being serious with Qt" were resolved. You got to understand though that I didn't just "get it", as there is almost 20 years of C++ and other framework baggage that I bring with me on my new Qt adventure.