PDA

View Full Version : Problem with QGraphicsView & mouseRelaseEvent



BlackT
29th October 2008, 17:38
Hello. I'm a begginer, then please don't scream at me :) I'm having problems with my simple application. mousePressEvent works fine (even in QgraphisView), but mouseRelaseEvent doesn't work in one. Please help. I'm using QT 4.4.3

This is part of my code:


BlackCat::BlackCat(QWidget *parent) : QWidget (parent)
{
(...)
area1 = new QGraphicsScene;
image1 = new QGraphicsView;
image1->setFixedSize(200, 120);
image1->setMouseTracking(true);
image1->setInteractive (true);
image1->setAcceptDrops(true);
image1->setDragMode(QGraphicsView::NoDrag);
(...)
image1->setScene(area1);
image1->show();
(...)
// x & y drag's position
selectedx = new QLineEdit();
selectedy = new QLineEdit();

// width & height drag's position
selectedw = new QLineEdit();
selectedh = new QLineEdit();
(...)
}
(...)

void BlackCat::mousePressEvent(QMouseEvent *event)
{
QPoint pozycja = event->pos();
QPoint posSceny = BlackCat::image1->mapFromScene(pozycja);
int posx = posSceny.x();
int posy = posSceny.y();
QString num1, num2;
num1.setNum(posx);
num2.setNum(posy);

this->selectedx->setText(num1);
this->selectedy->setText(num2);

}

void BlackCat::mouseReleaseEvent(QMouseEvent *event)
{
QPoint pozycja = event->pos();
QPoint posSceny = BlackCat::image1->mapFromScene(pozycja);
int posx = pozycja.x();
int posy = pozycja.y();
QString num1, num2;
num1.setNum(posx);
num2.setNum(posy);


this->selectedw->setText(num1);
this->selectedh->setText(num2);
}

wysota
29th October 2008, 17:56
What do you mean by "doesn't work"? Is it called? It's probable your graphics view eats the event as it will get before the widget it is embedded into.

pastor
29th October 2008, 17:57
Hello!

Can you post full code of BlackCat's ctor?

BlackT
29th October 2008, 18:44
mouseRelaseEvent is called only when i relase button outside QGraphicsView. That's why this one "don't work"..

and about full source code ... it's a mess.. but ok. i give it.

"blackcat.cpp"
#include <QApplication>
#include <QVBoxLayout>
#include <QString>
#include <QPushButton>
#include <QFileDialog>
#include <QGraphicsScene>
#include <QGraphicsView>
#include <QPixmap>
#include <QMouseEvent>
#include <QLabel>
#include <QLineEdit>
#include <QMessageBox>

#include "blackcat.h"

BlackCat::BlackCat(QWidget *parent) : QWidget (parent)
{
QPushButton *quit = new QPushButton(tr("Quit"));
QPushButton *open = new QPushButton(tr("Open"));

// GraphicsView data
// first image is loaded to create scene at start app
QString *file1 = new QString("../../test3.png");
QPixmap *bitmapa1 = new QPixmap;
area1 = new QGraphicsScene;
image1 = new QGraphicsView;

image1->setFixedSize(200, 120);
image1->setMouseTracking(true);
image1->setInteractive (true);
image1->setAcceptDrops(true);
image1->setDragMode(QGraphicsView::NoDrag);

bitmapa1->load(*file1);
area1->addPixmap(*bitmapa1);
image1->setScene(area1);
image1->show();

// test labels
positiontxt = new QLabel("Test of position");
scenetxt = new QLabel("Test of scene");
info = new QLabel("scene Info");

// x & y drag's position
selectedx = new QLineEdit();
selectedy = new QLineEdit();

// width & height drag's position
selectedw = new QLineEdit();
selectedh = new QLineEdit();

//connecting buttons
connect(quit, SIGNAL(clicked()), qApp, SLOT(quit()));
connect(open, SIGNAL(clicked()), this, SLOT(Open1()));

// test layout
QVBoxLayout *layout = new QVBoxLayout;
layout->addWidget(image1);
layout->addWidget(open);
layout->addWidget(quit);
layout->addWidget(positiontxt);
layout->addWidget(scenetxt);
layout->addWidget(info);
layout->addWidget(selectedx);
layout->addWidget(selectedy);
layout->addWidget(selectedw);
layout->addWidget(selectedh);
setLayout(layout);


// data to test label 3
int wscene;
wscene = (int) image1->width();
int hscene;
hscene = (int) image1->height();
QString num1, num2;
num1.setNum(wscene);
num2.setNum(hscene);
QString inf;
inf = "GraphicsScene width & height: w=" + num1 + " ,h=" + num2 ;
this->info->setText(inf);
}

void BlackCat::Open1()
{
//loading img
QString file1 = QFileDialog::getOpenFileName(this,
tr("Open file"), "../../", tr("Image files (*.png)") );

QPixmap *bitmapa1 = new QPixmap;
area1 = new QGraphicsScene(this);
bitmapa1->load(file1);

// checking img width & height -> if to small -> create biggest scene
int maxw, maxh;
if (bitmapa1->width() < image1->width())
maxw = image1->width()-2;
else
maxw = bitmapa1->width();

if (bitmapa1->height() < image1->height())
maxh = image1->height()-2;
else
maxh = bitmapa1->height();

area1->setSceneRect(0,0,maxw,maxh);

// showing image
area1->addPixmap(*bitmapa1);
image1->setScene(area1);
image1->show();

// data to test label 3
int wscene;
wscene = (int) area1->width();
int hscene;
hscene = (int) area1->height();
QString num1, num2;
num1.setNum(wscene);
num2.setNum(hscene);
QString inf;
inf = "GraphicsScene width & height: w=" + num1 + " ,h=" + num2 ;
this->info->setText(inf);
}

void BlackCat::mousePressEvent(QMouseEvent *event)
{
// get data to test label 1 - mouse position in window
QString num1, num2, info;
QPoint position = event->pos();
int posx = position.x();
int posy = position.y();
num1.setNum(posx);
num2.setNum(posy);
info = "Cursor pos in window: x=" + num1 + " ,y=" + num2 ;
this->positiontxt->setText(info);

//get data to test label 2 - mouse position in scene
QPoint posScene = BlackCat::image1->mapFromScene(position);
int posx2 = posScene.x()-BlackCat::image1->x();
int posy2 = posScene.y()-BlackCat::image1->y();
num1.setNum(posx2);
num2.setNum(posy2);
info = "Cursor pos in window: x=" + num1 + " ,y=" + num2 ;

//is cliced on image?
QString isimg;
if (BlackCat::image1->itemAt(posx2, posy2))
isimg = "yes";
else
isimg = "no";
info += "; Obrazek:"+ isimg ;
this->scenetxt->setText(info);

// filling qtextedit - x & y of mousedrag in scene
this->selectedx->setText(num1);
this->selectedy->setText(num2);
}

void BlackCat::mouseReleaseEvent(QMouseEvent *event)
{
QPoint position = event->pos();
// temporarily not needed
//QPoint posScene = BlackCat::image1->mapFromScene(position);
int posx = position.x();
int posy = position.y();
QString num1, num2;
num1.setNum(posx);
num2.setNum(posy);

// filling qtextedit - width & height of mousedrag in and out of scene
this->selectedw->setText(num1);
this->selectedh->setText(num2);
}



"blackcat.h"
#ifndef BLACKCAT_H
#define BLACKCAT_H

#include <QWidget>

class QGraphicsView;
class QGraphicsScene;
class QLabel;
class QLineEdit;

class BlackCat : public QWidget
{
Q_OBJECT

public:
BlackCat(QWidget *parent = 0);
QLabel *positiontxt;
QLabel *scenetxt;
QLabel *info;
QLineEdit *selectedx;
QLineEdit *selectedy;
QLineEdit *selectedw;
QLineEdit *selectedh;

private:
QGraphicsView *image1;
QGraphicsScene *area1;

public slots:
void Open1();

protected:
void mousePressEvent(QMouseEvent *event);
void mouseReleaseEvent(QMouseEvent *event);

};

#endif


and traditional "main.cpp"
#include <QApplication>

#include "blackcat.h"

int main(int argc, char *argv[])
{
QApplication app(argc, argv);
BlackCat widget;
widget.show();
return app.exec();
}

that's all

P.S. Code checked - compiling correctly .

wysota
29th October 2008, 18:54
mouseRelaseEvent is called only when i relase button outside QGraphicsView. That's why this one "don't work"..

If you want to handle events that occur on the graphics view, you have to handle them in the graphics view, the scene or the items themselves. If you handle the event in a parent widget, it won't get the event for its child if it handles the event itself (and that's what happens in case of the graphics view widget).

BlackT
1st November 2008, 09:13
OK. I've got it. MousePress & Relase Events working properly now. Big thx 4 help. But I've two more problems now:

1. I can't enable QRubberBand drag mode...

2. I haven't any idea to connect QGraphicsView->horizontalScrollBar() with any slot.

this is my new source code:
blackcat.cpp
#include <QVBoxLayout>
#include <QPushButton>
#include <QPixmap>
#include <QGraphicsScene>
#include <QString>
#include <QFileDialog>

#include "blackcat.h"
#include "blackview.h"


BlackCat::BlackCat(QWidget *parent) : QWidget(parent)
{
bview = new BlackView;
bview->setFixedSize(200, 120);
//RubberBandDrag not works :(
bview->setDragMode(QGraphicsView::RubberBandDrag);
bview->setCursor(Qt::CrossCursor);

QPushButton *open1 = new QPushButton(tr("Open"));


QVBoxLayout *layout = new QVBoxLayout;
layout->addWidget(bview);
layout->addWidget(open1);

setLayout(layout);

connect(open1, SIGNAL(clicked()), this, SLOT(Open1()));

// this function returs error "no maching function for call to ..." .. why ?-
//connect(bview->horizontalScrollBar(), SIGNAL(valueChanged(int)), bview, SLOT(Message()));
}

void BlackCat::Open1()
{
//loading img
QString file1 = QFileDialog::getOpenFileName(this,
"Open file", "../../", "Image files (*.png)") ;

QPixmap *bitmapa1 = new QPixmap;
QGraphicsScene *area1 = new QGraphicsScene(bview);
bitmapa1->load(file1);


// showing image
area1->addPixmap(*bitmapa1);
bview->setScene(area1);
//RubberBandDrag also not works :(
bview->setDragMode(QGraphicsView::RubberBandDrag);
bview->show();
}

blackview.cpp
#include <QGraphicsView>
#include <QVBoxLayout>
#include <QMessageBox>

#include "blackview.h"

BlackView::BlackView(QWidget *parent) : QGraphicsView(parent)
{
// in here RubberBandDrag also not works
setDragMode(QGraphicsView::RubberBandDrag);

show();

}

void BlackView::mousePressEvent(QMouseEvent *event)
{
//this->Message();

}

void BlackView::mouseReleaseEvent(QMouseEvent *event)
{
//this->Message();
}

void BlackView::Message()
{
QMessageBox msgBox;
msgBox.setText("Test OK!!");
msgBox.exec();
}

Full Source code is here -> http://bt.internetdsl.pl/blackcat02.zip


P.S. Ok, I've got RubberBandDrag in mouseEvents. Works fine. I still have a question about Horrizontal & Vertical scrollbars in GraphicsView. How i could get value from them?

P.S.2 I forgot small thing -> #include <QScrollBar> . Now everything works fine to me .

pastor
2nd November 2008, 09:44
You can get value from Horrizontal & Vertical scrollbars in GraphicsView in following way:


int hValue = graphicsView->horizontalScrollBar()->value();
int vValue = graphicsView->verticalScrollBar()->value();

maitrezeta
3rd September 2010, 16:58
Hello, I post in this old thread because I'm doing same thing that BlackT


OK. I've got it. MousePress & Relase Events working properly now. Big thx 4 help. But I've two more problems now:

1. I can't enable QRubberBand drag mode...

[...]

P.S. Ok, I've got RubberBandDrag in mouseEvents. Works fine.

I have exactly the same problem, I can't enable RubberBandDrag. How can I enable it ? My code is the same type as this BlackCat program.

Someone can explain to me please ?

wysota
3rd September 2010, 17:42
Do you call the base class implementation of mouse events from within your reimplementations?