How to set focus to a combo box on a key press event?
-
I have a set of comboBox each having a list of three elements.All my operations are concerned with the key press events.The problem with this is:My widget does not accept any key press event except Tab and Enter.I checked the same by disabling all the combox then the key press events are passed.
How do I resolve this?? -
I am not able to reproduce this problem with the example below. In the example the widget gets a keypress event when pressing "A". I then set focus to the second combobox and can switch between its element by pressing "F", "S" and "T". Does my example reproduce your problem? If not, can you modify it so that it does?
@
#include <QtGui>class ComboBox : public QComboBox
{
Q_OBJECT
public:
ComboBox(QWidget *parent) : QComboBox(parent)
{
addItem("First");
addItem("Second");
addItem("Third");
}
};class Widget : public QWidget
{
public:
Widget()
{
setFocusPolicy(Qt::StrongFocus);
box1 = new ComboBox(this);
box2 = new ComboBox(this);
box3 = new ComboBox(this);QVBoxLayout *layout = new QVBoxLayout(this);
layout->addWidget(box1);
layout->addWidget(box2);
layout->addWidget(box3);
}
void keyPressEvent(QKeyEvent *e)
{
if (e->key() == Qt::Key_A) {
qDebug() << "Widget got keypress event";
box2->setFocus();
}
QWidget::keyPressEvent(e);
}
private:
QComboBox *box1;
QComboBox *box2;
QComboBox *box3;
};#include "main.moc"
int main(int argc, char** argv)
{
QApplication app(argc, argv);
Widget wid;wid.setFocus();
wid.show();
return app.exec();}#include <QtGui>
class ComboBox : public QComboBox
{
Q_OBJECT
public:
ComboBox(QWidget *parent) : QComboBox(parent)
{
addItem("First");
addItem("Second");
addItem("Third");
}
};class Widget : public QWidget
{
public:
Widget()
{
setFocusPolicy(Qt::StrongFocus);
box1 = new ComboBox(this);
box2 = new ComboBox(this);
box3 = new ComboBox(this);QVBoxLayout *layout = new QVBoxLayout(this);
layout->addWidget(box1);
layout->addWidget(box2);
layout->addWidget(box3);
}
void keyPressEvent(QKeyEvent *e)
{
if (e->key() == Qt::Key_A) {
qDebug() << "Widget got keypress event";
box2->setFocus();
}
QWidget::keyPressEvent(e);
}
private:
QComboBox *box1;
QComboBox *box2;
QComboBox *box3;
};#include "main.moc"
int main(int argc, char** argv)
{
QApplication app(argc, argv);
Widget wid;wid.setFocus();
wid.show();
return app.exec();}
@ -
@
MainWindow.h
#ifndef MAINWINDOW_H
#define MAINWINDOW_H#include <QMainWindow>
namespace Ui {
class MainWindow;
}class MainWindow : public QMainWindow
{
Q_OBJECTpublic:
explicit MainWindow(QWidget *parent = 0);
~MainWindow();private:
Ui::MainWindow *ui;protected:
void keyPressEvent(QKeyEvent *key);
};#endif // MAINWINDOW_H
MainWindow.cpp
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <stdio.h>
#include <QKeyEvent>MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
}MainWindow::~MainWindow()
{
delete ui;
}
void MainWindow::keyPressEvent(QKeyEvent *e)
{
printf("In keypress event\n");switch(e->key())
{
case Qt::Key_Enter:
case Qt::Key_Return:
printf("Its enter \n");
//ui->comboBox_2->setFocus();
ui->comboBox->focusOutEvent(*e);
break;
case Qt::Key_Shift:
printf("Shift key \n");
ui->comboBox->setFocus();
break;
default:
printf("Not a valid key press\n");
}
QWidget::keyPressEvent(e);
}main.cpp
#include <QtGui/QApplication>
#include "mainwindow.h"int main(int argc, char *argv[])
{
QApplication a(argc, argv);
MainWindow w;
w.show();return a.exec();
}
@I had created the form using creator.with the above code as I mentioned in the previous post it does not take any alpha-numeric keys..
-
When I modify your example so that it is complete and I also set focus to the mainwindow, then it takes alpha-numeric keys. Is it for your comboboxes or for your mainwindow you don't receive the keys? If the example below does not give you the behavior you need, then can you make sure to send a complete example that can be run so that I can see the exact problem you are having?
@
#include <QtGui>class MainWindow : public QMainWindow
{
Q_OBJECTpublic:
MainWindow()
{
setFocusPolicy(Qt::StrongFocus);
QWidget *wid = new QWidget(this);
box1 = new QComboBox(wid);
box2 = new QComboBox(wid);
setCentralWidget(wid);box1->addItem("First");
box1->addItem("Second");box2->addItem("Third");
box2->addItem("Fourth");
QVBoxLayout *layout = new QVBoxLayout(wid);
layout->addWidget(box1);
layout->addWidget(box2);
}
protected:
void keyPressEvent(QKeyEvent *e)
{
qDebug() << "In keypress event";
switch(e->key())
{
case Qt::Key_Enter:
case Qt::Key_Return:
qDebug() << "Its enter";break;
case Qt::Key_Shift:
qDebug() << "Shift key \n";break;
case Qt::Key_A:
qDebug() << "A";
break;
default:
qDebug() << "Not a valid key press\n";
}
QMainWindow::keyPressEvent(e);
}
private:
QComboBox *box1;
QComboBox *box2;
};#include "main.moc"
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
MainWindow w;
w.setFocus();
w.show();
return a.exec();
}
@