Ich habe mit Qt Creator 3.1.2. eine einfache Qt-Widget-Anwendung "cb" erstellt,
abgeleitet von QWidget. Das GUI enthaelt nur einzige ComboBox "comboBox"
mit einem Inhalt von 20 Zeilen.
Ich moechte die ComboBox oeffnen, wenn ich mit der Maus darueberfahre.
Dies funktioniert mit dem Code unten.
Ich moechte nun die ComboBox wieder schliessen, wenn die Maus die geoeffnete
ComboBox wieder verlaesst. Dieses funktioniert nicht. Ich erhalte von der
ComboBox ueberhaupt keine Events mehr.
Das Gleiche passiert auch, wenn ich "QEvent::HoverLeave/Move" anstelle
"QEvent::Leave/Enter" im Code unten verwende.
Erst wenn ich ein ComboBox-Objekt via Mausklick auswaehle oder neben die ComboBox
klicke, schliesst sich die ComboBox wieder. Das moechte ich aber so nicht.
Ich bin dankbar fuer jegliche Tips.
Bernd
cb.h enthaelt
Code: Alles auswählen
#ifndef CB_H
#define CB_H
#include <QWidget>
#include <QDebug>
#include <QEvent>
#include <QMouseEvent>
#include <QMoveEvent>
namespace Ui {
class cb;
}
class cb : public QWidget
{
Q_OBJECT
public:
explicit cb(QWidget *parent = 0);
~cb();
private:
Ui::cb *ui;
private slots:
bool eventFilter(QObject *watched, QEvent *e);
};
#endif // CB_H
Code: Alles auswählen
#include "cb.h"
#include "ui_cb.h"
cb::cb(QWidget *parent) :
QWidget(parent),
ui(new Ui::cb)
{
ui->setupUi(this);
ui->comboBox->installEventFilter(this);
}
cb::~cb()
{
delete ui;
}
bool cb::eventFilter(QObject *watched, QEvent *e)
{
qDebug() << watched << " " << e->type();
if ( (watched == ui->comboBox) && (e->type() == QEvent::Leave) )
{
qDebug() << "Mouse leaves widget's boundaries.";
//ui->comboBox->hidePopup();
return false;
}
if ( (watched == ui->comboBox) && (e->type() == QEvent::Enter) )
{
qDebug() << "Mouse enters widget's boundaries.";
ui->comboBox->showPopup();
return true;
}
if ( (watched == ui->comboBox) && (e->type() == QEvent::InputMethodQuery) )
{
qDebug() << "a key press outside the popup closes the popup";
return false;
}
return QObject::eventFilter(watched, e);
}