Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. LineEdit only allow specific keyPressEvent
Forum Updated to NodeBB v4.3 + New Features

LineEdit only allow specific keyPressEvent

Scheduled Pinned Locked Moved Solved General and Desktop
2 Posts 2 Posters 560 Views 1 Watching
  • 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 Offline
    K Offline
    kocka
    wrote on last edited by
    #1

    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.

    eyllanescE 1 Reply Last reply
    0
    • K kocka

      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.

      eyllanescE Offline
      eyllanescE Offline
      eyllanesc
      wrote on last edited by
      #2

      @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
      1

      • Login

      • Login or register to search.
      • First post
        Last post
      0
      • Categories
      • Recent
      • Tags
      • Popular
      • Users
      • Groups
      • Search
      • Get Qt Extensions
      • Unsolved