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. How to set focus to a combo box on a key press event?
QtWS25 Last Chance

How to set focus to a combo box on a key press event?

Scheduled Pinned Locked Moved General and Desktop
8 Posts 2 Posters 14.9k Views
  • 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.
  • R Offline
    R Offline
    Revu
    wrote on last edited by
    #1

    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??

    Every fall is to raise higher than Before.

    1 Reply Last reply
    0
    • S Offline
      S Offline
      sigrid
      wrote on last edited by
      #2

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

      }
      @

      1 Reply Last reply
      0
      • R Offline
        R Offline
        Revu
        wrote on last edited by
        #3

        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.

        Every fall is to raise higher than Before.

        1 Reply Last reply
        0
        • S Offline
          S Offline
          sigrid
          wrote on last edited by
          #4

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

          1 Reply Last reply
          0
          • R Offline
            R Offline
            Revu
            wrote on last edited by
            #5

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

            Every fall is to raise higher than Before.

            1 Reply Last reply
            0
            • S Offline
              S Offline
              sigrid
              wrote on last edited by
              #6

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

              1 Reply Last reply
              0
              • R Offline
                R Offline
                Revu
                wrote on last edited by
                #7

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

                Every fall is to raise higher than Before.

                1 Reply Last reply
                0
                • S Offline
                  S Offline
                  sigrid
                  wrote on last edited by
                  #8

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

                  1 Reply Last reply
                  0

                  • Login

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