1. 通過(guò)QGraphicsScene()和QGraphicsView ()載入圖像
ui界面
在*.ui界面設(shè)計(jì)文件中插入 Display Widget -> Graphics View 控件。
包含頭文件:
#include <QGraphicsScene>
#include? <QGraphicsView>
#include <QFileDialog>
.h文件中定義變量:
QGraphicsScene *graph=new QGraphicsScene();
QPixmap map;
.cpp文件中函數(shù):
主窗口函數(shù)中添加??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
}
載入圖像函數(shù)如下
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. 通過(guò)QImage()操作graph像素
note: qt中只有QImage數(shù)據(jù)類型可以直接訪問(wèn)像素(貌似)爬坑。
.cpp文件中函數(shù):
QImage mimg = map.toImage();
mimg.pixel(int x, int y) 返回像素點(diǎn)(x,y)的RGB值
qGray(mimg.pixel(int x, int y)) 返回像素點(diǎn)(x,y)的灰度值