Results 1 to 4 of 4

Thread: [SOLVED] QT and C++ - Access Existing Widget from Event Handler

Threaded View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Join Date
    Dec 2012
    Posts
    3
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows Android

    Question [SOLVED] QT and C++ - Access Existing Widget from Event Handler

    Hello all,

    I'm trying to use QGraphicsView, QGraphicsScene and the mousePressed listener. The graphics view & scene all work perfectly, however, what I want to do is have something (anything!) drawn on the QGraphicsScene when the QGraphicsView attached to it is clicked. The widget already exists, so I can't create a new instance of it, I want to use the existing instance. Here is my code so far:

    Header File:

    Qt Code:
    1. namespace Ui {
    2. class MapEditor;
    3. }
    4.  
    5. // Custom implementation of the QGraphicsView widget
    6. // Woo! Event handlers!
    7. class MapViewer : public QGraphicsView
    8. {
    9. Q_OBJECT
    10.  
    11. public:
    12. explicit MapViewer(QGraphicsScene *scene, QWidget *parent = 0);
    13. ~MapViewer();
    14.  
    15. public slots:
    16. void mousePressEvent(QMouseEvent *event);
    17.  
    18. };
    19.  
    20. // Declaration for the map editor window.
    21. class MapEditor : public QMainWindow
    22. {
    23. Q_OBJECT
    24.  
    25. public:
    26. explicit MapEditor(QWidget *parent = 0);
    27. ~MapEditor();
    28.  
    29. public:
    30. QLayout *editorLayout;
    31. QGraphicsScene *mapScene;
    32. MapViewer *mapView;
    33.  
    34. private:
    35. Ui::MapEditor *ui;
    36. };
    To copy to clipboard, switch view to plain text mode 

    CPP file:

    Qt Code:
    1. MapViewer::MapViewer(QGraphicsScene *scene, QWidget *parent) :
    2. QGraphicsView(scene,parent)
    3. {
    4.  
    5. }
    6.  
    7. MapEditor::MapEditor(QWidget *parent) :
    8. QMainWindow(parent),
    9. ui(new Ui::MapEditor)
    10. {
    11.  
    12. ui->setupUi(this);
    13. this->setWindowTitle("2DXY :: Map Editor");
    14. this->setGeometry(10,10,1170,750);
    15. editorLayout = new QVBoxLayout; // Create a new layout
    16. this->setLayout(editorLayout); // Set the widget's layout to our newly created layout.
    17.  
    18. mapScene = new QGraphicsScene(); // Create a new graphics scene to draw upon.
    19.  
    20. mapView = new MapViewer(mapScene,this); // Create a new graphics view to display our scene - set its parent to 'this' so that it doesn't open in a new window.
    21. mapView->setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
    22. mapView->setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
    23. mapView->setGeometry(20,20,1178,546); // Width first, then height.
    24.  
    25. // Now we're going to draw a grid for our map editor
    26. int x = 0; // X draw position
    27. int y = 0; // Y draw position
    28.  
    29. while(x < 1178)
    30. {
    31. mapScene->addLine(x,0,x,546,QPen(QBrush(Qt::red),1)); // Draw a vertical red line
    32. x = x+32; // Every 32 pixels across.
    33. }
    34.  
    35. while(y < 546)
    36. {
    37. mapScene->addLine(0,y,1178,y,QPen(QBrush(Qt::red),1)); // Draw a horizontal red line
    38. y = y+32; // Every 32 pixels downwards.
    39. }
    40.  
    41.  
    42. // Test Button!
    43. btn = new QPushButton(this);
    44. btn->setText("Hello World!");
    45. btn->setGeometry(980,610,100,30);
    46.  
    47. editorLayout->addWidget(btn); //Add the button to the window's layout.
    48. editorLayout->addWidget(mapView); // Add the QGraphicsView render to the window's layout.
    49. btn->show(); // Show the button.
    50. mapView->show(); // Show the QGraphicsView
    51. }
    52.  
    53. void MapViewer::mousePressEvent(QMouseEvent *event)
    54. {
    55. // Show an empty message box, just to check that the event handler works!
    56. QMessageBox *notification = new QMessageBox();
    57. notification->show();
    58. notification->exec();
    59.  
    60. }
    To copy to clipboard, switch view to plain text mode 

    Any help would be greatly appreciated!

    Regards,
    Ben.
    Last edited by BnMcG; 16th December 2012 at 17:23. Reason: Solved

Similar Threads

  1. Key Event handler
    By jano_alex_es in forum Newbie
    Replies: 2
    Last Post: 5th December 2009, 10:32
  2. How to write correctly an event handler
    By franco.amato in forum Qt Programming
    Replies: 10
    Last Post: 3rd December 2009, 07:52
  3. Deleting objects in their event handler
    By drhex in forum Qt Programming
    Replies: 7
    Last Post: 6th May 2009, 16:08
  4. Is it bad to delete an object during its event handler?
    By codeslicer in forum Qt Programming
    Replies: 3
    Last Post: 30th December 2008, 16:14
  5. event handler
    By mattia in forum Newbie
    Replies: 10
    Last Post: 8th November 2007, 12:54

Tags for this Thread

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.