Seite 1 von 1

QComboBox-Events

Verfasst: 15. September 2014 07:09
von cubby
Hallo Qt-User

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
und cb.cpp

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

Re: QComboBox-Events

Verfasst: 15. September 2014 19:48
von Christian81
Das Dropdown-Menu ist ein eigenes Widget , deshalb passt die Abfrage 'watched == ui->comboBox' nicht. Wie das genau aufgebaut ist siehst Du nur im Qt Sourcecode.

Re: QComboBox-Events

Verfasst: 16. September 2014 06:58
von cubby
Hallo Christian81,

vielen Dank fuer Deinen Tip. Ich werde nun mal den Qt Sourcecode nach
diesem Dropdown-Menu durchforsten und hoffe, dass ich das Widget finde.

Viele Gruesse,
qtuserbefu