QT鼠标点击响应事件
Posted zieckey
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了QT鼠标点击响应事件相关的知识,希望对你有一定的参考价值。
假如我们想在窗口指定区域响应鼠标点击事件,怎么办呢?
比如我们有一个widget窗口,该窗口里有一个PixmapLabel图片,
我们假设想在图片的左上角响应鼠标的点击事件,那么我们可以这样做!
1. 创建一个新类
//mainForm.h
#ifndef MAINFORM_H
#define MAINFORM_H
#include <qevent.h>
#include <qvariant.h>
#include <qwidget.h>
#include <qmessagebox.h>
class QVBoxLayout;
class QHBoxLayout;
class QGridLayout;
class QLabel;
class mainForm : public QWidget
{
Q_OBJECT
public:
mainForm( QWidget* parent = 0, const char* name = 0, WFlags fl = 0 );
~mainForm();
QLabel* myPixmapLabel;
signals:
void clicked();
public slots:
virtual void mousePressEventSlot();
protected:
void mousePressEvent(QMouseEvent *);
protected slots:
virtual void languageChange();
};
#endif // MAINFORM_H
2. 实现文件
//mainForm.cpp
#include "mainForm.h"
#include <qvariant.h>
#include <qlabel.h>
#include <qlayout.h>
#include <qtooltip.h>
#include <qwhatsthis.h>
#include <qimage.h>
#include <qpixmap.h>
/*
* Constructs a mainForm as a child of 'parent', with the
* name 'name' and widget flags set to 'f'.
*/
mainForm::mainForm( QWidget* parent, const char* name, WFlags fl )
: QWidget( parent, name, fl )
{
if ( !name )
setName( "mainForm" );
//setCaption("Qt Mouse Click Event Example");
myPixmapLabel = new QLabel( this, "myPixmapLabel" );
myPixmapLabel->setGeometry( QRect( 120, 60, 300, 270 ) );
//放置一个图片,该图片应该在同一文件夹里,否则要指定路径
myPixmapLabel->setPixmap( QPixmap::fromMimeSource( "about-to-install.png" ) );
myPixmapLabel->setScaledContents( TRUE );
languageChange();
resize( QSize(600, 480).expandedTo(minimumSizeHint()) );
connect( this, SIGNAL( clicked() ), this, SLOT( mousePressEventSlot() ) ); //信号连接
}
/*
* Destroys the object and frees any allocated resources
*/
mainForm::~mainForm()
{
// no need to delete child widgets, Qt does it all for us
}
void mainForm::mousePressEvent(QMouseEvent *e)
{
int x = e->x();
int y = e->y();
//假如在QRect( 120, 60, 200, 200 ) );这个区域里,就发出信号
if (x>120 && x<200 && y>60 && y<200)
emit clicked();
}
void mainForm::mousePressEventSlot()
{
//该信号响应的曹
//给出一个提示信息
QMessageBox::about( this, "Qt Mouse Click Event Example",
"You haved clicked the prearranged position /nand the widget will be closed."
);
close(); //关闭程序
}
/*
* Sets the strings of the subwidgets using the current
* language.
*/
void mainForm::languageChange()
{
setCaption( tr( "Qt Mouse Click Event Example" ) );
}
3. main 函数
//main.cpp
#include <qapplication.h>
#include "mainForm.h"
int main( int argc, char ** argv )
{
QApplication a( argc, argv );
mainForm w;
w.show();
a.connect( &a, SIGNAL( lastWindowClosed() ), &a, SLOT( quit() ) );
return a.exec();
}
将三个上述文件放在同一文件夹里,然后新建 pro 文件:
TEMPLATE = app
INCLUDEPATH += .
# Input
HEADERS += mainForm.h
SOURCES += main.cpp mainForm.cpp
然后:
qmake
make
直接运行,看看效果如何?
比如我们有一个widget窗口,该窗口里有一个PixmapLabel图片,
我们假设想在图片的左上角响应鼠标的点击事件,那么我们可以这样做!
1. 创建一个新类
//mainForm.h
#ifndef MAINFORM_H
#define MAINFORM_H
#include <qevent.h>
#include <qvariant.h>
#include <qwidget.h>
#include <qmessagebox.h>
class QVBoxLayout;
class QHBoxLayout;
class QGridLayout;
class QLabel;
class mainForm : public QWidget
{
Q_OBJECT
public:
mainForm( QWidget* parent = 0, const char* name = 0, WFlags fl = 0 );
~mainForm();
QLabel* myPixmapLabel;
signals:
void clicked();
public slots:
virtual void mousePressEventSlot();
protected:
void mousePressEvent(QMouseEvent *);
protected slots:
virtual void languageChange();
};
#endif // MAINFORM_H
2. 实现文件
//mainForm.cpp
#include "mainForm.h"
#include <qvariant.h>
#include <qlabel.h>
#include <qlayout.h>
#include <qtooltip.h>
#include <qwhatsthis.h>
#include <qimage.h>
#include <qpixmap.h>
/*
* Constructs a mainForm as a child of 'parent', with the
* name 'name' and widget flags set to 'f'.
*/
mainForm::mainForm( QWidget* parent, const char* name, WFlags fl )
: QWidget( parent, name, fl )
{
if ( !name )
setName( "mainForm" );
//setCaption("Qt Mouse Click Event Example");
myPixmapLabel = new QLabel( this, "myPixmapLabel" );
myPixmapLabel->setGeometry( QRect( 120, 60, 300, 270 ) );
//放置一个图片,该图片应该在同一文件夹里,否则要指定路径
myPixmapLabel->setPixmap( QPixmap::fromMimeSource( "about-to-install.png" ) );
myPixmapLabel->setScaledContents( TRUE );
languageChange();
resize( QSize(600, 480).expandedTo(minimumSizeHint()) );
connect( this, SIGNAL( clicked() ), this, SLOT( mousePressEventSlot() ) ); //信号连接
}
/*
* Destroys the object and frees any allocated resources
*/
mainForm::~mainForm()
{
// no need to delete child widgets, Qt does it all for us
}
void mainForm::mousePressEvent(QMouseEvent *e)
{
int x = e->x();
int y = e->y();
//假如在QRect( 120, 60, 200, 200 ) );这个区域里,就发出信号
if (x>120 && x<200 && y>60 && y<200)
emit clicked();
}
void mainForm::mousePressEventSlot()
{
//该信号响应的曹
//给出一个提示信息
QMessageBox::about( this, "Qt Mouse Click Event Example",
"You haved clicked the prearranged position /nand the widget will be closed."
);
close(); //关闭程序
}
/*
* Sets the strings of the subwidgets using the current
* language.
*/
void mainForm::languageChange()
{
setCaption( tr( "Qt Mouse Click Event Example" ) );
}
3. main 函数
//main.cpp
#include <qapplication.h>
#include "mainForm.h"
int main( int argc, char ** argv )
{
QApplication a( argc, argv );
mainForm w;
w.show();
a.connect( &a, SIGNAL( lastWindowClosed() ), &a, SLOT( quit() ) );
return a.exec();
}
将三个上述文件放在同一文件夹里,然后新建 pro 文件:
TEMPLATE = app
INCLUDEPATH += .
# Input
HEADERS += mainForm.h
SOURCES += main.cpp mainForm.cpp
然后:
qmake
make
直接运行,看看效果如何?
以上是关于QT鼠标点击响应事件的主要内容,如果未能解决你的问题,请参考以下文章