2016-04-11 6 views
4

Ich weiß nicht, wie Sie die Verwaltung der Escape-Taste implementieren, um das Programm zu beenden. Ich weiß auch nicht, wo ich es in meinen Code schreiben soll, denn wenn ich es in eine Methode lege, wie kann es überall aufhören?Verwalten der Escape-Taste zum Beenden eines Programms

Dies ist mein eigentlicher 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); 
} 

und das ist mein .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_ */ 

Antwort

5

Sie müssen die Methode void QWidget::keyPressEvent(QKeyEvent *event) außer Kraft zu setzen. Es wird für Sie so aussehen:

void LibQt::keyPressEvent(QKeyEvent* event) 
{ 
    if(event->key() == Qt::Key_Escape) 
    { 
     QCoreApplication::quit(); 
    } 
    else 
     QWidget::keyPressEvent(event) 
} 
+0

Es funktioniert perfekt, danke! – Michael003

Verwandte Themen