Windows下Qt使用Usb摄像头采集视频实现鼠标事件
来源:互联网 发布:随遇软件 编辑:程序博客网 时间:2024/05/22 10:36
此程序为是在Windows下实现采集640*480的图像,并实现相应的鼠标事件
首先,在Qt Creator 中创建MainWindow,在创建的时候创建图形界面,在MainWindow.ui中使用垂直或是水平布局,改布局的名称为:ImageView
然后在MainWindow.h为
#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include <QMainWindow>
#include <QLabel>
#include <QStatusBar>
#include <QMouseEvent>
#include <QMainWindow>
#include <QCamera>
#include <QCameraViewfinder>
namespace Ui {
class MainWindow;
}
class QCamera;
class QCameraViewfinder;
class MainWindow : public QMainWindow
{
Q_OBJECT
public:
explicit MainWindow(QWidget *parent = 0);
~MainWindow();
private:
Ui::MainWindow *ui;
QLabel *statusLabel;
QLabel *MousePosLabel;
QCamera *camera;
QCameraViewfinder *viewfinder;
protected:
void mousePressEvent(QMouseEvent *event);
void mouseMoveEvent(QMouseEvent *event);
void mouseReleaseEvent(QMouseEvent *event);
void mouseDoubleClickEvent(QMouseEvent *event);
};
#endif // MAINWINDOW_H
相应的mainwindow.cpp为:
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include<QToolTip>
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
camera=new QCamera(this);
viewfinder=new QCameraViewfinder(this);
ui->ImageView->addWidget(viewfinder);
camera->setViewfinder(viewfinder);
camera->start();
setWindowTitle(tr("鼠标事件"));
MousePosLabel = new QLabel;
MousePosLabel -> setText(tr(" "));
MousePosLabel -> setFixedWidth(100);
this -> setMouseTracking(true);
}
MainWindow::~MainWindow()
{
delete ui;
}
void MainWindow::mousePressEvent(QMouseEvent *event)
{
QString pStr("%1,%2");
pStr = pStr.arg(event->pos().x()).arg(event->pos().y());
QToolTip::showText(mapToGlobal(event->pos()) ,pStr);
QPoint pos;
qDebug()<<"x="<<event->x()<<" "<<"y="<<event->y();//局部
pos = QCursor::pos();
qDebug()<<"x:"<<pos.x()<<" "<<"y:"<<pos.y();//全局
QPoint cur;
if((event->pos().x()>640)||(event->pos().y()>480)||(event->pos().x()<0)||(event->pos().y()<0))
{
qDebug()<<"######";
QCursor p;
p.setPos(240,136);//回到中间位置
}
}
void MainWindow::mouseMoveEvent(QMouseEvent *event)
{
QString pStr("%1,%2");
pStr = pStr.arg(event->pos().x()).arg(event->pos().y());
QToolTip::showText(mapToGlobal(event->pos()) ,pStr);
QPoint pos;
qDebug()<<"x="<<event->x()<<" "<<"y="<<event->y();//局部
pos = QCursor::pos();
qDebug()<<"x:"<<pos.x()<<" "<<"y:"<<pos.y();//全局
QPoint cur;
if((event->pos().x()>640)||(event->pos().y()>480)||(event->pos().x()<0)||(event->pos().y()<0))
{
qDebug()<<"######";
QCursor p;
p.setPos(240,136);//回到中间位置
}
// MousePosLabel -> setText("("+QString::number(event -> x())+","+QString::number(event -> y())+")");
}
void MainWindow::mouseReleaseEvent(QMouseEvent *event)
{
QString pStr("%1,%2");
pStr = pStr.arg(event->pos().x()).arg(event->pos().y());
QToolTip::showText(mapToGlobal(event->pos()) ,pStr);
QPoint pos;
qDebug()<<"x="<<event->x()<<" "<<"y="<<event->y();//局部
pos = QCursor::pos();
qDebug()<<"x:"<<pos.x()<<" "<<"y:"<<pos.y();//全局
QPoint cur;
if((event->pos().x()>640)||(event->pos().y()>480)||(event->pos().x()<0)||(event->pos().y()<0))
{
qDebug()<<"######";
QCursor p;
p.setPos(240,136);//回到中间位置
}
}
void MainWindow::mouseDoubleClickEvent(QMouseEvent *event)
{
QString pStr("%1,%2");
pStr = pStr.arg(event->pos().x()).arg(event->pos().y());
QToolTip::showText(mapToGlobal(event->pos()) ,pStr);
QPoint pos;
qDebug()<<"x="<<event->x()<<" "<<"y="<<event->y();//局部
pos = QCursor::pos();
qDebug()<<"x:"<<pos.x()<<" "<<"y:"<<pos.y();//全局
QPoint cur;
if((event->pos().x()>640)||(event->pos().y()>480)||(event->pos().x()<0)||(event->pos().y()<0))
{
qDebug()<<"######";
QCursor p;
p.setPos(240,136);//回到中间位置
}
}
main函数为:
#include "mainwindow.h"
#include <QApplication>
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
MainWindow w;
w.resize(640,480);
w.show();
return a.exec();
}实现效果为:
阅读全文
0 0
- Windows下Qt使用Usb摄像头采集视频实现鼠标事件
- Qt摄像头视频采集
- 在raspberry上使用USB摄像头做视频采集
- windows下用ffmpeg采集摄像头的视频
- USB摄像头图片采集+QT显示(二)
- Windows远程桌面实现之三(电脑内部声音采集,录音采集,摄像头视频采集)
- QT 小车客户端 摄像头采集视频播放
- Windows 下视频采集
- ARM9+Linux下USB摄像头图像采集
- ARM9+Linux下USB摄像头图像采集
- 基于itop4412在Linux最小系统下的USB摄像头视频采集
- 基于itop4412在Linux最小系统下的USB摄像头采集视频的H264编码
- 用v4l2和framebuffer实现usb摄像头视频采集并显示
- 用v4l2和framebuffer实现usb摄像头视频采集并显示
- opencv摄像头视频采集(Windows系统)
- Linux 下摄像头视频采集与显示
- Linux 下摄像头视频采集与显示
- Linux 下摄像头视频采集与显示
- Unity(五) Unity3D使用Assetbundle打包加载(Prefab、场景)转载原博客
- 剑指offer 按之字形顺序打印二叉树
- connect函数崩溃
- txt文件加密
- java爬取西刺ip代理练习
- Windows下Qt使用Usb摄像头采集视频实现鼠标事件
- Linux文件系统
- Tomcat 启动问题
- Spring MVC基本配置
- iOS之UIButton
- 一分钟了解阿里云产品:高性能计算HPC
- 第二周
- office办公文件在线预览
- Spring定时任务的实现方式