Install event filter for custom widget hides the widget



  • Hello All,

    I have a custom widget (MyCustomWidget), and inside MyCustomWidget, I have added a grid layout with some child widgets in it. Then I have added this MyCustomWidget in another QVBoxLayout.

    I want to enable and disable the MyCustomWidget using mouse click. So I used install event filter for MyCustomWidget and the child widgets. If I use this install event filter then MyCustomWidget is not showing at all. If I remove the install event filter then the MyCustomWidget is showing.

    Here's my code sample.

    ```
    MyCustomWidget  *enableDisableWidget = new MyCustomWidget(this);
    enableDisableWidget ->setObjectName("EnableOrDisableCusotmWidget");
    enableDisableWidget ->installEventFilter(this);
    
    MyCustomChildLabel  * myLabel  =  new MyCustomChildLabel (enableDisableWidget );
    myLabel->setObjectName("CustomLabe1l");
    myLabel->setText("Label1");
    myLabel->setStyleSheet("color: black");
    myLabel->installEventFilter(this);
    
    MyCustomChildLabel  * myLabel1  = new MyCustomChildLabel  (enableDisableWidget);
    myLabel1 ->setObjectName("CusotomLabel2");
    myLabel1 ->setStyleSheet("color: black");
    myLabel1 ->installEventFilter(this);
    
    QGridLayout *lay = new QGridLayout;
    lay ->addWidget(myLabel, 0, 0,1,1);
    lay ->addWidget(myLabel1,0, 1,1,1);
    
    enableDisableWidget->setLayout(lay);
    

    Then I add this MyCusotmWidget to another QVBoxLayout where other QWidgets gets added to the layout.

    QVBoxLayout *verticalLayout = new QVBoxLayout;
    verticalLayout ->addWidget(new QPushButton("OK", this),1,Qt::AlignLeft);
    verticalLayout ->addWidget(new QLineEdit(this), 1, Qt::AlignLefr;);
    verticalLayout ->addWidget(enableDisableWidget,10,Qt::AlignLeft);
    
    this->setLayout(verticalLayout);
    

    The problem is, here MyCustomwidget is not shown in the verticalLayout. I can see the QPushButton and the QLineEdit, but not the MyCustomWidget. If I Remove the Install event filter for the MyCustomWidgets then it is shown. Please give me your suggestions.


  • Lifetime Qt Champion

    Hi,

    Please show your implementation of the eventFilter method.



  • This post is deleted!


  • @SGaist

    Here's my event filter function.

    bool MyClass::eventFilter(QObject *obj, QEvent *evt)
    {
        if(obj->parent() != NULL && obj->parent() == enableDisableWidget ) 
        {
            if(evt->type() == QEvent::MouseButtonPress)
            {
            if(enableDisableWidget ->isEnabled())
                enableDisableWidget->setEnabled(false);
            else 
                enableDisableWidget->setEnabled(true);
            }
            return false;
        }
    
        return QWidget::eventFilter(obj,evt);
    }
    

  • Lifetime Qt Champion

    Since you are just changing the enable state, why aren't you letting the base class handle all the other events ?


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.