Home > Blockchain >  How to do Qt key is kept down
How to do Qt key is kept down

Time:03-15

When a key is kept down for increment/decrement (lets say for "Data"), the value should keep increasing/decreasing by 1 in every 100 milliseconds

○ That means the value will increase/decrease about 10 times in a second as long as the keyboard key is down

○ It doesn't have to be exactly 100 milliseconds, between 90 and 110 is OK

○ Apply this for all the values that is directly controlled by the keyboard

CodePudding user response:

Use a qButton and play with these 3 methods/props:

autoRepeat : bool
autoRepeatDelay : int
autoRepeatInterval : int

with those you can define your increment/decrement every 100 ms

autoRepeat

autoRepeatDelay

autoRepeatInterval

CodePudding user response:

How to use?

This is my code.

#include "mainwindow.h"

#include "ui_mainwindow.h"

#include <QKeyEvent>

using namespace Qt;

//constructor
MainWindow::MainWindow(QWidget * parent): QMainWindow(parent), ui(new Ui::MainWindow) {

    //const variables
  current = 1;

  ui -> setupUi(this);

  ui -> label_2 -> setStyleSheet("QLabel { color : white; }");

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

}

void MainWindow::keyPressEvent(QKeyEvent * e) {

  //Current
  switch (e -> key()) {
  case Key_Q:
    ui -> label_2 -> setText(QString::number(current  ));
    break;
  case Key_A:
    ui -> label_2 -> setText(QString::number(current--));
    break;

  default:
    break;
  }


}
  •  Tags:  
  • c qt
  • Related