qt调用重写GraphicsView后的类的方法
TGraphicsView *view = new TGraphicsView(this);
//ui->horizontalLayout->addWidget(view);
view->setScene(scene);
setCentralWidget(view);
TGraphicsView类继承了GraphicsView类
#include "mainwindow.h" #include "ui_mainwindow.h" #include "tgraphicsview.h" MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent) , ui(new Ui::MainWindow) { ui->setupUi(this); // QGraphicsView view(&scene); //view.show(); QGraphicsScene *scene = new QGraphicsScene(this); ui->graphicsView->setScene(scene); QPixmap image = QPixmap (":/img/qq.jpg"); // scene->addText("Hello, world!"); double scale = 1.0; double scaleX = static_cast<double>(ui->graphicsView->width()) / (image.width() + 1);// 不+1会有滚动条 double scaleY = static_cast<double>(ui->graphicsView->height()) / (image.height() + 1);// 不+1会有滚动条 if (scaleX > scaleY) { scale = scaleY; } else { scale = scaleX; } // 缩放 //image = image.scaled(image.width() * scale, image.height() * scale, Qt::KeepAspectRatio); scene->addPixmap(image); TGraphicsView *view = new TGraphicsView(this); //ui->horizontalLayout->addWidget(view); view->setScene(scene); setCentralWidget(view); // scene->clear(); // scene->addPixmap(image); // scene->update(); // this->resetTransform(); // this->setSceneRect(image.rect()); // this->fitInView(QRect(0, 0, m_image.width(), m_image.height()), Qt::KeepAspectRatio); } //缩略图测试 MainWindow::~MainWindow() { delete ui; }
欢迎讨论,相互学习。
cdtxw@foxmail.com