标签:Widget Qt ui 事件 QWidget include event
事件
什么是事件
主要是鼠标点击,键盘响应,进入离开什么的,这些事件其实是QWidget的一些虚函数,我们要做的就是继承QWidget或其子类,然后重写这些虚函数,我们可以在帮助文档里找QWidget的虚函数来查看这些事件
怎么用
一般我们是编辑组件的事件响应,但是Qt自带的那些组件我们没法编辑,所以这里我们要新建一个C++类继承QWidget或其子类,然后用这个,或者我们可以将ui里的组件提升为我们自己写的类(右键组件然后选择提升为),然后我们把我们想要的事件复制到头文件然后在源文件里实现就行了。
举例
键盘移动图片,滑条控制速度(没有实现,一动滑条,图片的键盘响应好像就失效了),鼠标可以拖动图片,关闭后滑条值保存
代码如下
先是总体
mytoolbutton.h
#ifndef MYTOOLBAR_H
#define MYTOOLBAR_H
#include <QToolButton>
#include<QPoint>
#include<QMouseEvent>
#include<QKeyEvent>//要添头文件
class MyToolButton : public QToolButton//继承QToolButton
{
Q_OBJECT
public:
explicit MyToolButton(QWidget *parent = nullptr);
//将帮助文档里需要的事件函数复制到这里
void
keyPressEvent(QKeyEvent *event);
void
mouseMoveEvent(QMouseEvent *event);
void
mousePressEvent(QMouseEvent *event);
QPoint point;//存点击后鼠标的坐标
double speed=30;//默认的移动速度
signals:
};
#endif // MYTOOLBAR_H
mytoolbutton.cpp
#include "mytoolbutton.h"
#include<QDebug>
MyToolButton::MyToolButton(QWidget *parent) : QToolButton(parent)
{
}
void MyToolButton::keyPressEvent(QKeyEvent *event)
{
switch(event->key())//关注key()和Qt作用域下的那些键的宏
{
case Qt::Key_W : case Qt::Key_Up :
this->move(this->pos()+QPoint(0,-1*speed));
break;
case Qt::Key_S : case Qt::Key_Down :
this->move(this->pos()+QPoint(0,1*speed));
break;
case Qt::Key_D : case Qt::Key_Right :
this->move(this->pos()+QPoint(1*speed,0));
break;
case Qt::Key_A : case Qt::Key_Left :
this->move(this->pos()+QPoint(-1*speed,0));
break;
}
}
void MyToolButton::mouseMoveEvent(QMouseEvent *event)//这个事件默认只有在点击时移动才会出现,所以我们再写一个鼠标点击响应来保存点击时的鼠标坐标
{
QPoint delta=event->pos()-point;
this->move(this->pos()+delta);
}
void MyToolButton::mousePressEvent(QMouseEvent *event)
{
if(event->button()==Qt::LeftButton)
{
qDebug()<<"left clicked tianyi";
}
else if(event->button()==Qt::RightButton)
{
qDebug()<<"right clicked tianyi";
}
point=event->pos();//保存点击时的坐标,为后面图片移动做准备
}
widget.h
#ifndef WIDGET_H
#define WIDGET_H
#include <QWidget>
#include<QSettings>
QT_BEGIN_NAMESPACE
namespace Ui { class Widget; }
QT_END_NAMESPACE
class Widget : public QWidget
{
Q_OBJECT
public:
Widget(QWidget *parent = nullptr);
~Widget();
private slots:
void on_verticalSlider_valueChanged(int value);
private:
Ui::Widget *ui;
};
#endif // WIDGET_H
widget.cpp
#include "widget.h"
#include "ui_widget.h"
#include<QDebug>
Widget::Widget(QWidget *parent)
: QWidget(parent)
, ui(new Ui::Widget)
{
ui->setupUi(this);
this->ui->verticalSlider->setMaximum(100);
QSettings setting("config.ini");
ui->verticalSlider->setValue(setting.value("volume").toInt());//读取配置文件
}
Widget::~Widget()
{
delete ui;
}
void Widget::on_verticalSlider_valueChanged(int value)
{
ui->label->setText("speed:"+QString::number(value)+"%");
QSettings settingfile("config.ini");//QSetting对象可以实现一个查字典一样的效果,我们可以给每个关键字存一个值,用的时候调用value函数(注意返回结果是字符串)
settingfile.setValue("volume",value);
qDebug()<<value;
this->ui->tianyi->speed=value;
qDebug()<<this->ui->tianyi->speed;
}
标签:Widget,Qt,ui,事件,QWidget,include,event 来源: https://blog.csdn.net/asapigi/article/details/117876363
本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享; 2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关; 3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关; 4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除; 5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。