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