1. 通过QGraphicsScene()和QGraphicsView ()载入图像
ui界面
在*.ui界面设计文件中插入 Display Widget -> Graphics View 控件。
包含头文件:
#include <QGraphicsScene>
#include <QGraphicsView>
#include <QFileDialog>
.h文件中定义变量:
QGraphicsScene *graph=new QGraphicsScene();
QPixmap map;
.cpp文件中函数:
主窗口函数中添加 ui->graphicsView->setScene(graph) 如下
MainWindow::MainWindow(QWidget *parent)
: QMainWindow(parent)
, ui(new Ui::MainWindow)
{
ui->setupUi(this);
ui->graphicsView->setScene(graph); // connecting the graph to the View
}
载入图像函数如下
void MainWindow::on_load_clicked()
{
QString filename=QFileDialog::getOpenFileName(this,tr("Load Note"),tr("*.png")); // Allows the usr to choose an input file
map = QPixmap(filename); // Saves the file in a a QPixmap object
graph->addPixmap(map); // Adds the pixmap to the scene
}
2. 通过QImage()操作graph像素
note: qt中只有QImage数据类型可以直接访问像素(貌似)。
.cpp文件中函数:
QImage mimg = map.toImage();
mimg.pixel(int x, int y) 返回像素点(x,y)的RGB值
qGray(mimg.pixel(int x, int y)) 返回像素点(x,y)的灰度值