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. Event Handling for QComboBox

Event Handling for QComboBox

Scheduled Pinned Locked Moved General and Desktop
7 Posts 3 Posters 6.4k Views 1 Watching
  • 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.
  • P Offline
    P Offline
    psbhardwaj09gmail.com
    wrote on last edited by
    #1

    Hi All,

    I have an application in which i want to handle mousePress event for QComboBox.
    My Combo box is edittable. & I wants that when i click on combo box its default text is clear.
    my code is as following:-

    .h file
    @
    #ifndef MAINWINDOW_H
    #define MAINWINDOW_H

    #include <QtGui/QMainWindow>
    #include <QTextEdit>
    #include <QComboBox>

    class MainWindow : public QMainWindow
    {
    Q_OBJECT

    public:
    MainWindow(QWidget *parent = 0);
    ~MainWindow();

    protected:
    bool eventFilter(QObject *obj, QEvent *ev);

    public:
    QTextEdit textEdit;
    QComboBox
    mpgroupCombo;
    };

    #endif // MAINWINDOW_H
    @

    & .cpp file is
    @
    #include "mainwindow.h"
    #include <QDebug>
    #include <QEvent>
    #include <QKeyEvent>
    #include <QMouseEvent>

    MainWindow::MainWindow(QWidget *parent)
    : QMainWindow(parent)
    {

    //textEdit = new QTextEdit;
    
    mpgroupCombo = new QComboBox();
    mpgroupCombo->setEditable(true);
    mpgroupCombo->setEditText(tr("Type or Select Contact"));
    setCentralWidget(mpgroupCombo);
    
    mpgroupCombo->installEventFilter(this);
    

    }

    MainWindow::~MainWindow()
    {

    }

    bool MainWindow::eventFilter(QObject *obj, QEvent *event)
    {
    if (obj == mpgroupCombo) {
    if (event->type() == QEvent::MouseButtonPress || event->type() == QEvent::KeyPress )
    {
    QKeyEvent keyEvent =(QKeyEvent)(event);
    qDebug() << "Ate key press";
    // textEdit->setText("keyEvent->key()");
    return true;
    }
    else
    {
    return false;
    }
    } else {
    // pass the event on to the parent class
    return QMainWindow::eventFilter(obj, event);
    }
    }

    @

    here problem is that when i clicked on the text part of QCombobox, nothing is print whereas when i click on the dropdown button then text is printed.
    So how can i print text when i click on text?

    Can any one suggest me for that.

    Pardeep Sharma

    1 Reply Last reply
    0
    • B Offline
      B Offline
      b1gsnak3
      wrote on last edited by
      #2

      You are trying to do something that QComboBox already has... I think... You are trying to add a placeholder text to the comboBox... Try using:

      @
      mygroupCombo->lineEdit()->setPlaceHolderText("Some text");
      @

      Sorry first time I wrote it wrong..

      1 Reply Last reply
      0
      • P Offline
        P Offline
        psbhardwaj09gmail.com
        wrote on last edited by
        #3

        Hi b1gsnak3,
        when i try
        @mygroupCombo->lineEdit()->setPlaceHolderText("Some text");@

        the following errors occurs

        bq. error: C2027: use of undefined type 'QLineEdit'

        bq. error: C2227: left of '->setPlaceHolderText' must point to class/struct/union/generic type

        Pardeep Sharma

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

          Try to include QLineEdit.h

          1 Reply Last reply
          0
          • P Offline
            P Offline
            psbhardwaj09gmail.com
            wrote on last edited by
            #5

            Hi Sam,

            Thank you for your reply. But After include @qlineedit.h@ there is again another error

            bq. error: C2039: 'setPlaceHolderText' : is not a member of 'QLineEdit'

            Pardeep Sharma

            1 Reply Last reply
            0
            • P Offline
              P Offline
              psbhardwaj09gmail.com
              wrote on last edited by
              #6

              But after including @QLineEdit@

              its coplied with no error but place holder is not displaying?

              Pardeep Sharma

              1 Reply Last reply
              0
              • S Offline
                S Offline
                Sam
                wrote on last edited by
                #7

                It wont be displayed as your comboBox->lineEdit already has the focus.

                Try to add the following for test

                @MainWindow::MainWindow(QWidget *parent)
                : QMainWindow(parent)
                {
                //textEdit = new QTextEdit;

                mpgroupCombo = new QComboBox();
                mpgroupCombo->setEditable(true);
                
                QPushButton *mButton = new QPushButton("Press Me");
                mpgroupCombo->lineEdit()->setPlaceholderText("Hellooo");
                
                QHBoxLayout *layout = new QHBoxLayout;
                layout->addWidget(mpgroupCombo);
                layout->addWidget(mButton);
                
                QWidget *widget = new QWidget(this);
                widget->setLayout(layout);
                
                setCentralWidget(widget);
                
                mpgroupCombo->installEventFilter(this);
                

                }@

                Click on the button to change to focus.

                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