Results 1 to 2 of 2

Thread: QGraphicsScene doesn't respond to mousePressEvent

  1. #1
    Join Date
    Jan 2010
    Posts
    15
    Thanks
    2
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Question QGraphicsScene doesn't respond to mousePressEvent

    Hey there,

    I've been looking around and at the example but just can't figure out why my code won't accept the mousePressEvent. It would be great if someone could please have a look at it.

    MainWindow.cpp
    Qt Code:
    1. #include <QGraphicsTextItem>
    2. #include <QGraphicsSceneMouseEvent>
    3.  
    4. #include "mainwindow.h"
    5. #include "ui_mainwindow.h"
    6.  
    7. MainWindow::MainWindow(QWidget *parent) :QMainWindow(parent),ui(new Ui::MainWindow)
    8. {
    9. ui->setupUi(this);
    10. setupCalendar();
    11. }
    12.  
    13. MainWindow::~MainWindow()
    14. {
    15. delete ui;
    16. }
    17.  
    18. void MainWindow::changeEvent(QEvent *e)
    19. {
    20. QMainWindow::changeEvent(e);
    21. switch (e->type()) {
    22. case QEvent::LanguageChange:
    23. ui->retranslateUi(this);
    24. break;
    25. default:
    26. break;
    27. }
    28. }
    29.  
    30. void MainWindow::setupCalendar(){
    31. ui->CalendarView->setScene(&calendarScene);
    32. ui->CalendarView->move(0,0);
    33. ui->CalendarView->show();
    34.  
    35. initializeCalendarVariables();
    36. drawGrid();
    37. }
    38.  
    39. void MainWindow::initializeCalendarVariables(){
    40. columns = 3;
    41.  
    42. interval = 15; // Minutes
    43. boxHeight = 19;
    44. dayStart = 9;
    45. dayEnd = 17;
    46.  
    47. totalDayLength = dayEnd - dayStart;
    48. horizontalLines = totalDayLength * (60 / interval) + 2; // 1 for the Heading and one for OB1
    49. leftMargin = 35;
    50. topMargin = 20;
    51. columnWidth = 200;
    52. totalWidth = columnWidth *columns; //left to right
    53. totalHeight = boxHeight * horizontalLines; // total height
    54. }
    55.  
    56. void MainWindow::drawGrid(){
    57.  
    58. QPen gridLinePen(Qt::black, 1, Qt::SolidLine, Qt::RoundCap, Qt::RoundJoin);
    59.  
    60. //Horizontal Lines
    61. for(int ii=0; ii <= horizontalLines; ii++){
    62. calendarScene.addLine(leftMargin,ii*boxHeight,leftMargin+totalWidth,ii*boxHeight,gridLinePen);
    63. }
    64.  
    65. //Vertical Lines
    66. for(int jj=0; jj <= columns; jj ++){
    67. calendarScene.addLine(jj * columnWidth + leftMargin,0,jj*columnWidth + leftMargin,totalHeight,gridLinePen);
    68. }
    69. }
    70.  
    71. void MainWindow::mousePressEvent(QGraphicsSceneMouseEvent *event)
    72. {
    73. QApplication::setOverrideCursor(QCursor(Qt::WaitCursor)); //Just for testing
    74. }
    To copy to clipboard, switch view to plain text mode 

    MainWindow.h
    Qt Code:
    1. #ifndef MAINWINDOW_H
    2. #define MAINWINDOW_H
    3.  
    4. #include <QMainWindow>
    5. #include <QGraphicsScene>
    6. #include <QGraphicsSceneMouseEvent>
    7.  
    8. namespace Ui {
    9. class MainWindow;
    10. }
    11.  
    12. class MainWindow : public QMainWindow {
    13. Q_OBJECT
    14. public:
    15. MainWindow(QWidget *parent = 0);
    16. ~MainWindow();
    17.  
    18. protected:
    19. void changeEvent(QEvent *e);
    20. void mousePressEvent(QGraphicsSceneMouseEvent *);
    21.  
    22. private:
    23. void initializeCalendarVariables();
    24. void setupCalendar();
    25.  
    26. Ui::MainWindow *ui;
    27. QGraphicsScene calendarScene;
    28.  
    29. //Calendar
    30. void drawGrid();
    31. int columns;
    32.  
    33. int interval;
    34. int boxHeight;
    35. int horizontalLines;
    36. int leftMargin;
    37. int topMargin;
    38. int columnWidth;
    39. int totalWidth; //left to right
    40. int totalHeight; // total height
    41. int dayStart;
    42. int dayEnd;
    43. int totalDayLength;
    44.  
    45. };
    46.  
    47. #endif // MAINWINDOW_H
    To copy to clipboard, switch view to plain text mode 

    Any ideas? I'm stomped. Thanks a lot already.
    Alex

  2. #2
    Join Date
    Jan 2010
    Posts
    15
    Thanks
    2
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QGraphicsScene doesn't respond to mousePressEvent


Similar Threads

  1. How to respond the reallty button?
    By anson97209 in forum Qt Programming
    Replies: 2
    Last Post: 18th October 2009, 20:04
  2. How to respond the reallty button?
    By anson97209 in forum Qt for Embedded and Mobile
    Replies: 1
    Last Post: 15th October 2009, 12:08
  3. respond to up dn keys tableview
    By rogerholmes in forum Newbie
    Replies: 1
    Last Post: 28th March 2009, 07:05
  4. QFileInfo for directory does not respond to 'touch'
    By kghose in forum Qt Programming
    Replies: 2
    Last Post: 28th July 2008, 15:09
  5. mousePressEvent
    By mickey in forum Newbie
    Replies: 3
    Last Post: 21st March 2006, 15:36

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.