Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Install event filter for custom widget hides the widget
Forum Updated to NodeBB v4.3 + New Features

Install event filter for custom widget hides the widget

Scheduled Pinned Locked Moved Unsolved General and Desktop
5 Posts 2 Posters 2.4k Views 2 Watching
  • 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.
  • NewMoonN Offline
    NewMoonN Offline
    NewMoon
    wrote on last edited by
    #1

    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
    0
    • SGaistS Offline
      SGaistS Offline
      SGaist
      Lifetime Qt Champion
      wrote on last edited by
      #2

      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

      NewMoonN 1 Reply Last reply
      1
      • NewMoonN Offline
        NewMoonN Offline
        NewMoon
        wrote on last edited by
        #3
        This post is deleted!
        1 Reply Last reply
        0
        • SGaistS SGaist

          Hi,

          Please show your implementation of the eventFilter method.

          NewMoonN Offline
          NewMoonN Offline
          NewMoon
          wrote on last edited by
          #4

          @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
          0
          • SGaistS Offline
            SGaistS Offline
            SGaist
            Lifetime Qt Champion
            wrote on last edited by SGaist
            #5

            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
            0

            • Login

            • Login or register to search.
            • First post
              Last post
            0
            • Categories
            • Recent
            • Tags
            • Popular
            • Users
            • Groups
            • Search
            • Get Qt Extensions
            • Unsolved