[Interest] Whole edit window closes.

william.crocker at analog.com william.crocker at analog.com
Thu Mar 14 11:50:42 CET 2019


>>
>> I have a QTableView.
>> I use a QStyledItemDelegate on each cell.
>> The delegate opens a custom, modal editor on double click.
>> The editor is based on a QDialog and contains a number of widgets,
>> one of which is a QComboBox.
>> When I press <Return> in the text entry area of the QComboBox,
>> the whole edit window closes, but I am not done "editing". Arg.
>>

>
> Hi,
> I had this kind of issue back in the Qt 4.8 days
>
>>From what I remember, the issue is that the QComboBox does not consume
> the "return pressed" event.
> So the event propagates to the QDialog which handles the event by
> "pressing" the default button and closing itself.
> The only solution I found was to filter the "return pressed" event,
> using an event filter (`QObject::installEventFilter()`)
> so that it does not reach the QDialog.
>

Thanks.

That was it.
The following code worked for me.
( In defense of Qt, I am still using 5.7 )

Bill

----------------

class ComboEventFilter : public QObject {
     Q_OBJECT
     typedef QObject BaseClass;
   public:
     ComboEventFilter( QComboBox *cp ) : BaseClass(cp), boxp(cp) { }
   protected:
     bool eventFilter( QObject *op, QEvent *ep ) {
         if( ep->type() == QEvent::KeyPress ) {
             QKeyEvent *kep = static_cast<QKeyEvent*>(ep);
             if( kep->key() == Qt::Key_Enter || kep->key() == Qt::Key_Return ) {
                 boxp->lineEdit()->returnPressed();
                 // Stop propagation of event. Was reaching the top level dialog
                 // and pressing the default button and closing it.
                 return true;
             } else {
                 return QObject::eventFilter(op,ep);
             }
         } else {
             return QObject::eventFilter(op,ep);
         }
     }
     QComboBox *boxp;
};

class MyComboBox2 : public QComboBox {
     Q_OBJECT
     typedef QComboBox BaseClass;
   public:
     MyComboBox2() {
         ComboEventFilter *efp = new ComboEventFilter(this);
         installEventFilter(efp);
     }
};



More information about the Interest mailing list