[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:
Code:
namespace Ui {
class MapEditor;
}
// Custom implementation of the QGraphicsView widget
// Woo! Event handlers!
{
Q_OBJECT
public:
~MapViewer();
public slots:
};
// Declaration for the map editor window.
{
Q_OBJECT
public:
explicit MapEditor
(QWidget *parent
= 0);
~MapEditor();
public:
MapViewer *mapView;
private:
Ui::MapEditor *ui;
};
CPP file:
Code:
{
}
MapEditor
::MapEditor(QWidget *parent
) : ui(new Ui::MapEditor)
{
ui->setupUi(this);
this->setWindowTitle("2DXY :: Map Editor");
this->setGeometry(10,10,1170,750);
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::ScrollBarAlwaysOff);
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->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
}
{
// Show an empty message box, just to check that the event handler works!
notification->show();
notification->exec();
}
Any help would be greatly appreciated!
Regards,
Ben.
Re: QT and C++ - Access Existing Widget from Event Handler
Where exactly is the problem?
Re: QT and C++ - Access Existing Widget from Event Handler
Thanks for the reply, however I figured this out!
Re: QT and C++ - Access Existing Widget from Event Handler
then why don't you show the solution? Then others who google for a problem and find this thread will also get some benefit!