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. How to override functionality for widgets hiding when losing mouse focus?
Qt 6.11 is out! See what's new in the release blog

How to override functionality for widgets hiding when losing mouse focus?

Scheduled Pinned Locked Moved Unsolved General and Desktop
5 Posts 3 Posters 5.0k 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.
  • Q Offline
    Q Offline
    Qtstarter121
    wrote on last edited by Qtstarter121
    #1

    I need some kind of way to replace the existing functionality where a widget will hide automatically when a click occurs outside of it ( loses focus) with my own code to do something else instead.

    1 Reply Last reply
    0
    • ? Offline
      ? Offline
      A Former User
      wrote on last edited by
      #2

      Hi, and welcome to the Qt forum.

      widget will hide automatically when [...] loses focus [...]

      You can override QWidget::focusOutEvent(QFocusEvent *event), see: focusOutEvent().

      class MyPlainTextEdit : public QPlainTextEdit
      {
          Q_OBJECT
      public:
          explicit MyPlainTextEdit(QWidget *parent = nullptr);
      protected:
          virtual void focusOutEvent(QFocusEvent *) override { hide(); }
      };
      
      1 Reply Last reply
      1
      • Q Offline
        Q Offline
        Qtstarter121
        wrote on last edited by
        #3

        Thank you. The problem is that I need to implement this in the eventFilter of my QMainWindow to filter for the correct object. I cannot implement this function for the object I need since there's no class implementation for that object, it's all part of my QMainWindow. How would I do this?

        1 Reply Last reply
        0
        • ? Offline
          ? Offline
          A Former User
          wrote on last edited by A Former User
          #4

          Let's assume you have your MainWindow class and it contains the widget plainTextEdit.

          myeventfilter.h

          #ifndef MYEVENTFILTER_H
          #define MYEVENTFILTER_H
          
          #include <QObject>
          
          class MyEventFilter : public QObject
          {
              Q_OBJECT
          public:
              MyEventFilter(QObject *parent = nullptr);
          protected:
              virtual bool eventFilter(QObject *watched, QEvent *event) override;
          };
          
          #endif // MYEVENTFILTER_H
          

          myeventfilter.cpp

          #include "myeventfilter.h"
          #include <QEvent>
          #include <QWidget>
          
          MyEventFilter::MyEventFilter(QObject *parent)
              : QObject(parent)
          {
          }
          
          bool MyEventFilter::eventFilter(QObject *watched, QEvent *event)
          {
              if (event->type() == QEvent::FocusOut) {
                  auto widget = dynamic_cast<QWidget *>(watched);
                  widget->hide();
                  return true;
              } else {
                  return QObject::eventFilter(watched, event);
              }
          }
          

          mainwindow.cpp

          #include "mainwindow.h"
          #include "ui_mainwindow.h"
          #include "myeventfilter.h"
          
          MainWindow::MainWindow(QWidget *parent)
              : QMainWindow(parent)
              , ui(new Ui::MainWindow)
          {
              ui->setupUi(this);
              auto myEventFilter = new MyEventFilter(this);
              ui->plainTextEdit->installEventFilter(myEventFilter);
          }
          
          // ...
          
          1 Reply Last reply
          1
          • Vinod KuntojiV Offline
            Vinod KuntojiV Offline
            Vinod Kuntoji
            wrote on last edited by
            #5

            @Qtstarter121 ,

            Hi,

            You can use,
            yourWidget->setWindowFlags(Qt::Popup);
            this will hide your widget when you click outside of it.

            C++, Qt, Qt Quick Developer,
            PthinkS, Bangalore

            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