From 705c8de54c16b9be11a5e0a45fc5bc59cdfc3cc8 Mon Sep 17 00:00:00 2001 From: wh201906 Date: Sat, 20 Feb 2021 20:51:26 +0800 Subject: [PATCH] Fix a bug in RawCommand Tab If a widget is set to NoFocus, then it will not respond to the key Event --- ui/mainwindow.cpp | 59 +++++++++++++++++++++++++++-------------------- ui/mainwindow.h | 3 +-- ui/mainwindow.ui | 3 --- 3 files changed, 35 insertions(+), 30 deletions(-) diff --git a/ui/mainwindow.cpp b/ui/mainwindow.cpp index bc14ce0..2af9994 100644 --- a/ui/mainwindow.cpp +++ b/ui/mainwindow.cpp @@ -33,7 +33,7 @@ MainWindow::MainWindow(QWidget *parent): util = new Util(this); mifare = new Mifare(ui, util, this); - keyEventFilter = new MyEventFilter(QEvent::KeyRelease); + keyEventFilter = new MyEventFilter(QEvent::KeyPress); resizeEventFilter = new MyEventFilter(QEvent::Resize); // hide unused tabs @@ -155,7 +155,7 @@ void MainWindow::on_PM3_disconnectButton_clicked() void MainWindow::refreshOutput(const QString& output) { // qDebug() << "MainWindow::refresh:" << output; - ui->Raw_outputEdit->insertPlainText(output); + ui->Raw_outputEdit->appendPlainText(output); ui->Raw_outputEdit->moveCursor(QTextCursor::End); } @@ -240,33 +240,40 @@ void MainWindow::refreshCMD(const QString& cmd) ui->Raw_CMDEdit->blockSignals(false); } -void MainWindow::on_Raw_CMDEdit_keyPressed(QObject* obj_addr, QEvent& event) +void MainWindow::on_Raw_keyPressed(QObject* obj_addr, QEvent& event) { - if(obj_addr == ui->Raw_CMDEdit && event.type() == QEvent::KeyRelease) + if(event.type() == QEvent::KeyPress) { QKeyEvent& keyEvent = static_cast(event); - if(keyEvent.key() == Qt::Key_Up) + if(obj_addr == ui->Raw_CMDEdit) { - if(stashedIndex > 0) - stashedIndex--; - else if(stashedIndex == -1) - stashedIndex = ui->Raw_CMDHistoryWidget->count() - 1; - } - else if(keyEvent.key() == Qt::Key_Down) - { - if(stashedIndex < ui->Raw_CMDHistoryWidget->count() - 1 && stashedIndex != -1) - stashedIndex++; - else if(stashedIndex == ui->Raw_CMDHistoryWidget->count() - 1) - stashedIndex = -1; + if(keyEvent.key() == Qt::Key_Up) + { + if(stashedIndex > 0) + stashedIndex--; + else if(stashedIndex == -1) + stashedIndex = ui->Raw_CMDHistoryWidget->count() - 1; + } + else if(keyEvent.key() == Qt::Key_Down) + { + if(stashedIndex < ui->Raw_CMDHistoryWidget->count() - 1 && stashedIndex != -1) + stashedIndex++; + else if(stashedIndex == ui->Raw_CMDHistoryWidget->count() - 1) + stashedIndex = -1; + } + if(keyEvent.key() == Qt::Key_Up || keyEvent.key() == Qt::Key_Down) + { + ui->Raw_CMDEdit->blockSignals(true); + if(stashedIndex == -1) + ui->Raw_CMDEdit->setText(stashedCMDEditText); + else + ui->Raw_CMDEdit->setText(ui->Raw_CMDHistoryWidget->item(stashedIndex)->text()); + ui->Raw_CMDEdit->blockSignals(false); + } } - if(keyEvent.key() == Qt::Key_Up || keyEvent.key() == Qt::Key_Down) + else if(obj_addr == ui->Raw_outputEdit) { - ui->Raw_CMDEdit->blockSignals(true); - if(stashedIndex == -1) - ui->Raw_CMDEdit->setText(stashedCMDEditText); - else - ui->Raw_CMDEdit->setText(ui->Raw_CMDHistoryWidget->item(stashedIndex)->text()); - ui->Raw_CMDEdit->blockSignals(false); + ui->Raw_CMDEdit->setFocus(); } } } @@ -907,11 +914,12 @@ void MainWindow::MF_widgetReset() void MainWindow::uiInit() { - connect(ui->Raw_CMDEdit, &QLineEdit::editingFinished, this, &MainWindow::sendMSG); + connect(ui->Raw_CMDEdit, &QLineEdit::returnPressed, this, &MainWindow::sendMSG); ui->Raw_CMDEdit->installEventFilter(keyEventFilter); - connect(keyEventFilter, &MyEventFilter::eventHappened, this, &MainWindow::on_Raw_CMDEdit_keyPressed); + connect(keyEventFilter, &MyEventFilter::eventHappened, this, &MainWindow::on_Raw_keyPressed); ui->MF_keyWidget->installEventFilter(resizeEventFilter); connect(resizeEventFilter, &MyEventFilter::eventHappened, this, &MainWindow::on_MF_keyWidget_resized); + ui->Raw_outputEdit->installEventFilter(keyEventFilter); connectStatusBar = new QLabel(this); programStatusBar = new QLabel(this); @@ -994,6 +1002,7 @@ void MainWindow::uiInit() ui->MF_RW_keyTypeBox->addItem("B", Mifare::KEY_B); on_Raw_CMDHistoryBox_stateChanged(Qt::Unchecked); + } void MainWindow::signalInit() diff --git a/ui/mainwindow.h b/ui/mainwindow.h index e0ec0a3..36a0785 100644 --- a/ui/mainwindow.h +++ b/ui/mainwindow.h @@ -53,7 +53,7 @@ public slots: void setStatusBar(QLabel* target, const QString& text); void onPM3StateChanged(bool st, const QString& info); void MF_onMFCardTypeChanged(int id, bool st); - void on_Raw_CMDEdit_keyPressed(QObject *obj_addr, QEvent &event); + void on_Raw_keyPressed(QObject *obj_addr, QEvent &event); void on_MF_keyWidget_resized(QObject *obj_addr, QEvent &event); private slots: @@ -208,7 +208,6 @@ private: MF_trailerDecoderDialog* decDialog; - void signalInit(); void MF_widgetReset(); void setTableItem(QTableWidget *widget, int row, int column, const QString& text); diff --git a/ui/mainwindow.ui b/ui/mainwindow.ui index ba5bfdf..2135250 100644 --- a/ui/mainwindow.ui +++ b/ui/mainwindow.ui @@ -1446,9 +1446,6 @@ 0 - - Qt::NoFocus - Qt::ScrollBarAlwaysOn