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 write a closing prompt?
Forum Updated to NodeBB v4.3 + New Features

How to write a closing prompt?

Scheduled Pinned Locked Moved Unsolved General and Desktop
9 Posts 3 Posters 1.0k 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.
  • S Offline
    S Offline
    Source
    wrote on last edited by
    #1

    It takes a long time to close the program.How to write a closing prompt?

    1 Reply Last reply
    0
    • Christian EhrlicherC Offline
      Christian EhrlicherC Offline
      Christian Ehrlicher
      Lifetime Qt Champion
      wrote on last edited by
      #2

      Can you please be a little bit more precise about your problem? Don't understand what you really want. If you want to show an Image during exit you can take a look at QSplashScreen or simply show a QLabel.

      Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
      Visit the Qt Academy at https://academy.qt.io/catalog

      S 1 Reply Last reply
      1
      • Christian EhrlicherC Christian Ehrlicher

        Can you please be a little bit more precise about your problem? Don't understand what you really want. If you want to show an Image during exit you can take a look at QSplashScreen or simply show a QLabel.

        S Offline
        S Offline
        Source
        wrote on last edited by
        #3

        @Christian-Ehrlicher said in How to write a closing prompt?:

        QSplashScreen

        I wrote a program in Qt.It take a little long time during it exits.I want to display a widget with prompts in closeEvent.But it can't.

        aha_1980A 1 Reply Last reply
        0
        • S Source

          @Christian-Ehrlicher said in How to write a closing prompt?:

          QSplashScreen

          I wrote a program in Qt.It take a little long time during it exits.I want to display a widget with prompts in closeEvent.But it can't.

          aha_1980A Offline
          aha_1980A Offline
          aha_1980
          Lifetime Qt Champion
          wrote on last edited by
          #4

          @Source

          Can you show your code?

          Qt has to stay free or it will die.

          S 1 Reply Last reply
          0
          • aha_1980A aha_1980

            @Source

            Can you show your code?

            S Offline
            S Offline
            Source
            wrote on last edited by
            #5

            @aha_1980
            waitwidget.h

            #pragma once

            #include <QWidget.h>

            class WaitWidgetData;
            class WaitWidget : public QWidget
            {
            Q_OBJECT
            public:
            explicit WaitWidget(QWidget *parent = nullptr);
            virtual ~WaitWidget();

            protected:
            void showEvent(QShowEvent *event);

            private:
            WaitWidgetData *d;
            };

            waitwidget.cpp

            #include "waitwidget.h"
            #include "circlewait.h"

            #include <QThread>
            #include <QWaitCondition>

            #include <QVBoxLayout>
            #include <QLabel>

            class WaitWidgetData
            {
            public:
            WaitWidgetData() :
            circleWait(nullptr),
            thread(this)
            {
            }

            CircleWait *circleWait;
            
            class WaitThread : public QThread
            {
            public:
                WaitThread(WaitWidgetData *data) :
                    QThread(),
                    d(data),
                    quit(false)
                {}
                ~WaitThread()
                {
                    stopThread();
                }
            
                void startThread()
                {
                    QMutexLocker locker(&mutex);
                    quit = false;
                    if (!isRunning())
                        start();
                    else
                        cond.wakeOne();
                }
                void stopThread()
                {
                    mutex.lock();
                    quit = true;
                    cond.wakeOne();
                    mutex.unlock();
                    wait();
                }
            
                void run();
            
            protected:
                QMutex mutex;
                QWaitCondition cond;
                bool quit;
            
                WaitWidgetData *d;
            };
            
            WaitThread thread;
            
            friend class WaitThread;
            

            };

            WaitWidget::WaitWidget(QWidget *parent) :
            QWidget(parent),
            d(new WaitWidgetData)
            {
            setWindowFlags(Qt::FramelessWindowHint);
            setWindowFlags(windowFlags() | Qt::WindowStaysOnTopHint);
            setAttribute(Qt::WA_NoSystemBackground, false);
            setAttribute(Qt::WA_TranslucentBackground, true);

            QHBoxLayout *layout = new QHBoxLayout;
            layout->setMargin(0);
            layout->setSpacing(0);
            layout->addStretch();
            d->circleWait = new CircleWait(this);
            d->circleWait->setColor(QColor(46, 194, 163));
            d->circleWait->setClockwise(false);
            layout->addWidget(d->circleWait);
            QLabel *labelWait = new QLabel(tr("The program is shutting down, please wait..."), this);
            QFont font = labelWait->font();
            font.setBold(true);
            font.setPointSize(20);
            labelWait->setFont(font);
            layout->addWidget(labelWait);
            layout->addStretch();
            
            QVBoxLayout *mainLayout = new QVBoxLayout(this);
            mainLayout->setContentsMargins(10, 5, 10, 5);
            mainLayout->setSpacing(0);
            mainLayout->addStretch();
            mainLayout->addLayout(layout);
            mainLayout->addStretch();
            

            }
            WaitWidget::~WaitWidget()
            {
            delete d;
            d = nullptr;
            }

            void WaitWidget::showEvent(QShowEvent *event)
            {
            QWidget::showEvent(event);

            d->thread.startThread();
            

            }

            //---------------------------------------------------------------------
            void WaitWidgetData::WaitThread::run()
            {
            while (!quit)
            {
            d->circleWait->update();

                QThread::msleep(500);
            }
            

            }

            closeEvent

            void MainWindow::closeEvent(QCloseEvent *event)
            {
            QApplication::setOverrideCursor(QCursor(Qt::WaitCursor));

            WaitWidget *waitWidget = new WaitWidget;
            waitWidget->show();
            
            ModuleManager::removeAll();
            
            QApplication::restoreOverrideCursor();
            
            waitWidget->close();
            waitWidget->deleteLater();
            
            event->accept();
            

            }

            1 Reply Last reply
            0
            • Christian EhrlicherC Offline
              Christian EhrlicherC Offline
              Christian Ehrlicher
              Lifetime Qt Champion
              wrote on last edited by
              #6

              @Source said in How to write a closing prompt?:

              d->circleWait = new CircleWait(this);
              ...
              void WaitWidgetData::WaitThread::run()
              {
              d->circleWait->update();

              You're updating a Widget from within a thread which is not the main gui thread - this will create problems sooner or later. See QThread documentation on how to pass data between threads.
              And since your thread only calls update() which does not more than creating an update event (and therefore the repainting is delayed) the thread is useless.

              Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
              Visit the Qt Academy at https://academy.qt.io/catalog

              S 1 Reply Last reply
              1
              • Christian EhrlicherC Christian Ehrlicher

                @Source said in How to write a closing prompt?:

                d->circleWait = new CircleWait(this);
                ...
                void WaitWidgetData::WaitThread::run()
                {
                d->circleWait->update();

                You're updating a Widget from within a thread which is not the main gui thread - this will create problems sooner or later. See QThread documentation on how to pass data between threads.
                And since your thread only calls update() which does not more than creating an update event (and therefore the repainting is delayed) the thread is useless.

                S Offline
                S Offline
                Source
                wrote on last edited by
                #7

                @Christian-Ehrlicher
                The thread is not the issue.The issue is how I can display the WaitWidget on top of the MainWindow. I write:
                setWindowFlags(windowFlags() | Qt::WindowStaysOnTopHint);
                But is doesn't work.

                1 Reply Last reply
                0
                • Christian EhrlicherC Offline
                  Christian EhrlicherC Offline
                  Christian Ehrlicher
                  Lifetime Qt Champion
                  wrote on last edited by
                  #8

                  You have a thread issue for sure.
                  Regarding the widget not showing up - there is not eventloop inbetween the creation and (defered) deletion of WaitWidget - so Qt is unable to process the events and can't show anything.

                  Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
                  Visit the Qt Academy at https://academy.qt.io/catalog

                  S 1 Reply Last reply
                  1
                  • Christian EhrlicherC Christian Ehrlicher

                    You have a thread issue for sure.
                    Regarding the widget not showing up - there is not eventloop inbetween the creation and (defered) deletion of WaitWidget - so Qt is unable to process the events and can't show anything.

                    S Offline
                    S Offline
                    Source
                    wrote on last edited by
                    #9

                    @Christian-Ehrlicher
                    Can you show me an example? Thank you very much!

                    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