#include "opcv/CvGraphicsViewWidget.h" #include CvGraphicsViewWidget::CvGraphicsViewWidget(QWidget* parent) :QGraphicsView(parent) { if (parent != Q_NULLPTR) this->setParent(parent); this->setStyleSheet("QGraphicsView{" "background-color:transparent;" "border:none;" "}"); mGraphicsScene = new QGraphicsScene(); setMouseTracking(true);//跟踪鼠标位置 setDragMode(QGraphicsView::NoDrag); setTransformationAnchor(QGraphicsView::AnchorUnderMouse); setResizeAnchor(QGraphicsView::AnchorUnderMouse); } void CvGraphicsViewWidget::showImage(cv::Mat const& _cvimg) { QThread* thread =new QThread(); qDebug() << "CvGraphicsViewWidget::showImage thread:"<currentThreadId(); if (_cvimg.empty()) return; //QImage toShow; QPixmap pixmap; CvImage2QPixmap(_cvimg, pixmap); //QPixmap pixmap = QPixmap::fromImage(toShow); mGraphicsScene->clear(); mGraphicsScene->addPixmap(pixmap); setScene(mGraphicsScene); } //void CvGraphicsViewWidget::paintEvent(QPaintEvent* event) //{ // QGraphicsView::paintEvent(event); //} void CvGraphicsViewWidget::CvImage2QPixmap(cv::Mat const& fromCv, QPixmap& toPix) { const uchar* pSrc = (const uchar*)fromCv.data; QImage image; if (fromCv.type() == CV_8UC1) { qDebug() << "CV_8UC1"; image = QImage(pSrc, fromCv.cols, fromCv.rows, fromCv.step, QImage::Format_Grayscale8); } else if (fromCv.type() == CV_8UC3) { qDebug() << "CV_8UC3"; image = QImage(pSrc, fromCv.cols, fromCv.rows, fromCv.step, QImage::Format_RGB888); } else if (fromCv.type() == CV_8UC4) { qDebug() << "CV_8UC4"; image = QImage(pSrc, fromCv.cols, fromCv.rows, fromCv.step, QImage::Format_ARGB32); } else { qDebug() << "ERROR: Mat could not be converted to QImage."; return; } toPix = QPixmap::fromImage(image); } bool CvGraphicsViewWidget::QImage2CvImage(QImage& fromQ, cv::Mat& toCv) { return false; } //void CvGraphicsViewWidget::wheelEvent(QWheelEvent* ev) //{ // qreal qrTmp = 1.0; // if (ev->delta() > 0) // { // qrTmp = 1.2; // this->scale(qrTmp, qrTmp); // } // else // { // qrTmp = 1.0 / 1.2; // this->scale(qrTmp, qrTmp); // } // mScaledFactor *= qrTmp; //保存放大倍数 //}