zoukankan      html  css  js  c++  java
  • 3. QT窗体间值的传递(续)

    一、前言

    上篇博客中通过重载子窗体的构造函数将主窗体的值传入到子窗体,但是在子窗体运行过程中如何才能将值动态的传入到子窗体?可以有两种办法,1、信号和槽的方式传值;2、主窗体中将传出值设置为public

    本文主要实现功能为:鼠标点击主窗体的GraphicsView中的Scene时,将对应像素处的各个波段值传递给子窗体的TabelView并显示。其中子窗体的接受主窗体的值是通过2(public)的方式,简单方便。

    二、主、子窗体的设计

    定义子窗体类:ShowPixelData

    主要实现:1 接受主窗体传入的文件名、当前点击图像的行、列号;

                  2 根据传入值,读取并显示pixel data;

    头文件如下:

     1 #ifndef SHOWPIXELDATA_H
     2 #define SHOWPIXELDATA_H
     3 
     4 #include <QDialog>
     5 #include <QStandardItemModel>
     6 #include "hyperprocess.h"
     7 
     8 class HyperProcess;
     9 
    10 namespace Ui {
    11 class ShowPixelData;
    12 }
    13 
    14 class ShowPixelData : public QDialog
    15 {
    16     Q_OBJECT
    17 
    18 public:
    19     ShowPixelData(QWidget *parent = 0);
    20     ~ShowPixelData();
    21 
    22 
    23 private slots:
    24     void receiveHyperToPixelDataSlot();  // 接受鼠标单击信号
    25     void exitShowPixelDataSlot();  //
    26 
    27 signals:
    28     void sendExit();  //
    29 
    30 private:
    31     Ui::ShowPixelData *ui;
    32     HyperProcess *ptr;
    33 
    34     QStandardItemModel *myPixelData;
    35 
    36     int currentRow;  //
    37     int currentCol;  //
    38     QString curFileName;  //  文件名
    39 };
    40 
    41 #endif // SHOWPIXELDATA_H

    源文件如下:

     1 #include "ShowPixelData.h"
     2 #include "ui_ShowPixelData.h"
     3 
     4 
     5 ShowPixelData::ShowPixelData(QWidget *parent) :
     6     QDialog(parent),
     7     ui(new Ui::ShowPixelData)
     8 {
     9     ui->setupUi(this);
    10 
    11     currentRow = -1;
    12     currentCol = -1;
    13     curFileName = "";
    14     ptr = (HyperProcess*)parentWidget();   //获得主窗体的指针
    15 
    16     ui->pixelInfo->setText("Pixel Info");
    17     myPixelData = new QStandardItemModel;
    18     myPixelData->setColumnCount(2);
    19     myPixelData->setHorizontalHeaderLabels(QStringList()<<QStringLiteral("Band")<<QStringLiteral("Data"));
    20     ui->pixelDataView->setModel(myPixelData);// 初始化
    21     ui->pixelDataView->setEditTriggers(QAbstractItemView::NoEditTriggers);
    22     ui->pixelDataView->setColumnWidth(0,75);
    23     ui->pixelDataView->setColumnWidth(1,80);
    24 
    25     connect(ptr,SIGNAL(sendSignalToShowPixelData()),this,SLOT(receiveHyperToPixelDataSlot()));
    26     connect(ui->ExitButton,SIGNAL(clicked()),this,SLOT(exitShowPixelDataSlot()));
    27 }
    28 
    29 ShowPixelData::~ShowPixelData()
    30 {
    31     delete ui;
    32 }
    33 
    34 void ShowPixelData::receiveHyperToPixelDataSlot()
    35 {
    36     curFileName = ptr->curFileName;
    37     currentRow = ptr->curRow;
    38     currentCol = ptr->curCol;
    39 
    40     if(curFileName == "")
    41     {
    42         QMessageBox::information(this,"Message Error","Current Scene IS NULL!");
    43         return;
    44     }
    45 
    46     if(currentCol == -1 || currentRow == -1)
    47     {
    48         QMessageBox::information(this,"Message Error","please Select A Pixel");
    49         return;
    50     }
    51 
    52     cv::Mat curImg = GDALOpenCV::GDAL2Mat(curFileName);
    53     if(currentCol > curImg.cols || currentRow > curImg.rows)
    54         return;
    55 
    56     int count  = curImg.channels();
    57     ui->pixelInfo->setText(QString("pixel:(%1,%2)").arg(currentRow+1).arg(currentCol+1));
    58 
    59     std::vector<cv::Mat> curImgMat(count);
    60     cv::split(curImg,curImgMat);
    61     QVector<float> pixSpectralData(count);
    62 
    63     myPixelData->clear();
    64     myPixelData->setColumnCount(2);
    65     myPixelData->setHorizontalHeaderLabels(QStringList()<<QStringLiteral("Band")<<QStringLiteral("Data"));
    66     ui->pixelDataView->setModel(myPixelData);
    67     ui->pixelDataView->setEditTriggers(QAbstractItemView::NoEditTriggers);
    68     ui->pixelDataView->setColumnWidth(0,75);
    69     ui->pixelDataView->setColumnWidth(1,75);
    70     for(int c = 0;c<count;c++)
    71     {
    72         pixSpectralData[c] = curImgMat[c].at<float>(currentCol,currentRow);
    73         myPixelData->setItem(c,0,new QStandardItem(tr("band%1").arg(QString::number(c+1, 10))));
    74         myPixelData->setItem(c,1,new QStandardItem(QString("%1").arg(pixSpectralData[c])));
    75     }
    76     ui->pixelDataView->setModel(myPixelData);
    77 
    78 }
    79 
    80 void ShowPixelData::exitShowPixelDataSlot()
    81 {
    82     emit sendExit();
    83     close();
    84 }
    View Code

    主窗体类:HyperProcess

    实现功能:1 传出鼠标点击信号;

                  2 public声明,并且动态改变相应的输出值

    头文件如下:

     1 private slots:
     2     void ShowPixelDataSlot();   // 显示子窗体槽
     3     void receiveRowColSlot(int,int);  // 从主窗体的GraphicsView接收行、列号,并传出信号
     4 
     5 signals:
     6     void sendSignalToShowPixelData();
     7 
     8 public:
     9     int curRow;
    10     int curCol;
    11     QString curFileName;

    源文件:

     1 void HyperProcess::ShowPixelDataSlot()
     2 {
     3     if(currentSceneIndex == -1)
     4     {
     5         QMessageBox::information(this,"Message Error","please Open At Least One Image!");
     6         return;
     7     }else
     8     {
     9         myShowPixelData = new ShowPixelData(this);
    10         myShowPixelData->show();
    11         connect(myShowPixelData,SIGNAL(sendExit()),this,SLOT(receiveShowPixelDataSlot()));
    12         myImg->setCursor(Qt::CrossCursor);
    13         return;
    14     }
    15 }
    16 
    17 void HyperProcess::receiveRowColSlot(int row,int col)
    18 {
    19     curCol = col;
    20     curRow = row;
    21     emit sendSignalToShowPixelData();
    22 }
    23 
    24 void HyperProcess::receiveShowPixelDataSlot()
    25 {
    26     myImg->setCursor(Qt::ArrowCursor);
    27 }

    三、实现效果

       

  • 相关阅读:
    内部类
    四种权限修饰符
    final关键字
    多态
    关于接口
    c语言学习
    嵌入式-文件I/O
    嵌入式-基础三-打印-粘贴
    嵌入式-基础二
    嵌入式-基础一
  • 原文地址:https://www.cnblogs.com/zyore2013/p/4672973.html
Copyright © 2011-2022 走看看