c++ - 管理退出程序的转义键

标签 c++ qt events key keyevent

我不知道如何实现退出程序退出键的管理。我也不知道将它放在我的代码中的什么位置,因为如果我将它放在一个方法中,它怎么能在任何地方退出?

这是我的实际代码:

    #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)
}

关于c++ - 管理退出程序的转义键,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36552425/

相关文章:

c++ - unordered_set 使用值对象地址的散列

c++ - 删除特定字符后的所有内容

qt - 如何使用 Qt 在文本文件中搜索字符串

javascript - 我可以从 "input"事件中获取什么样的信息?有没有更好的方法来跟踪文本更改?

HTML <option> 标签工具提示

c++ - 为什么使用 DrawText API 绘制时我的字体边缘不平滑?

java - Java有堆和栈吗?

c++ - cmake 在 macOS 下构建 llvm/clang 时出错

python - wx在PyQt中的空闲和UI更新事件

qt4 - 使用 Qt 4.6 钩住键盘上的键和组合键