Creating a GUI application using QT

Source: Internet
Author: User


Create new project...And chooseQt GUI application


Specifying the opencv library files and header files location (with extension. Pro)

#-------------------------------------------------## Project created by QtCreator 2012-05-29T17:22:53##-------------------------------------------------QT       += core guiTARGET = p35QtGUIapTEMPLATE = appSOURCES += main.cpp\        mainwindow.cppHEADERS  += mainwindow.hFORMS    += mainwindow.uiINCLUDEPATH += E:\OpenCV2.3.1\build\include\opencv2\INCLUDEPATH += E:\OpenCV2.3.1\build\include\opencv\INCLUDEPATH += E:\OpenCV2.3.1\build\include\LIBS += -LE:\OpenCV2.3.1\build\x86\vc10\lib \-lopencv_core231d \-lopencv_highgui231d \-lopencv_video231d \-lopencv_ml231d \-lopencv_legacy231d \-lopencv_imgproc231d

Finding a file having the extension. UI, which is the one that describes the UI Layout

Defining a CV: mat class member variable in the header file of the mainwindow class

#ifndef MAINWINDOW_H#define MAINWINDOW_H#include <QMainWindow>#include <QFileDialog>#include <opencv2/core/core.hpp>#include <opencv2/highgui/highgui.hpp>namespace Ui {class MainWindow;}class MainWindow : public QMainWindow{    Q_OBJECT    public:    explicit MainWindow(QWidget *parent = 0);    ~MainWindow();    private:    Ui::MainWindow *ui;    cv::Mat iamge; // the image variable};#endif // MAINWINDOW_H

Callback Function

void MainWindow::on_pushButton_clicked(){    QString fileName = QFileDialog::getOpenFileName(this,            tr("Open Image"),".",          tr("Image File (*.png *.jpg *.jpeg *.bmp)"));        image= cv::imread(fileName.toAscii().data());    cv::cvtColor(image,image,CV_BGR2RGB);        // Qt image    QImage img= QImage((const unsigned char*)(image.data),                           image.cols,image.rows,QImage::Format_RGB888);        // display on label    ui->label->setPixmap(QPixmap::fromImage(img));        // resize the label to fit the image    ui->label->resize(ui->label->pixmap()->size());}void MainWindow::on_pushButton_2_clicked(){    cv::flip(image,image,1);    //cv::cvtColor(image,image,CV_BGR2RGB);    // Qt image    QImage img= QImage((const unsigned char*)(image.data),                       image.cols,image.rows,QImage::Format_RGB888);    // display on label    ui->label->setPixmap(QPixmap::fromImage(img));    // resize the label to fit the image    ui->label->resize(ui->label->pixmap()->size());}

Run:

Process:

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.