PDA

View Full Version : [SOLVED] QT and C++ - Access Existing Widget from Event Handler



BnMcG
16th December 2012, 11:28
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:



namespace Ui {
class MapEditor;
}

// Custom implementation of the QGraphicsView widget
// Woo! Event handlers!
class MapViewer : public QGraphicsView
{
Q_OBJECT

public:
explicit MapViewer(QGraphicsScene *scene, QWidget *parent = 0);
~MapViewer();

public slots:
void mousePressEvent(QMouseEvent *event);

};

// Declaration for the map editor window.
class MapEditor : public QMainWindow
{
Q_OBJECT

public:
explicit MapEditor(QWidget *parent = 0);
~MapEditor();

public:
QLayout *editorLayout;
QPushButton *btn;
QGraphicsScene *mapScene;
MapViewer *mapView;

private:
Ui::MapEditor *ui;
};


CPP file:



MapViewer::MapViewer(QGraphicsScene *scene, QWidget *parent) :
QGraphicsView(scene,parent)
{

}

MapEditor::MapEditor(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MapEditor)
{

ui->setupUi(this);
this->setWindowTitle("2DXY :: Map Editor");
this->setGeometry(10,10,1170,750);
editorLayout = new QVBoxLayout; // Create a new layout
this->setLayout(editorLayout); // Set the widget's layout to our newly created layout.

mapScene = new QGraphicsScene(); // Create a new graphics scene to draw upon.

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.
mapView->setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOf f);
mapView->setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOff) ;
mapView->setGeometry(20,20,1178,546); // Width first, then height.

// Now we're going to draw a grid for our map editor
int x = 0; // X draw position
int y = 0; // Y draw position

while(x < 1178)
{
mapScene->addLine(x,0,x,546,QPen(QBrush(Qt::red),1)); // Draw a vertical red line
x = x+32; // Every 32 pixels across.
}

while(y < 546)
{
mapScene->addLine(0,y,1178,y,QPen(QBrush(Qt::red),1)); // Draw a horizontal red line
y = y+32; // Every 32 pixels downwards.
}


// Test Button!
btn = new QPushButton(this);
btn->setText("Hello World!");
btn->setGeometry(980,610,100,30);

editorLayout->addWidget(btn); //Add the button to the window's layout.
editorLayout->addWidget(mapView); // Add the QGraphicsView render to the window's layout.
btn->show(); // Show the button.
mapView->show(); // Show the QGraphicsView
}

void MapViewer::mousePressEvent(QMouseEvent *event)
{
// Show an empty message box, just to check that the event handler works!
QMessageBox *notification = new QMessageBox();
notification->show();
notification->exec();

}


Any help would be greatly appreciated!

Regards,
Ben.

wysota
16th December 2012, 12:18
Where exactly is the problem?

BnMcG
16th December 2012, 17:23
Thanks for the reply, however I figured this out!

amleto
16th December 2012, 21:36
then why don't you show the solution? Then others who google for a problem and find this thread will also get some benefit!