一、前言
上篇博客中通过重载子窗体的构造函数将主窗体的值传入到子窗体,但是在子窗体运行过程中如何才能将值动态的传入到子窗体?可以有两种办法,1、信号和槽的方式传值;2、主窗体中将传出值设置为public
本文主要实现功能为:鼠标点击主窗体的GraphicsView中的Scene时,将对应像素处的各个波段值传递给子窗体的TabelView并显示。其中子窗体的接受主窗体的值是通过2(public)的方式,简单方便。
二、主、子窗体的设计
定义子窗体类:ShowPixelData
主要实现:1 接受主窗体传入的文件名、当前点击图像的行、列号;
2 根据传入值,读取并显示pixel data;
头文件如下:
#ifndef SHOWPIXELDATA_H
#define SHOWPIXELDATA_H #include <QDialog>
#include <QStandardItemModel>
#include "hyperprocess.h" class HyperProcess; namespace Ui {
class ShowPixelData;
} class ShowPixelData : public QDialog
{
Q_OBJECT public:
ShowPixelData(QWidget *parent = );
~ShowPixelData(); private slots:
void receiveHyperToPixelDataSlot(); // 接受鼠标单击信号
void exitShowPixelDataSlot(); // signals:
void sendExit(); // private:
Ui::ShowPixelData *ui;
HyperProcess *ptr; QStandardItemModel *myPixelData; int currentRow; // 行
int currentCol; // 列
QString curFileName; // 文件名
}; #endif // SHOWPIXELDATA_H
源文件如下:
#include "ShowPixelData.h"
#include "ui_ShowPixelData.h" ShowPixelData::ShowPixelData(QWidget *parent) :
QDialog(parent),
ui(new Ui::ShowPixelData)
{
ui->setupUi(this); currentRow = -;
currentCol = -;
curFileName = "";
ptr = (HyperProcess*)parentWidget(); //获得主窗体的指针 ui->pixelInfo->setText("Pixel Info");
myPixelData = new QStandardItemModel;
myPixelData->setColumnCount();
myPixelData->setHorizontalHeaderLabels(QStringList()<<QStringLiteral("Band")<<QStringLiteral("Data"));
ui->pixelDataView->setModel(myPixelData);// 初始化
ui->pixelDataView->setEditTriggers(QAbstractItemView::NoEditTriggers);
ui->pixelDataView->setColumnWidth(,);
ui->pixelDataView->setColumnWidth(,); connect(ptr,SIGNAL(sendSignalToShowPixelData()),this,SLOT(receiveHyperToPixelDataSlot()));
connect(ui->ExitButton,SIGNAL(clicked()),this,SLOT(exitShowPixelDataSlot()));
} ShowPixelData::~ShowPixelData()
{
delete ui;
} void ShowPixelData::receiveHyperToPixelDataSlot()
{
curFileName = ptr->curFileName;
currentRow = ptr->curRow;
currentCol = ptr->curCol; if(curFileName == "")
{
QMessageBox::information(this,"Message Error","Current Scene IS NULL!");
return;
} if(currentCol == - || currentRow == -)
{
QMessageBox::information(this,"Message Error","please Select A Pixel");
return;
} cv::Mat curImg = GDALOpenCV::GDAL2Mat(curFileName);
if(currentCol > curImg.cols || currentRow > curImg.rows)
return; int count = curImg.channels();
ui->pixelInfo->setText(QString("pixel:(%1,%2)").arg(currentRow+).arg(currentCol+)); std::vector<cv::Mat> curImgMat(count);
cv::split(curImg,curImgMat);
QVector<float> pixSpectralData(count); myPixelData->clear();
myPixelData->setColumnCount();
myPixelData->setHorizontalHeaderLabels(QStringList()<<QStringLiteral("Band")<<QStringLiteral("Data"));
ui->pixelDataView->setModel(myPixelData);
ui->pixelDataView->setEditTriggers(QAbstractItemView::NoEditTriggers);
ui->pixelDataView->setColumnWidth(,);
ui->pixelDataView->setColumnWidth(,);
for(int c = ;c<count;c++)
{
pixSpectralData[c] = curImgMat[c].at<float>(currentCol,currentRow);
myPixelData->setItem(c,,new QStandardItem(tr("band%1").arg(QString::number(c+, ))));
myPixelData->setItem(c,,new QStandardItem(QString("%1").arg(pixSpectralData[c])));
}
ui->pixelDataView->setModel(myPixelData); } void ShowPixelData::exitShowPixelDataSlot()
{
emit sendExit();
close();
}
主窗体类:HyperProcess
实现功能:1 传出鼠标点击信号;
2 public声明,并且动态改变相应的输出值
头文件如下:
private slots:
void ShowPixelDataSlot(); // 显示子窗体槽
void receiveRowColSlot(int,int); // 从主窗体的GraphicsView接收行、列号,并传出信号 signals:
void sendSignalToShowPixelData(); public:
int curRow;
int curCol;
QString curFileName;
源文件:
void HyperProcess::ShowPixelDataSlot()
{
if(currentSceneIndex == -)
{
QMessageBox::information(this,"Message Error","please Open At Least One Image!");
return;
}else
{
myShowPixelData = new ShowPixelData(this);
myShowPixelData->show();
connect(myShowPixelData,SIGNAL(sendExit()),this,SLOT(receiveShowPixelDataSlot()));
myImg->setCursor(Qt::CrossCursor);
return;
}
} void HyperProcess::receiveRowColSlot(int row,int col)
{
curCol = col;
curRow = row;
emit sendSignalToShowPixelData();
} void HyperProcess::receiveShowPixelDataSlot()
{
myImg->setCursor(Qt::ArrowCursor);
}
三、实现效果