How to draw rectangle on video using mouse left button click event and mouse drag event in Qt Creator 2.8.1 Based on Qt 5.1.1?

I need help, I want to draw rectangle on video using mouse left button click in Qt creator.I load the video in Qlabel. I wants when I press mouse left button at that time those points should become the starting point of rectangle (like x1,y1) and then I continue mouse drag without leave the mouse left button and last when I leave mouse button at that time those points should become the end points of my rectangle(like x2,y2).

I already did it in visual C++ in visual studio 2008 and lots of try to do it in Qt creator but can't get success. So please help me.

Thanks in advance

This is the line where my mouse event will be called.

cvSetMouseCallback("Motion Detector", mouseEvent, 0);

And bellow is my mouse event full code:

void mouseEvent(int evt, int x, int y, int flags, void* param)
{
if(evt==CV_EVENT_LBUTTONDOWN)
{
     p1.x = x;
     p1.y = y;
     drag = 1;
    }

if(evt==CV_EVENT_LBUTTONUP)
{
     p2.x = x;
     p2.y = y;
     drag = 0;

    if(p1.x > p2.x)
    {
        int temp = p1.x;
        p1.x = p2.x;
        p2.x = temp;
    }

    if(p1.y > p2.y)
    {
        int temp = p1.y;
        p1.y = p2.y;
        p2.y = temp;
    }

    posx = p1.x;
    posy = p1.y;
    }

if(evt==CV_EVENT_MOUSEMOVE && drag==1)
{
    cvCopyImage(frame,img1,0);
    cvDrawRect(img1,p1,cvPoint(x,y),cvScalar(255,0,0,0),1,8,0);
    cvShowImage("Motion Detector",img1);
    }
}

This is my simple Qt program which is display image in QLabel and I want to draw rectangle on that image using mouse left button click and drag the size of rectangle and leave the button of mouse when I got the appropriate size of rectangle.

Mainwindow.cpp

#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <opencv2/opencv.hpp>
#include <QFileDialog>
#include "QMouseEvent"
#include "QMoveEvent"

MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
{
    ui->setupUi(this);

    //openImage();
}
MainWindow::~MainWindow()
{
    delete ui;
}

void MainWindow::openImage()
{
    int x1 = 1,x2 = 640,y1 = 100;

    iplImg = cvLoadImage("E://img2.jpg");

    //cvLine(iplImg, cvPoint(x1,y1), cvPoint(x2,y1), cvScalar(255,0,0), 2, 0);

    qimgNew = QImage((const unsigned char*)iplImg->imageData,iplImg->width,iplImg->height,QImage::Format_RGB888).rgbSwapped();
    ui->lblImage->setPixmap(QPixmap::fromImage(qimgNew)); 
}

void MainWindow::on_btnOpen_clicked()
{
    openImage();
}

main.cpp

#include "mainwindow.h"
#include <QApplication>

int main(int argc, char *argv[])
{
    QApplication a(argc, argv);
    MainWindow w;
    w.show();

    return a.exec();
}

mainwindow.h

#ifndef MAINWINDOW_H
#define MAINWINDOW_H

#include <QMainWindow>
#include <opencv/cv.h>
#include <opencv/highgui.h>

#include <QMainWindow>
//#include <QtGui/QWidget>
#include<QMouseEvent>
#include<QGraphicsLineItem>
#include<QGraphicsScene>
#include<QGraphicsView>
#include<QGraphicsItem>
#include<QHBoxLayout>

namespace Ui {
class MainWindow;
}

namespace converter
{
    IplImage* QImage2IplImage(const QImage* qimg);
    QImage* IplImage2QImage(const IplImage* iplImg);
}

class MainWindow : public QMainWindow
{
    Q_OBJECT

public:
    explicit MainWindow(QWidget *parent = 0);
    ~MainWindow();


private slots:

    void openImage();
    //void mousePressEvent();

    void on_btnOpen_clicked();
private:
    Ui::MainWindow *ui;
    //QString fileName;
    IplImage *iplImg;
    //char* charFileName;
    QImage qimgNew;
    //QImage qimgGray;
};
#endif // MAINWINDOW_H

.pro file

QT += core gui

QT += widgets

greaterThan(QT_MAJOR_VERSION, 4): QT += widgets

TARGET = Display_image_and_video
TEMPLATE = app


SOURCES += main.cpp\
        mainwindow.cpp

HEADERS  += mainwindow.h

FORMS    += mainwindow.ui

INCLUDEPATH += E:\\ImageProcessing\\opencv_cmake_binaries\\install\\include
LIBS += -LE:\\ImageProcessing\\opencv_cmake_binaries\\install\\lib \
    -lopencv_core244.dll \
    -lopencv_highgui244.dll \
    -lopencv_imgproc244.dll \
    -lopencv_features2d244.dll \
    -lopencv_calib3d244.dll

Answers


Well, you could start by overriding the virtual functions in QWidget:

to see that the events are captured (you should do this for the QWidget that is the label where you wan't to draw) and then in the event changed the picture displayed.

After the code update you did for your question i'd say that you can subclass QLabel you make your own widget, add the mouse event overrides mentioned above to it and use your class instance insteat of QLabel for the lblImage variable. And then change the pixel map in these event handlers.


Need Your Help

Javascript if else with a json file

javascript jquery json

I am using twitch api json files with javascript functions and I wanted to see if I could write an if else statement with a part of the json file being tested. Here is the link to the json file.So ...

How does Spring resolve views?

java spring java-ee spring-mvc

I've been having a little trouble with View Resolution under Spring 2.5.5. Basically I'm just trying to show my view with a message from the controller passed in. The issue comes when the Controller