Qt鼠标点击事件处理:按Escape键退出程序

创建项目

Qt 入门实战教程(目录)

首先,创建一个名称为QtKeyEscape的Qt默认的窗口程序。

参考 :Qt Creator 创建 Qt 默认窗口程序

Qt响应键盘Escape事件

打开Qt Creator >>编辑 >> 项目 >> Headers>> mainwindow.h

右键 class MainWindow >> Refactor >> Insert Virtual Function of Base Classes

如下图所示:

选择 QWidget >> keyPressEvent

Insertion options:

>> Insert definitions in implementation file >> OK

如下图所示:

Qt按Escape键退出程序

打开 Sources >> mainwindow.cpp

添加头文件包含

>> #include <QKeyEvent>

退出当前程序

对于单窗口的Qt程序来说,只需要关闭当前窗口即可。

关闭当前窗口只需要使用 this->close(); 即可关闭退出当前窗口。

具体实现如下:给你的窗口类添加下面的成员函数实现,即可。

在 MainWindow::keyPressEvent 中退出的代码

void MainWindow::keyPressEvent(QKeyEvent *event)
{
    if(event->key() == Qt::Key_Escape)
    {
        this->close();
    }
}

此时运行程序,弹出窗口:

按下Escape键,窗口消失(程序退出)。

代码下载

百度云

链接:https://pan.baidu.com/s/1ZTsZqW2nLXOctb58_1G4Pw

提取码:1234

Gitee 码云

QtKeyEscape · CalmReason/learn-qt-with-code - 码云 - 开源中国 (gitee.com)

您可以使用QtC++来实现这个功能。首先,您需要在Qt中创建一个视频窗口,并在底部添加一个工具栏。在工具栏中,您可以添加两个QPushButton按钮,一个用于电子放大,另一个用于全屏显示。 当用户点击电子放大按钮时,您需要将视频窗口切换到全屏显示模式,并启动放大操作的代码逻辑。在全屏模式下,您可以通过捕捉鼠标左按下和释放事件,并在鼠标移动事件中实现放大和缩小操作。 以下是一个大致的代码示例: ```cpp #include <QtWidgets> class VideoWindow : public QMainWindow { public: VideoWindow(QWidget* parent = nullptr) : QMainWindow(parent) { videoWidget = new QWidget(this); setCentralWidget(videoWidget); toolbar = new QToolBar(this); addToolBar(Qt::BottomToolBarArea, toolbar); magnifyButton = new QPushButton("电子放大", toolbar); fullscreenButton = new QPushButton("全屏显示", toolbar); connect(magnifyButton, &QPushButton::clicked, this, &VideoWindow::onMagnifyClicked); connect(fullscreenButton, &QPushButton::clicked, this, &VideoWindow::onFullscreenClicked); // 其他初始化代码... } private slots: void onMagnifyClicked() { if (isFullscreen) return; // 切换到全屏模式 showFullScreen(); isFullscreen = true; // 启动放大操作的代码逻辑 // 捕捉鼠标事件并实现放大和缩小操作 // ... // 保持放大和缩小操作,直到按下ESC grabKeyboard(); } void onFullscreenClicked() { showFullScreen(); isFullscreen = true; } protected: void keyPressEvent(QKeyEvent* event) override { if (event->key() == Qt::Key_Escape) { if (isFullscreen) { // 退出放大和缩小操作,恢复到全屏状态 showNormal(); isFullscreen = false; releaseKeyboard(); } } } void mousePressEvent(QMouseEvent* event) override { if (isFullscreen && event->button() == Qt::LeftButton) { // 根据鼠标点击位置实现放大操作 // ... } } void mouseReleaseEvent(QMouseEvent* event) override { if (isFullscreen && event->button() == Qt::LeftButton) { // 根据鼠标释放位置实现缩小操作 // ... } } private: QWidget* videoWidget; QToolBar* toolbar; QPushButton* magnifyButton; QPushButton* fullscreenButton; bool isFullscreen = false; }; int main(int argc, char** argv) { QApplication app(argc, argv); VideoWindow videoWindow; videoWindow.show(); return app.exec(); } ``` 请注意,这只是一个基本的代码示例,您可能需要根据实际情况进行适当的修改和完善。希望对您有所帮助!
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

C++程序员Carea

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值