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;
};
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;
};
To copy to clipboard, switch view to plain text mode
{
}
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();
}
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::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 = 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();
}
To copy to clipboard, switch view to plain text mode
Bookmarks