管理退出程序的转义键
Managing the escape key to quit a program
我不知道如何实现转义键的管理以退出程序。我也不知道该把它放在代码中的哪里,因为如果我把它放进一个方法中,它怎么能在任何地方退出?
这是我的实际代码:
#include <iostream>
#include <QApplication>
#include <QPushButton>
#include <QLineEdit>
#include <QFormLayout>
#include <QDebug>
#include "LibQt.hpp"
LibQt::LibQt() : QWidget()
{
this->size_x = 500;
this->size_y = 500;
QWidget::setWindowTitle("The Plazza");
setFixedSize(this->size_x, this->size_y);
manageOrder();
}
LibQt::~LibQt()
{
}
void LibQt::manageOrder()
{
this->testline = new QLineEdit;
this->m_button = new QPushButton("Send Order");
QFormLayout *converLayout = new QFormLayout;
this->m_button->setCursor(Qt::PointingHandCursor);
this->m_button->setFont(QFont("Comic Sans MS", 14));
converLayout->addRow("Order : ", this->testline);
converLayout->addWidget(this->m_button);
this->setLayout(converLayout);
QObject::connect(m_button, SIGNAL(clicked()), this, SLOT(ClearAndGetTxt()));
}
std::string LibQt::ClearAndGetTxt()
{
QString txt = this->testline->text();
this->usertxt = txt.toStdString();
std::cout << this->usertxt << std::endl;
this->testline->clear();
return (this->usertxt);
}
std::string LibQt::getUsertxt()
{
return (this->usertxt);
}
这是我的.hpp
#ifndef _LIBQT_HPP_
#define _LIBQT_HPP_
#include <QApplication>
#include <QWidget>
#include <QPushButton>
#include <QLineEdit>
#include <QKeyEvent>
#include <QCheckBox>
#include <QPlainTextEdit>
class LibQt : public QWidget
{
Q_OBJECT
public:
LibQt();
~LibQt();
void manageOrder();
std::string getUsertxt();
public slots:
std::string ClearAndGetTxt();
protected:
int size_x;
int size_y;
QPushButton *m_button;
QLineEdit *testline;
std::string usertxt;
};
#endif /* _LIBQT_HPP_ */
您需要重写方法void QWidget::keyPressEvent(QKeyEvent *event)
。你会看到这样的:
void LibQt::keyPressEvent(QKeyEvent* event)
{
if(event->key() == Qt::Key_Escape)
{
QCoreApplication::quit();
}
else
QWidget::keyPressEvent(event)
}
我知道你解决了这个问题,我的解决方案是Python,但其他人可能会觉得这很有用。从本质上讲,您可以将此功能作为一种操作来实现:
w = LibQt() # QWidget().
escape = QAction('Escape', w)
escape.setShortcut(QKeySequence('Esc'))
escape.setShortcutContext(Qt.WidgetWithChildrenShortcut) # acts as an event filter.
escape.triggered.connect(w.close)
w.addAction(escape)
显然,删除第一行后,可以将此代码添加到类构造函数中。
相关文章:
- 为什么异常不退出程序?
- 虽然循环在使用回车键时不退出程序
- 如果 I/O read() 处于阻塞阶段,如何使用 Ctrl+C 退出 C++ 程序?
- 捕获异常后如何退出程序执行
- 在C++中使用exit()退出程序有问题吗
- 从函数打印 CLI 帮助消息后,我应该如何干净地退出 C++ 程序?
- 如何实现否定用户输入退出程序和打印列表?
- gdb 退出:程序接收信号 SIGILL,使用 gcc / intel_mkl / gfortran -mavx 编译的
- 试图调节我的 do-while 以停止在终端中退出程序.菜单不会根据需要循环使用当前语句
- 如何在C 中按ENTER/返回键时退出程序循环
- 退出程序,默认开关
- 我想让程序保持运行,直到用户输入 n 退出程序
- 我找不到第一个用户输入后立即退出程序的原因
- 安全地退出程序 Win32 C++从分离的 std::线程
- 如何使用QCloseEvent退出程序
- 在主之前退出程序是明确的行为吗?
- C++键盘挂钩-退出程序,但也防止密钥在其他地方被处理
- 在类中运行析构函数时退出程序
- 由于删除 [] 而未退出程序
- 管理退出程序的转义键