我正在尝试学习如何使用QtCreator使用GUI制作软件。我以前做过一些编程,但从来没有涉及过任何事情。到目前为止,我已经制作了一个包含 5 个项目的窗口:2 个标签、1 个按钮、1 个行编辑和 1 个列表小部件。
我的目标是能够在行编辑中输入文本并使其出现在listWidget中。如果您用鼠标单击按钮,则可以正常工作。
我还希望能够使用键盘上的回车键来激活按钮。这是我需要帮助的部分。
我创建了一个用于处理键事件的新类,称为键盘过滤器。
我已经在主窗口对象上安装了事件过滤器。eventFilter 函数应该接收任何事件,检查它是否是关键事件,然后检查它是否是 Enter 按钮。如果是,那么我想激活按钮。
我无法判断我为 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。如果我输入文本,然后点击"输入",则没有任何反应。
在按回车键之前,我尝试按选项卡以将焦点放在行编辑,列表小部件和按钮上,但并没有帮助。
也许 installEventFilter 的 Qt 文档不是绝对清楚的,您需要将事件过滤器安装到要过滤的事件的对象中。在你的例子中,它是QLineEdit(Input?)而不是MainWindow。此外,您实际上不需要事件过滤器的额外类。您可以覆盖 MainWindow 的 eventFilte 并将其安装在 QLineEdit 上。如果您不想停止处理事件,请务必返回false。
这是它的外观:
主.cpp
#include "mainwindow.h"
#include <QApplication>
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
MainWindow w;
w.show();
return a.exec();
}
主窗口.h
#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include <QMainWindow>
#include <QLineEdit>
#include <QKeyEvent>
namespace Ui {
class MainWindow;
}
class MainWindow : public QMainWindow
{
Q_OBJECT
public:
explicit MainWindow(QWidget *parent = nullptr);
~MainWindow() override;
private:
Ui::MainWindow *ui;
public slots:
void EnterPressed();
bool eventFilter(QObject *target, QEvent *event) override;
};
#endif // MAINWINDOW_H
主窗口.cpp
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QDebug>
#include <QKeyEvent>
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
//... your code here
this->ui->Input->installEventFilter(this);
}
MainWindow::~MainWindow()
{
delete ui;
}
void MainWindow::EnterPressed(){
//.... your code here
}
bool MainWindow::eventFilter(QObject *target, QEvent *event)
{
if(target != this->ui->Input)return false;
if(event->type() == QEvent::KeyPress)
{
QKeyEvent *keyEvent = static_cast<QKeyEvent*>(event);
if(keyEvent->key() == Qt::Key_Return || keyEvent->key() == Qt::Key_Enter){
qDebug() << "eventFilter Enter pressed";
this->EnterPressed(); // call your slot
return true;
}
}
if(event->type() == QEvent::KeyRelease){
QKeyEvent *keyEvent = static_cast<QKeyEvent*>(event);
if(keyEvent->key() == Qt::Key_Return || keyEvent->key() == Qt::Key_Enter){
qDebug() << "eventFilter Enter pressed released";
return true;
}
}
return false;
}
全局捕获Enter
/Return
键的一种简单方法是使用QShortcut
,然后将触发器事件连接到按钮单击。
QWidget *widget = new QWidget(this); //The main window central widget
QLineEdit *edit = new QLineEdit(this); //Your edit
QPushButton *button = new QPushButton("Accept", this); //Your button
QLabel *label = new QLabel(this); //Your label
QHBoxLayout *layout = new QHBoxLayout(widget); //Arrange items horizontally
layout->addWidget(edit);
layout->addWidget(button);
layout->addWidget(label);
connect(button, &QPushButton::clicked, this, [=](bool checked){ //The button clicked event
Q_UNUSED(checked)
label->setText(edit->text());
edit->clear();
});
QShortcut *shortcut_return = new QShortcut(Qt::Key_Return, this); //The return shortcut event
connect(shortcut_return, &QShortcut::activated, button, &QPushButton::click);
QShortcut *shortcut_enter = new QShortcut(Qt::Key_Enter, this); //The enter shortcut event
connect(shortcut_enter, &QShortcut::activated, button, &QPushButton::click);
button->setDefault(true); //Set the button as default
setCentralWidget(widget);