#include <QApplication>
#include <QGraphicsScene>
#include <QGraphicsView>
#include <QTimer>
{
Q_OBJECT
public:
{
_graphicsScene->setBackgroundBrush(Qt::blue);
// resize(800, 800);
setScene(_graphicsScene);
QPen blackPen
(Qt
::black,
6);
_rect = _graphicsScene->addRect(0, 0, 100, 100, blackPen, redBrush);
_graphicsScene
->addLine
( 0,
1000,
0,
-1000,
QPen(Qt
::white));
_graphicsScene
->addLine
(1000,
0,
-1000,
0,
QPen(Qt
::white));
// Make the square move 3 seconds from now
QTimer::singleShot(3000,
this,
&MyView
::moveItem);
}
private slots:
void moveItem() { _rect->moveBy(100, 100); }
};
int main(int argc, char *argv[])
{
MyView view;
view.show();
return a.exec();
}
#include "main.moc"
#include <QApplication>
#include <QGraphicsScene>
#include <QGraphicsView>
#include <QTimer>
class MyView: public QGraphicsView
{
Q_OBJECT
QGraphicsScene *_graphicsScene;
QGraphicsRectItem *_rect;
public:
explicit MyView(QWidget *p = nullptr): QGraphicsView(p), _graphicsScene(nullptr), _rect(nullptr)
{
_graphicsScene = new QGraphicsScene(0, 0, 500, 500, this);
_graphicsScene->setBackgroundBrush(Qt::blue);
// resize(800, 800);
setScene(_graphicsScene);
QBrush redBrush(Qt::red);
QPen blackPen(Qt::black, 6);
_rect = _graphicsScene->addRect(0, 0, 100, 100, blackPen, redBrush);
_graphicsScene->addLine( 0, 1000, 0, -1000, QPen(Qt::white));
_graphicsScene->addLine(1000, 0, -1000, 0, QPen(Qt::white));
// Make the square move 3 seconds from now
QTimer::singleShot(3000, this, &MyView::moveItem);
}
private slots:
void moveItem() { _rect->moveBy(100, 100); }
};
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
MyView view;
view.show();
return a.exec();
}
#include "main.moc"
To copy to clipboard, switch view to plain text mode
Uncomment line 17 to see how the initial view changes for the same scene.
Bookmarks