Event Handling for QComboBox
-
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_OBJECTpublic:
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.
-
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
-
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'
-
But after including @QLineEdit@
its coplied with no error but place holder is not displaying?
-
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.