close
#include "mainwindow.h"
#include "ui_mainwindow.h"
MainWindow::MainWindow(QWidget *parent)
: QMainWindow(parent)
, ui(new Ui::MainWindow)
{
ui->setupUi(this);
this->setGeometry(0,0, 640, 530);
// create a label to display an image
dispLbl = new QLabel("display", this);
dispLbl->setGeometry(QRect(QPoint(0, 0), QSize(640, 480)));
// create a pushbutton to load an image
loadBtn = new QPushButton("Load", this);
loadBtn->setGeometry(QRect(QPoint(0, 480), QSize(640, 50)));
// connect pushbutton signal
connect(loadBtn, SIGNAL(clicked()), this, SLOT(slotLoadImage()));
}
MainWindow::~MainWindow()
{
delete ui;
}
void MainWindow::slotLoadImage()
{
QString filename = QFileDialog::getOpenFileName(
this,
"Open file",
QString("c:/OpenCV/images/"),
"All files (*.*)"
);
this->path = filename;
this->loadBtn->setText(filename);
}
mainwindow.hpp
#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include <QMainWindow>
#include <QtWidgets/QPushButton>
#include <QtWidgets/QLabel>
#include <QString>
#include <QFileDialog>
#include <opencv2/opencv.hpp>
QT_BEGIN_NAMESPACE
namespace Ui { class MainWindow; }
QT_END_NAMESPACE
class MainWindow : public QMainWindow
{
Q_OBJECT
public:
MainWindow(QWidget *parent = nullptr);
~MainWindow();
private slots:
void slotLoadImage();
private:
Ui::MainWindow *ui;
QPushButton *loadBtn;
QLabel *dispLbl;
QString path;
void loadImage();
};
#endif // MAINWINDOW_H
mainwindow.cpp
#include "mainwindow.h"
#include "ui_mainwindow.h"
MainWindow::MainWindow(QWidget *parent)
: QMainWindow(parent)
, ui(new Ui::MainWindow)
{
ui->setupUi(this);
this->setGeometry(0,0, 640, 530);
// create a label to display an image
dispLbl = new QLabel("display", this);
dispLbl->setGeometry(QRect(QPoint(0, 0), QSize(640, 480)));
// create a pushbutton to load an image
loadBtn = new QPushButton("Load", this);
loadBtn->setGeometry(QRect(QPoint(0, 480), QSize(640, 50)));
// connect pushbutton signal
connect(loadBtn, SIGNAL(clicked()), this, SLOT(slotLoadImage()));
}
MainWindow::~MainWindow()
{
delete ui;
}
void MainWindow::slotLoadImage()
{
QString filename = QFileDialog::getOpenFileName(
this,
"Open file",
QString("c:/OpenCV/images/"),
"All files (*.*)"
);
this->path = filename;
this->loadBtn->setText(filename);
this->loadImage();
}
void MainWindow::loadImage()
{
cv::Mat img = cv::imread(path.toStdString(), cv::IMREAD_COLOR);
auto w = this->dispLbl->width();
auto h = this->dispLbl->height();
this->dispLbl->setPixmap(
QPixmap::fromImage(
QImage(img.data, img.cols, img.rows, img.step, QImage::Format_BGR888).scaled(w, h, Qt::KeepAspectRatio)
)
);
}
全站熱搜
留言列表