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();

    }
    @



  • Hi..Thanks for the response.
    Am using creator to design my forms.I had set them with strong focus.But still no key press events are accepted.



  • What happens if you call setFocus() on your forms like I do for the widget in the example above?



  • Apart from enter,shift and other modifiers and function keys it is not accepting any other key.



  • Did the example I pasted above work for you? If it does, can you provide an example that shows the problem you are having?



  • @
    MainWindow.h
    #ifndef MAINWINDOW_H
    #define MAINWINDOW_H

    #include <QMainWindow>

    namespace Ui {
    class MainWindow;
    }

    class MainWindow : public QMainWindow
    {
    Q_OBJECT

    public:
    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&#40;&#41;;
    

    }
    @

    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_OBJECT

    public:
    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();
    }
    @


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.