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. Long press button with progress dialog
Forum Updated to NodeBB v4.3 + New Features

Long press button with progress dialog

Scheduled Pinned Locked Moved Solved General and Desktop
8 Posts 4 Posters 865 Views 1 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.
  • W Offline
    W Offline
    wasawi2
    wrote on last edited by
    #1

    Dear all,

    I implemented a long press button. So if the user holds pressed for less than 2 sec. nothing happens. Holding for more than 2sec. will trigger an action on timer timeout. So far so good.

    Now I want to have progress dialog showing the growing bar before the 2sec. happen.

    The problem is that when i implement the dialog, for some reason, my long press button looses focus and Qt automatically throws a release event on it. Which breaks the functionality i intend to implement.

    Is there any way to prevent Qt from throwing that release event?

    Or how would you implement this simple functionality?

    Here is my current code:

        m_longClickTimer.setSingleShot(true);
        connect(ui.button_auto, &QPushButton::pressed, this, &MainWindow::StartLongPress);
        connect(ui.button_auto, &QPushButton::released, this, &MainWindow::CheckLongPress);
        connect(&m_longClickTimer, &QTimer::timeout, this, &MainWindow::LongPressTimeOut);
    
    
    void MainWindow::StartLongPress()
    {
        m_longClickTimer.start(2000);
        m_progressdialog_auto = new QProgressDialog(ui.button_auto);
        m_progressdialog_auto->setModal(false);
        m_progressdialog_auto->show();
    }
    
    // BUG HERE
    // this method is triggered automatically if i open a diglog
    void MainWindow::CheckLongPress()
    {
        if (m_longClickTimer.isActive()) {
            qDebug() << "CheckLongPress: Released too early";
            m_longClickTimer.stop();
            m_progressdialog_auto->deleteLater();
        }
    }
    
    void MainWindow::LongPressTimeOut()
    {
        qDebug() << "LongPress: My next action here";
    }
    
    JonBJ 1 Reply Last reply
    0
    • W wasawi2

      @JonB Thank you very much.

      Yes, what you explain is exactly what happens. I have tried with QDialog, QMainWindow and QProgressDialog, all behave in the same way, the release event is triggered when a new window opens...

      So it seems I will have to find a workaround. I'm quite surprised because this kind of functionality is not too rare.

      Any other ideas will be appreciated.

      Pl45m4P Offline
      Pl45m4P Offline
      Pl45m4
      wrote on last edited by Pl45m4
      #4

      @wasawi2 said in Long press button with progress dialog:

      Any other ideas will be appreciated.

      Try Qt::WindowDoesNotAcceptFocus window flag on your progressBar widget.
      Or Qt::Tooltip. AFAIK tooltips dont steal focus. Might be a bit hacky. Would prefer the first one if it works.

      Edit:
      There is also this

      setAttribute(Qt::WA_ShowWithoutActivating)

      • https://doc.qt.io/qt-6/qt.html#WidgetAttribute-enum

      which might be the best solution if it does what you expect


      If debugging is the process of removing software bugs, then programming must be the process of putting them in.

      ~E. W. Dijkstra

      Christian EhrlicherC W 2 Replies Last reply
      2
      • W wasawi2

        Dear all,

        I implemented a long press button. So if the user holds pressed for less than 2 sec. nothing happens. Holding for more than 2sec. will trigger an action on timer timeout. So far so good.

        Now I want to have progress dialog showing the growing bar before the 2sec. happen.

        The problem is that when i implement the dialog, for some reason, my long press button looses focus and Qt automatically throws a release event on it. Which breaks the functionality i intend to implement.

        Is there any way to prevent Qt from throwing that release event?

        Or how would you implement this simple functionality?

        Here is my current code:

            m_longClickTimer.setSingleShot(true);
            connect(ui.button_auto, &QPushButton::pressed, this, &MainWindow::StartLongPress);
            connect(ui.button_auto, &QPushButton::released, this, &MainWindow::CheckLongPress);
            connect(&m_longClickTimer, &QTimer::timeout, this, &MainWindow::LongPressTimeOut);
        
        
        void MainWindow::StartLongPress()
        {
            m_longClickTimer.start(2000);
            m_progressdialog_auto = new QProgressDialog(ui.button_auto);
            m_progressdialog_auto->setModal(false);
            m_progressdialog_auto->show();
        }
        
        // BUG HERE
        // this method is triggered automatically if i open a diglog
        void MainWindow::CheckLongPress()
        {
            if (m_longClickTimer.isActive()) {
                qDebug() << "CheckLongPress: Released too early";
                m_longClickTimer.stop();
                m_progressdialog_auto->deleteLater();
            }
        }
        
        void MainWindow::LongPressTimeOut()
        {
            qDebug() << "LongPress: My next action here";
        }
        
        JonBJ Offline
        JonBJ Offline
        JonB
        wrote on last edited by JonB
        #2

        @wasawi2
        To be clear: you start a key press on a focussed button. That open a modeless dialog and shows it. Doubtless that causes some kind of focus change and that causes button-press-termination in the original window, with a release event. Right? I don't know but either this has to happen or maybe you can do something about the new dialog now getting/accepting focus so that does not occur?

        P.S.
        Try your code with a plain QDialog instead of a QProgressDialog, and also with a plain QWidget. Any difference? Then we know whether it is anything to do with being a QProgressDialog.

        W 1 Reply Last reply
        0
        • JonBJ JonB

          @wasawi2
          To be clear: you start a key press on a focussed button. That open a modeless dialog and shows it. Doubtless that causes some kind of focus change and that causes button-press-termination in the original window, with a release event. Right? I don't know but either this has to happen or maybe you can do something about the new dialog now getting/accepting focus so that does not occur?

          P.S.
          Try your code with a plain QDialog instead of a QProgressDialog, and also with a plain QWidget. Any difference? Then we know whether it is anything to do with being a QProgressDialog.

          W Offline
          W Offline
          wasawi2
          wrote on last edited by
          #3

          @JonB Thank you very much.

          Yes, what you explain is exactly what happens. I have tried with QDialog, QMainWindow and QProgressDialog, all behave in the same way, the release event is triggered when a new window opens...

          So it seems I will have to find a workaround. I'm quite surprised because this kind of functionality is not too rare.

          Any other ideas will be appreciated.

          Pl45m4P 1 Reply Last reply
          0
          • W wasawi2

            @JonB Thank you very much.

            Yes, what you explain is exactly what happens. I have tried with QDialog, QMainWindow and QProgressDialog, all behave in the same way, the release event is triggered when a new window opens...

            So it seems I will have to find a workaround. I'm quite surprised because this kind of functionality is not too rare.

            Any other ideas will be appreciated.

            Pl45m4P Offline
            Pl45m4P Offline
            Pl45m4
            wrote on last edited by Pl45m4
            #4

            @wasawi2 said in Long press button with progress dialog:

            Any other ideas will be appreciated.

            Try Qt::WindowDoesNotAcceptFocus window flag on your progressBar widget.
            Or Qt::Tooltip. AFAIK tooltips dont steal focus. Might be a bit hacky. Would prefer the first one if it works.

            Edit:
            There is also this

            setAttribute(Qt::WA_ShowWithoutActivating)

            • https://doc.qt.io/qt-6/qt.html#WidgetAttribute-enum

            which might be the best solution if it does what you expect


            If debugging is the process of removing software bugs, then programming must be the process of putting them in.

            ~E. W. Dijkstra

            Christian EhrlicherC W 2 Replies Last reply
            2
            • Pl45m4P Pl45m4

              @wasawi2 said in Long press button with progress dialog:

              Any other ideas will be appreciated.

              Try Qt::WindowDoesNotAcceptFocus window flag on your progressBar widget.
              Or Qt::Tooltip. AFAIK tooltips dont steal focus. Might be a bit hacky. Would prefer the first one if it works.

              Edit:
              There is also this

              setAttribute(Qt::WA_ShowWithoutActivating)

              • https://doc.qt.io/qt-6/qt.html#WidgetAttribute-enum

              which might be the best solution if it does what you expect

              Christian EhrlicherC Offline
              Christian EhrlicherC Offline
              Christian Ehrlicher
              Lifetime Qt Champion
              wrote on last edited by
              #5

              I would use something not that intrusive. Maybe color the button over time to show the progress.

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

              1 Reply Last reply
              2
              • Pl45m4P Pl45m4

                @wasawi2 said in Long press button with progress dialog:

                Any other ideas will be appreciated.

                Try Qt::WindowDoesNotAcceptFocus window flag on your progressBar widget.
                Or Qt::Tooltip. AFAIK tooltips dont steal focus. Might be a bit hacky. Would prefer the first one if it works.

                Edit:
                There is also this

                setAttribute(Qt::WA_ShowWithoutActivating)

                • https://doc.qt.io/qt-6/qt.html#WidgetAttribute-enum

                which might be the best solution if it does what you expect

                W Offline
                W Offline
                wasawi2
                wrote on last edited by
                #6

                @Pl45m4 said in Long press button with progress dialog:

                setAttribute(Qt::WA_ShowWithoutActivating)

                @Pl45m4 Thank you so much! that made the trick!

                @Christian-Ehrlicher Thank you for your suggestion. Unfortunately in my case i use a touch screen and the buttons have to be the size of a finger, so i can't display anything in the button itself.

                Christian EhrlicherC 1 Reply Last reply
                1
                • W wasawi2 has marked this topic as solved on
                • W wasawi2

                  @Pl45m4 said in Long press button with progress dialog:

                  setAttribute(Qt::WA_ShowWithoutActivating)

                  @Pl45m4 Thank you so much! that made the trick!

                  @Christian-Ehrlicher Thank you for your suggestion. Unfortunately in my case i use a touch screen and the buttons have to be the size of a finger, so i can't display anything in the button itself.

                  Christian EhrlicherC Offline
                  Christian EhrlicherC Offline
                  Christian Ehrlicher
                  Lifetime Qt Champion
                  wrote on last edited by
                  #7

                  @wasawi2 said in Long press button with progress dialog:

                  buttons have to be the size of a finger, so i can't display anything in the button itself.

                  How does this prevent coloring the button?

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

                  W 1 Reply Last reply
                  0
                  • Christian EhrlicherC Christian Ehrlicher

                    @wasawi2 said in Long press button with progress dialog:

                    buttons have to be the size of a finger, so i can't display anything in the button itself.

                    How does this prevent coloring the button?

                    W Offline
                    W Offline
                    wasawi2
                    wrote on last edited by
                    #8

                    @Christian-Ehrlicher the user will not see well the button because they will have the finger on top of it blocking visibility.

                    1 Reply Last reply
                    2

                    • Login

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