Qt Forum

    • Login
    • Search
    • Categories
    • Recent
    • Tags
    • Popular
    • Users
    • Groups
    • Search
    • Unsolved

    Solved LineEdit only allow specific keyPressEvent

    General and Desktop
    2
    2
    275
    Loading More Posts
    • Oldest to Newest
    • Newest to Oldest
    • Most Votes
    Reply
    • Reply as topic
    Log in to reply
    This topic has been deleted. Only users with topic management privileges can see it.
    • K
      kocka last edited by

      I am using win10 and qt5.

      I would like to disable everything if I press a button on the keyboard except the moving the cursor. When I pressed right or left arrow I would like to move the cursor.

      LineEdit.h

      #pragma once
      
      #include <QLineEdit>
      #include <QKeyEvent>
      #include <QDebug>
      
      class LineEdit : public QLineEdit {
      
          Q_OBJECT
      
      public:
      
          LineEdit(QWidget* parent = nullptr)
              : QLineEdit(parent) {}
      
          virtual void keyPressEvent(QKeyEvent* event) override {
      
              if(event->key() == Qt::Key_Left || event->key() == Qt::Key_Right) {
                  event->accept();
                  qDebug() << "accept";
              }
              else {
                  event->ignore();
                  qDebug() << "ignore";
              }
      
          }
      
      };
      

      mainwindow.cpp

      #include "mainwindow.h"
      #include "ui_mainwindow.h"
      
      #include "LineEdit.h"
      
      MainWindow::MainWindow(QWidget *parent)
          : QMainWindow(parent)
          , ui(new Ui::MainWindow) {
          ui->setupUi(this);
      
          QWidget* window = new QWidget;
      
          LineEdit* line = new LineEdit(window);
          line->setText("abcdefgh");
      
          setCentralWidget(window);
      }
      
      MainWindow::~MainWindow() {
          delete ui;
      }
      

      I get the appropiate qDebug output, so that cannot be the problem.

      eyllanesc 1 Reply Last reply Reply Quote 0
      • eyllanesc
        eyllanesc @kocka last edited by

        @kocka Change to:

        if(event->key() == Qt::Key_Left || event->key() == Qt::Key_Right) {
                QLineEdit::keyPressEvent(event);
                return;
        }
        

        If you want me to help you develop some work then you can write to my email: e.yllanescucho@gmal.com.

        1 Reply Last reply Reply Quote 1
        • eyllanesc
          eyllanesc @kocka last edited by

          @kocka Change to:

          if(event->key() == Qt::Key_Left || event->key() == Qt::Key_Right) {
                  QLineEdit::keyPressEvent(event);
                  return;
          }
          

          If you want me to help you develop some work then you can write to my email: e.yllanescucho@gmal.com.

          1 Reply Last reply Reply Quote 1
          • First post
            Last post