0

QtCreator を使用して GUI でソフトウェアを作成する方法を学ぼうとしています。私は以前にいくつかのプログラミングを行ったことがありますが、これに関係することはありませんでした。これまでのところ、2 つのラベル、1 つのボタン、1 つの lineEdit、1 つの listWidget の 5 つの項目を持つウィンドウを作成しました。

私の目標は、lineEdit にテキストを入力して listWidget に表示できるようにすることです。マウスでボタンをクリックすると、これで問題なく動作します。

また、キーボードの Enter/Return キーを使用してボタンをアクティブにできるようにしたいと考えています。これは私が助けを必要としている部分です。

キー イベントを処理するために、KeyboardFilter という新しいクラスを作成しました。

メイン ウィンドウ オブジェクトにイベント フィルタをインストールしました。eventFilter 関数は、任意のイベントを受け取り、それがキー イベントであるかどうかを確認し、次にそれが入力ボタンであるかどうかを確認する必要があります。そうであれば、ボタンをアクティブにしたいと思います。

eventFilter のために書いたものが実際に機能しているかどうかはわかりません。

// == keyboardfilter.h =======================================================

#ifndef KEYBOARDFILTER_H
#define KEYBOARDFILTER_H

#include <QApplication>
#include <QLineEdit>
#include <QKeyEvent>

class KeyboardFilter : public QObject
{
public:
    KeyboardFilter( QObject *parent = nullptr ) : QObject( parent ) {}

//protected:
public:
    bool eventFilter( QObject *target, QEvent *event );
};

#endif // KEYBOARDFILTER_H


// == mainwindow.h ===========================================================

#ifndef MAINWINDOW_H
#define MAINWINDOW_H

#include <QMainWindow>

namespace Ui {
class MainWindow;
}

class MainWindow : public QMainWindow
{
    Q_OBJECT

public:
    explicit MainWindow(QWidget *parent = nullptr);
    ~MainWindow();

private:
    Ui::MainWindow *ui;

// handles clicking the enter button with the mouse
//private slots:
public slots:
    void EnterPressed();

// supposed to handle key presses. doesn't actually work
//protected:
public:
    void KeyPressEvent(QKeyEvent *event);
    void KeyReleaseEvent(QKeyEvent *event);
    bool EventFilter(QEvent *event);
};

#endif // MAINWINDOW_H


// == keyboardfilter.cpp =====================================================

#include "keyboardfilter.h"

bool KeyboardFilter::eventFilter(QObject *target, QEvent *event)
{
    if(event->type() == QEvent::KeyPress)
    {
        QKeyEvent *keyEvent = static_cast<QKeyEvent*>(event);
        if(keyEvent->key() == Qt::Key_Return || keyEvent->key() == Qt::Key_Enter){
            return true;
        }
    }
    if(event->type() == QEvent::KeyRelease){
        QKeyEvent *keyEvent = static_cast<QKeyEvent*>(event);
        if(keyEvent->key() == Qt::Key_Return || keyEvent->key() == Qt::Key_Enter){
            return true;
        }
    }
    return false;
}


// == mainwindow.cpp =========================================================

#include "mainwindow.h"
#include "ui_mainwindow.h"
#include "QDebug"
#include <QKeyEvent>
#include <iostream>

QString stack[10];

MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
{
    ui->setupUi(this);

    stack[1] = "stack";
    ui->Display->addItem(stack[1]);

    connect(ui->Enter, SIGNAL(released()), this, SLOT(EnterPressed()));
}

MainWindow::~MainWindow()
{
    delete ui;
}

//qDebug() << "Debug Message";
//QDebug("text");

void MainWindow::EnterPressed(){
    //ui->Input->setText(QString("text"));

    ui->Display->clear();

    QString input = ui->Input->text();
    ui->Input->clear();

    ui->Display->addItem(input);
}

// -- keyboardfilter.cpp

#include "keyboardfilter.h"

bool KeyboardFilter::eventFilter(QObject *target, QEvent *event)
{
    if(event->type() == QEvent::KeyPress)
    {
        QKeyEvent *keyEvent = static_cast<QKeyEvent*>(event);
        if(keyEvent->key() == Qt::Key_Return || keyEvent->key() == Qt::Key_Enter){
            return true;
        }
    }
    if(event->type() == QEvent::KeyRelease){
        QKeyEvent *keyEvent = static_cast<QKeyEvent*>(event);
        if(keyEvent->key() == Qt::Key_Return || keyEvent->key() == Qt::Key_Enter){
            return true;
        }
    }
    return false;
}

// == main.cpp ===============================================================

#include "mainwindow.h"
#include "keyboardfilter.h"
#include <QApplication>

int main(int argc, char *argv[])
{
    QApplication a(argc, argv);
    MainWindow w;
    w.show();

    //    KeyboardFilter filter;
    //    a.installEventFilter(&filter);

    KeyboardFilter* key = new KeyboardFilter();
    w.installEventFilter(key);

    if(key){
        w.EnterPressed();
    }
    return a.exec();
}

このコードを実行すると、ウィンドウがポップアップし、lineEdit にテキストを入力できます。マウスでボタンをクリックすると、必要に応じてテキストが listWidget に移動されます。テキストを入力して「Enter」を押しても、何も起こりません。

Enterキーを押す前に、タブを押してlineEdit、listWidget、およびボタンにフォーカスを移そうとしましたが、役に立ちませんでした。

4

2 に答える 2