Results 1 to 9 of 9

Thread: Problem with QGraphicsView & mouseRelaseEvent

  1. #1
    Join Date
    Oct 2008
    Posts
    3
    Qt products
    Qt4
    Platforms
    Windows

    Default Problem with QGraphicsView & mouseRelaseEvent

    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:

    Qt Code:
    1. BlackCat::BlackCat(QWidget *parent) : QWidget (parent)
    2. {
    3. (...)
    4. area1 = new QGraphicsScene;
    5. image1 = new QGraphicsView;
    6. image1->setFixedSize(200, 120);
    7. image1->setMouseTracking(true);
    8. image1->setInteractive (true);
    9. image1->setAcceptDrops(true);
    10. image1->setDragMode(QGraphicsView::NoDrag);
    11. (...)
    12. image1->setScene(area1);
    13. image1->show();
    14. (...)
    15. // x & y drag's position
    16. selectedx = new QLineEdit();
    17. selectedy = new QLineEdit();
    18.  
    19. // width & height drag's position
    20. selectedw = new QLineEdit();
    21. selectedh = new QLineEdit();
    22. (...)
    23. }
    24. (...)
    25.  
    26. void BlackCat::mousePressEvent(QMouseEvent *event)
    27. {
    28. QPoint pozycja = event->pos();
    29. QPoint posSceny = BlackCat::image1->mapFromScene(pozycja);
    30. int posx = posSceny.x();
    31. int posy = posSceny.y();
    32. QString num1, num2;
    33. num1.setNum(posx);
    34. num2.setNum(posy);
    35.  
    36. this->selectedx->setText(num1);
    37. this->selectedy->setText(num2);
    38.  
    39. }
    40.  
    41. void BlackCat::mouseReleaseEvent(QMouseEvent *event)
    42. {
    43. QPoint pozycja = event->pos();
    44. QPoint posSceny = BlackCat::image1->mapFromScene(pozycja);
    45. int posx = pozycja.x();
    46. int posy = pozycja.y();
    47. QString num1, num2;
    48. num1.setNum(posx);
    49. num2.setNum(posy);
    50.  
    51.  
    52. this->selectedw->setText(num1);
    53. this->selectedh->setText(num2);
    54. }
    To copy to clipboard, switch view to plain text mode 

  2. #2
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Problem with QGraphicsView & mouseRelaseEvent

    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.

  3. #3
    Join Date
    Oct 2008
    Posts
    70
    Thanks
    1
    Thanked 9 Times in 9 Posts

    Default Re: Problem with QGraphicsView & mouseRelaseEvent

    Hello!

    Can you post full code of BlackCat's ctor?

  4. #4
    Join Date
    Oct 2008
    Posts
    3
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Problem with QGraphicsView & mouseRelaseEvent

    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"
    Qt Code:
    1. #include <QApplication>
    2. #include <QVBoxLayout>
    3. #include <QString>
    4. #include <QPushButton>
    5. #include <QFileDialog>
    6. #include <QGraphicsScene>
    7. #include <QGraphicsView>
    8. #include <QPixmap>
    9. #include <QMouseEvent>
    10. #include <QLabel>
    11. #include <QLineEdit>
    12. #include <QMessageBox>
    13.  
    14. #include "blackcat.h"
    15.  
    16. BlackCat::BlackCat(QWidget *parent) : QWidget (parent)
    17. {
    18. QPushButton *quit = new QPushButton(tr("Quit"));
    19. QPushButton *open = new QPushButton(tr("Open"));
    20.  
    21. // GraphicsView data
    22. // first image is loaded to create scene at start app
    23. QString *file1 = new QString("../../test3.png");
    24. QPixmap *bitmapa1 = new QPixmap;
    25. area1 = new QGraphicsScene;
    26. image1 = new QGraphicsView;
    27.  
    28. image1->setFixedSize(200, 120);
    29. image1->setMouseTracking(true);
    30. image1->setInteractive (true);
    31. image1->setAcceptDrops(true);
    32. image1->setDragMode(QGraphicsView::NoDrag);
    33.  
    34. bitmapa1->load(*file1);
    35. area1->addPixmap(*bitmapa1);
    36. image1->setScene(area1);
    37. image1->show();
    38.  
    39. // test labels
    40. positiontxt = new QLabel("Test of position");
    41. scenetxt = new QLabel("Test of scene");
    42. info = new QLabel("scene Info");
    43.  
    44. // x & y drag's position
    45. selectedx = new QLineEdit();
    46. selectedy = new QLineEdit();
    47.  
    48. // width & height drag's position
    49. selectedw = new QLineEdit();
    50. selectedh = new QLineEdit();
    51.  
    52. //connecting buttons
    53. connect(quit, SIGNAL(clicked()), qApp, SLOT(quit()));
    54. connect(open, SIGNAL(clicked()), this, SLOT(Open1()));
    55.  
    56. // test layout
    57. QVBoxLayout *layout = new QVBoxLayout;
    58. layout->addWidget(image1);
    59. layout->addWidget(open);
    60. layout->addWidget(quit);
    61. layout->addWidget(positiontxt);
    62. layout->addWidget(scenetxt);
    63. layout->addWidget(info);
    64. layout->addWidget(selectedx);
    65. layout->addWidget(selectedy);
    66. layout->addWidget(selectedw);
    67. layout->addWidget(selectedh);
    68. setLayout(layout);
    69.  
    70.  
    71. // data to test label 3
    72. int wscene;
    73. wscene = (int) image1->width();
    74. int hscene;
    75. hscene = (int) image1->height();
    76. QString num1, num2;
    77. num1.setNum(wscene);
    78. num2.setNum(hscene);
    79. QString inf;
    80. inf = "GraphicsScene width & height: w=" + num1 + " ,h=" + num2 ;
    81. this->info->setText(inf);
    82. }
    83.  
    84. void BlackCat::Open1()
    85. {
    86. //loading img
    87. QString file1 = QFileDialog::getOpenFileName(this,
    88. tr("Open file"), "../../", tr("Image files (*.png)") );
    89.  
    90. QPixmap *bitmapa1 = new QPixmap;
    91. area1 = new QGraphicsScene(this);
    92. bitmapa1->load(file1);
    93.  
    94. // checking img width & height -> if to small -> create biggest scene
    95. int maxw, maxh;
    96. if (bitmapa1->width() < image1->width())
    97. maxw = image1->width()-2;
    98. else
    99. maxw = bitmapa1->width();
    100.  
    101. if (bitmapa1->height() < image1->height())
    102. maxh = image1->height()-2;
    103. else
    104. maxh = bitmapa1->height();
    105.  
    106. area1->setSceneRect(0,0,maxw,maxh);
    107.  
    108. // showing image
    109. area1->addPixmap(*bitmapa1);
    110. image1->setScene(area1);
    111. image1->show();
    112.  
    113. // data to test label 3
    114. int wscene;
    115. wscene = (int) area1->width();
    116. int hscene;
    117. hscene = (int) area1->height();
    118. QString num1, num2;
    119. num1.setNum(wscene);
    120. num2.setNum(hscene);
    121. QString inf;
    122. inf = "GraphicsScene width & height: w=" + num1 + " ,h=" + num2 ;
    123. this->info->setText(inf);
    124. }
    125.  
    126. void BlackCat::mousePressEvent(QMouseEvent *event)
    127. {
    128. // get data to test label 1 - mouse position in window
    129. QString num1, num2, info;
    130. QPoint position = event->pos();
    131. int posx = position.x();
    132. int posy = position.y();
    133. num1.setNum(posx);
    134. num2.setNum(posy);
    135. info = "Cursor pos in window: x=" + num1 + " ,y=" + num2 ;
    136. this->positiontxt->setText(info);
    137.  
    138. //get data to test label 2 - mouse position in scene
    139. QPoint posScene = BlackCat::image1->mapFromScene(position);
    140. int posx2 = posScene.x()-BlackCat::image1->x();
    141. int posy2 = posScene.y()-BlackCat::image1->y();
    142. num1.setNum(posx2);
    143. num2.setNum(posy2);
    144. info = "Cursor pos in window: x=" + num1 + " ,y=" + num2 ;
    145.  
    146. //is cliced on image?
    147. QString isimg;
    148. if (BlackCat::image1->itemAt(posx2, posy2))
    149. isimg = "yes";
    150. else
    151. isimg = "no";
    152. info += "; Obrazek:"+ isimg ;
    153. this->scenetxt->setText(info);
    154.  
    155. // filling qtextedit - x & y of mousedrag in scene
    156. this->selectedx->setText(num1);
    157. this->selectedy->setText(num2);
    158. }
    159.  
    160. void BlackCat::mouseReleaseEvent(QMouseEvent *event)
    161. {
    162. QPoint position = event->pos();
    163. // temporarily not needed
    164. //QPoint posScene = BlackCat::image1->mapFromScene(position);
    165. int posx = position.x();
    166. int posy = position.y();
    167. QString num1, num2;
    168. num1.setNum(posx);
    169. num2.setNum(posy);
    170.  
    171. // filling qtextedit - width & height of mousedrag in and out of scene
    172. this->selectedw->setText(num1);
    173. this->selectedh->setText(num2);
    174. }
    To copy to clipboard, switch view to plain text mode 

    "blackcat.h"
    Qt Code:
    1. #ifndef BLACKCAT_H
    2. #define BLACKCAT_H
    3.  
    4. #include <QWidget>
    5.  
    6. class QLabel;
    7. class QLineEdit;
    8.  
    9. class BlackCat : public QWidget
    10. {
    11. Q_OBJECT
    12.  
    13. public:
    14. BlackCat(QWidget *parent = 0);
    15. QLabel *positiontxt;
    16. QLabel *scenetxt;
    17. QLabel *info;
    18. QLineEdit *selectedx;
    19. QLineEdit *selectedy;
    20. QLineEdit *selectedw;
    21. QLineEdit *selectedh;
    22.  
    23. private:
    24. QGraphicsView *image1;
    25.  
    26. public slots:
    27. void Open1();
    28.  
    29. protected:
    30. void mousePressEvent(QMouseEvent *event);
    31. void mouseReleaseEvent(QMouseEvent *event);
    32.  
    33. };
    34.  
    35. #endif
    To copy to clipboard, switch view to plain text mode 

    and traditional "main.cpp"
    Qt Code:
    1. #include <QApplication>
    2.  
    3. #include "blackcat.h"
    4.  
    5. int main(int argc, char *argv[])
    6. {
    7. QApplication app(argc, argv);
    8. BlackCat widget;
    9. widget.show();
    10. return app.exec();
    11. }
    To copy to clipboard, switch view to plain text mode 

    that's all

    P.S. Code checked - compiling correctly .

  5. #5
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Problem with QGraphicsView & mouseRelaseEvent

    Quote Originally Posted by BlackT View Post
    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).

  6. #6
    Join Date
    Oct 2008
    Posts
    3
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Problem with QGraphicsView & mouseRelaseEvent

    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
    Qt Code:
    1. #include <QVBoxLayout>
    2. #include <QPushButton>
    3. #include <QPixmap>
    4. #include <QGraphicsScene>
    5. #include <QString>
    6. #include <QFileDialog>
    7.  
    8. #include "blackcat.h"
    9. #include "blackview.h"
    10.  
    11.  
    12. BlackCat::BlackCat(QWidget *parent) : QWidget(parent)
    13. {
    14. bview = new BlackView;
    15. bview->setFixedSize(200, 120);
    16. //RubberBandDrag not works :(
    17. bview->setDragMode(QGraphicsView::RubberBandDrag);
    18. bview->setCursor(Qt::CrossCursor);
    19.  
    20. QPushButton *open1 = new QPushButton(tr("Open"));
    21.  
    22.  
    23. QVBoxLayout *layout = new QVBoxLayout;
    24. layout->addWidget(bview);
    25. layout->addWidget(open1);
    26.  
    27. setLayout(layout);
    28.  
    29. connect(open1, SIGNAL(clicked()), this, SLOT(Open1()));
    30.  
    31. // this function returs error "no maching function for call to ..." .. why ?-
    32. //connect(bview->horizontalScrollBar(), SIGNAL(valueChanged(int)), bview, SLOT(Message()));
    33. }
    34.  
    35. void BlackCat::Open1()
    36. {
    37. //loading img
    38. QString file1 = QFileDialog::getOpenFileName(this,
    39. "Open file", "../../", "Image files (*.png)") ;
    40.  
    41. QPixmap *bitmapa1 = new QPixmap;
    42. QGraphicsScene *area1 = new QGraphicsScene(bview);
    43. bitmapa1->load(file1);
    44.  
    45.  
    46. // showing image
    47. area1->addPixmap(*bitmapa1);
    48. bview->setScene(area1);
    49. //RubberBandDrag also not works :(
    50. bview->setDragMode(QGraphicsView::RubberBandDrag);
    51. bview->show();
    52. }
    To copy to clipboard, switch view to plain text mode 

    blackview.cpp
    Qt Code:
    1. #include <QGraphicsView>
    2. #include <QVBoxLayout>
    3. #include <QMessageBox>
    4.  
    5. #include "blackview.h"
    6.  
    7. BlackView::BlackView(QWidget *parent) : QGraphicsView(parent)
    8. {
    9. // in here RubberBandDrag also not works
    10. setDragMode(QGraphicsView::RubberBandDrag);
    11.  
    12. show();
    13.  
    14. }
    15.  
    16. void BlackView::mousePressEvent(QMouseEvent *event)
    17. {
    18. //this->Message();
    19.  
    20. }
    21.  
    22. void BlackView::mouseReleaseEvent(QMouseEvent *event)
    23. {
    24. //this->Message();
    25. }
    26.  
    27. void BlackView::Message()
    28. {
    29. QMessageBox msgBox;
    30. msgBox.setText("Test OK!!");
    31. msgBox.exec();
    32. }
    To copy to clipboard, switch view to plain text mode 

    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 .
    Last edited by BlackT; 1st November 2008 at 12:00.

  7. #7
    Join Date
    Oct 2008
    Posts
    70
    Thanks
    1
    Thanked 9 Times in 9 Posts

    Default Re: Problem with QGraphicsView & mouseRelaseEvent

    You can get value from Horrizontal & Vertical scrollbars in GraphicsView in following way:

    Qt Code:
    1. int hValue = graphicsView->horizontalScrollBar()->value();
    2. int vValue = graphicsView->verticalScrollBar()->value();
    To copy to clipboard, switch view to plain text mode 

  8. #8
    Join Date
    Sep 2010
    Posts
    4
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: Problem with QGraphicsView & mouseRelaseEvent

    Hello, I post in this old thread because I'm doing same thing that BlackT

    Quote Originally Posted by BlackT View Post
    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 ?

  9. #9
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Problem with QGraphicsView & mouseRelaseEvent

    Do you call the base class implementation of mouse events from within your reimplementations?
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


Similar Threads

  1. Replies: 2
    Last Post: 26th February 2009, 10:12
  2. Problem with QGraphicsView and ItemIgnoresTransformations
    By coderCPP1981 in forum Qt Programming
    Replies: 6
    Last Post: 27th June 2008, 05:28
  3. QGraphicsView Problem !!
    By Gamalof in forum Qt Programming
    Replies: 3
    Last Post: 14th June 2008, 13:55
  4. Mystic Problem (QGraphicsView + QGLWidget)
    By NoRulez in forum Qt Programming
    Replies: 2
    Last Post: 28th April 2008, 10:39
  5. Problem determining size of QGraphicsView
    By Bocki in forum Qt Programming
    Replies: 3
    Last Post: 17th February 2008, 14: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
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.