How can I move a QSlider slider handle with the keyboard?
I can't figure out how I can move a slider handle of a QSlider in QT. When I press for example A( I want to move coursor left) and D(I want move coursor right) so I did
(void) new QShortcut(Qt::Key_A, this, SLOT(moveTickmarkLeft()));
(void) new QShortcut(Qt::Key_D, this, SLOT(moveTickmarkRight()));
implementation:
void LCDRange::moveTickmarkLeft()
{
slider->setSliderPosition(slider->sliderPosition - 1);
}
void LCDRange::moveTickmarkRight()
{
slider->setSliderPosition(slider->sliderPosition + 1);
}
the same I did using function setTickPosition()
, w开发者_如何转开发hat is the difference between setSliderPosition()
and setTickPosition()
how can I implement my idea, thanx in advance for any help
I believe using setSliderPosition method is the correct way of moving your slider in code. setTickPosition set the way how the tick mark should be drawn, so I guess, this is not smth you need. As for intercepting keyboard events: you can install an event filter to your form ui controls and put your slider moving logic there. Below is an example. More details on event filter here
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
// install event filter to ui controls of the window
ui->textEdit->installEventFilter(this);
ui->pushButton->installEventFilter(this);
ui->horizontalSlider->installEventFilter(this);
}
// event filter implementation
bool MainWindow::eventFilter(QObject* watched, QEvent* event)
{
if (event->type() == QEvent::KeyPress )
{
QKeyEvent* keyEvent = (QKeyEvent*)event;
if (keyEvent->key()=='A')
{
qDebug() << "move slider";
ui->horizontalSlider->setSliderPosition(ui->horizontalSlider->sliderPosition()+1);
}
else if (keyEvent->key()=='B')
{
qDebug() << "move slider";
ui->horizontalSlider->setSliderPosition(ui->horizontalSlider->sliderPosition()-1);
}
}
return QMainWindow::eventFilter(watched, event);
}
hope this helps, regards
精彩评论