工程文件 test.pro :
TEMPLATE = app
QT += widgets
SOURCES += main.cppmain.cpp :
#include <QApplication>
#include <QGraphicsScene>
#include <QGraphicsView>
#include <QGraphicsItem>
int main(int argc, char** argv)
{
    QApplication app(argc, argv);
    QGraphicsScene scene;
    QGraphicsRectItem item(0, 0, 80, 80);
    scene.addItem(&item);
    QGraphicsView view;
    view.setScene(&scene);
    view.show();
    app.exec();
    return 0;
}
离线
main.cpp 添加按键处理槽函数进行缩放:
#include <QApplication>
#include <QGraphicsScene>
#include <QGraphicsView>
#include <QGraphicsItem>
#include <QPushButton>
#include <QDebug>
QGraphicsView *view;
void view_test()
{
    view->scale(1.2f, 1.2f);
}
int main(int argc, char** argv)
{
    QApplication app(argc, argv);
    QGraphicsScene scene;
    scene.setBackgroundBrush(Qt::gray);
    QGraphicsRectItem item(0, 0, 80, 80);
    scene.addItem(&item);
    QPushButton button;
    scene.addWidget(&button);
    QObject::connect(&button, &QPushButton::clicked, view_test);
    view = new QGraphicsView();
    view->setScene(&scene);
    view->show();
    app.exec();
    return 0;
}
Qt5槽函数花样比Qt4丰富:
https://wiki.qt.io/New_Signal_Slot_Syntax
离线