Qt Forum

    • Login
    • Search
    • Categories
    • Recent
    • Tags
    • Popular
    • Users
    • Groups
    • Search
    • Unsolved

    Update: Forum Guidelines & Code of Conduct

    Unsolved Install event filter for custom widget hides the widget

    General and Desktop
    2
    5
    1952
    Loading More Posts
    • Oldest to Newest
    • Newest to Oldest
    • Most Votes
    Reply
    • Reply as topic
    Log in to reply
    This topic has been deleted. Only users with topic management privileges can see it.
    • NewMoon
      NewMoon last edited by

      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.

      1 Reply Last reply Reply Quote 0
      • SGaist
        SGaist Lifetime Qt Champion last edited by

        Hi,

        Please show your implementation of the eventFilter method.

        Interested in AI ? www.idiap.ch
        Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

        NewMoon 1 Reply Last reply Reply Quote 1
        • NewMoon
          NewMoon last edited by

          This post is deleted!
          1 Reply Last reply Reply Quote 0
          • NewMoon
            NewMoon @SGaist last edited by

            @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);
            }
            
            1 Reply Last reply Reply Quote 0
            • SGaist
              SGaist Lifetime Qt Champion last edited by SGaist

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

              Interested in AI ? www.idiap.ch
              Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

              1 Reply Last reply Reply Quote 0
              • First post
                Last post