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. Minimising parent widget window without minimising its children
Forum Updated to NodeBB v4.3 + New Features

Minimising parent widget window without minimising its children

Scheduled Pinned Locked Moved Unsolved General and Desktop
3 Posts 2 Posters 312 Views
  • 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.
  • B Offline
    B Offline
    booksword
    wrote on last edited by
    #1

    Background

    I'm developing a trading tool for our traders, and the tool contains a summary QTableWidget panel shows general info and traders can click the button in related row to open a order panel. The trader wants that the order panel can be shown always regardless of the summary panel is minimised or hide (except closed, in which case all order panels should be closed as well).

    Problem

    But the difficulty here is the order panels always gets minimised when I minimise the summary panel.

    Simplified code

    Below is a simplified demo to illustrate the problem

    #include <QtWidgets/QWidget>
    #include <QtWidgets/QPushButton>
    #include <QtWidgets/QLabel>
    #include <QtWidgets/QApplication>
    #include <QEvent>
    #include <QtWidgets/QVBoxLayout>
    
    class ChildWidget : public QWidget
    {
    public:
        ChildWidget(QWidget* parent = nullptr) : QWidget(parent)
        {
            setWindowTitle("Child Widget");
            setMinimumWidth(500);
            auto layout = new QVBoxLayout(this);
            auto label = new QLabel("Child Widget", this);
            layout->addWidget(label);
        }
    /*
        bool event(QEvent* myevent) override
        {
            if (myevent->type() == QEvent::WindowStateChange)
            {
                if (windowState() & Qt::WindowMinimized)
                {
                    setWindowState(windowState() & ~Qt::WindowMinimized);
                }
            }
            return QWidget::event(myevent);
        }
    */
    };
    
    class MainWindow : public QWidget
    {
    public:
        MainWindow(QWidget* parent = nullptr) : QWidget(parent)
        {
            setWindowTitle("Main Window");
            setMinimumWidth(500);
            auto layout = new QVBoxLayout(this);
            auto button = new QPushButton("Open Child", this);
            layout->addWidget(button);
            connect(button, &QPushButton::clicked, this, &MainWindow::show_child);
        }
    
    public:
        void show_child()
        {
            auto child = new ChildWidget(this);
            child->setWindowFlag(Qt::Window);
            child->show();
        }
    };
    
    int main(int argc, char* argv[])
    {
        QApplication app(argc, argv);
        MainWindow window;
        window.show();
        return app.exec();
    }
    

    I did some googling and asked github copilot, which suggest a solution of block minimising event as the commented part in ChildWidget, but unfortunately this doesn't work. Even it blocks the minimising button on the order panel, i.e. the order panels cannot be minimised with itself, but still, when the summary panel gets minimised, all order panels gets minimised as well.

    The only way I have found now is to unlink the parent-child relationship of the summary panel and order panels, i.e., when creating ChildWidget, we use nullptr for its parent param (instead of this). But doing this, closing parent panel MainWidget doesn't close child panel. I will need to re-implement all WindowState related event handling other than minimising.

    So could someone have better way of implementing this?

    Thanks in advance!

    jsulmJ 1 Reply Last reply
    0
    • B booksword

      Background

      I'm developing a trading tool for our traders, and the tool contains a summary QTableWidget panel shows general info and traders can click the button in related row to open a order panel. The trader wants that the order panel can be shown always regardless of the summary panel is minimised or hide (except closed, in which case all order panels should be closed as well).

      Problem

      But the difficulty here is the order panels always gets minimised when I minimise the summary panel.

      Simplified code

      Below is a simplified demo to illustrate the problem

      #include <QtWidgets/QWidget>
      #include <QtWidgets/QPushButton>
      #include <QtWidgets/QLabel>
      #include <QtWidgets/QApplication>
      #include <QEvent>
      #include <QtWidgets/QVBoxLayout>
      
      class ChildWidget : public QWidget
      {
      public:
          ChildWidget(QWidget* parent = nullptr) : QWidget(parent)
          {
              setWindowTitle("Child Widget");
              setMinimumWidth(500);
              auto layout = new QVBoxLayout(this);
              auto label = new QLabel("Child Widget", this);
              layout->addWidget(label);
          }
      /*
          bool event(QEvent* myevent) override
          {
              if (myevent->type() == QEvent::WindowStateChange)
              {
                  if (windowState() & Qt::WindowMinimized)
                  {
                      setWindowState(windowState() & ~Qt::WindowMinimized);
                  }
              }
              return QWidget::event(myevent);
          }
      */
      };
      
      class MainWindow : public QWidget
      {
      public:
          MainWindow(QWidget* parent = nullptr) : QWidget(parent)
          {
              setWindowTitle("Main Window");
              setMinimumWidth(500);
              auto layout = new QVBoxLayout(this);
              auto button = new QPushButton("Open Child", this);
              layout->addWidget(button);
              connect(button, &QPushButton::clicked, this, &MainWindow::show_child);
          }
      
      public:
          void show_child()
          {
              auto child = new ChildWidget(this);
              child->setWindowFlag(Qt::Window);
              child->show();
          }
      };
      
      int main(int argc, char* argv[])
      {
          QApplication app(argc, argv);
          MainWindow window;
          window.show();
          return app.exec();
      }
      

      I did some googling and asked github copilot, which suggest a solution of block minimising event as the commented part in ChildWidget, but unfortunately this doesn't work. Even it blocks the minimising button on the order panel, i.e. the order panels cannot be minimised with itself, but still, when the summary panel gets minimised, all order panels gets minimised as well.

      The only way I have found now is to unlink the parent-child relationship of the summary panel and order panels, i.e., when creating ChildWidget, we use nullptr for its parent param (instead of this). But doing this, closing parent panel MainWidget doesn't close child panel. I will need to re-implement all WindowState related event handling other than minimising.

      So could someone have better way of implementing this?

      Thanks in advance!

      jsulmJ Offline
      jsulmJ Offline
      jsulm
      Lifetime Qt Champion
      wrote on last edited by
      #2

      @booksword said in Minimising parent widget window without minimising its children:

      closing parent panel MainWidget doesn't close child panel

      You can override https://doc.qt.io/qt-6/qwidget.html#closeEvent in MainWindow and close ChildWidget there.

      https://forum.qt.io/topic/113070/qt-code-of-conduct

      B 1 Reply Last reply
      1
      • jsulmJ jsulm

        @booksword said in Minimising parent widget window without minimising its children:

        closing parent panel MainWidget doesn't close child panel

        You can override https://doc.qt.io/qt-6/qwidget.html#closeEvent in MainWindow and close ChildWidget there.

        B Offline
        B Offline
        booksword
        wrote on last edited by
        #3

        @jsulm Thanks for answering.

        Yes, this is my current solution, but I'm wondering if there is more elegant method to do so. Since just because I don't want to make child minimised with parent, I'll need to overwrite closeEvent and add all other actions one can do with a window (close, maximise etc) seems not reasonable.

        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