Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Managing the escape key to quit a program

I don't know how to implement the managing of the escape key to quit the program. I don't know either where to put it in my code, because if I put it in a method, how can it quit anywhere?

This is my actual code :

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

and this is my .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_ */
like image 428
Michael003 Avatar asked Apr 11 '16 15:04

Michael003


People also ask

Is there an alternative to the Escape key?

Is there any alternative key for Esc? If you have an American English keyboard, pressing Ctrl-[ (control plus left square bracket) is equivalent to pressing Esc.

How do you continue or exit the program by pressing the keys?

1: press "Enter" to continue. 2: press "Esc" to exit the program.

What is the Escape key called in Python?

Learn More. In Python strings, the backslash "\" is a special character, also called the "escape" character. It is used in representing certain whitespace characters: "\t" is a tab, "\n" is a newline, and "\r" is a carriage return.

How do you exit a Python program with keypress?

To end a while loop prematurely in Python, press CTRL-C while your program is stuck in the loop. This will raise a KeyboardInterrupt error that terminates the whole program.


1 Answers

You need to override the method void QWidget::keyPressEvent(QKeyEvent *event). It will look like this for you :

void LibQt::keyPressEvent(QKeyEvent* event)
{
    if(event->key() == Qt::Key_Escape)
    {
         QCoreApplication::quit();   
    }
    else
        QWidget::keyPressEvent(event)
}
like image 105
IAmInPLS Avatar answered Oct 13 '22 00:10

IAmInPLS