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 auto click pushButton in ui after running the mainwindow ?
Forum Updated to NodeBB v4.3 + New Features

How to auto click pushButton in ui after running the mainwindow ?

Scheduled Pinned Locked Moved Solved General and Desktop
7 Posts 5 Posters 773 Views 3 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.
  • R Offline
    R Offline
    Ramkumar Mohan
    wrote on 29 Dec 2022, 12:30 last edited by
    #1

    How to auto-click pushButton in UI after running the main window

    main. CPP

    #include "mainwindow.h"
    #include "ui_mainwindow.h"
    #include <QApplication>
    #include "Aes.h"
    #include <QThread>
    #include <QDebug>

    int main(int argc, char *argv[])
    {
        QApplication a(argc, argv);
        MainWindow w;
        w.show();
        return a.exec();
    }
    

    UI Page :

    Capture.JPG

    Thanks in advance

    M J 2 Replies Last reply 29 Dec 2022, 12:48
    0
    • R Ramkumar Mohan
      29 Dec 2022, 12:30

      How to auto-click pushButton in UI after running the main window

      main. CPP

      #include "mainwindow.h"
      #include "ui_mainwindow.h"
      #include <QApplication>
      #include "Aes.h"
      #include <QThread>
      #include <QDebug>

      int main(int argc, char *argv[])
      {
          QApplication a(argc, argv);
          MainWindow w;
          w.show();
          return a.exec();
      }
      

      UI Page :

      Capture.JPG

      Thanks in advance

      M Offline
      M Offline
      mpergand
      wrote on 29 Dec 2022, 12:48 last edited by mpergand
      #2

      @Ramkumar-Mohan
      If I understand you well, you can simulate a click with:

      [slot] void QAbstractButton::click()
      Performs a click.
      All the usual signals associated with a click are emitted as appropriate. If the button is checkable, the state of the button is toggled.
      This function does nothing if the button is disabled.
      See also animateClick().

      1 Reply Last reply
      0
      • R Ramkumar Mohan
        29 Dec 2022, 12:30

        How to auto-click pushButton in UI after running the main window

        main. CPP

        #include "mainwindow.h"
        #include "ui_mainwindow.h"
        #include <QApplication>
        #include "Aes.h"
        #include <QThread>
        #include <QDebug>

        int main(int argc, char *argv[])
        {
            QApplication a(argc, argv);
            MainWindow w;
            w.show();
            return a.exec();
        }
        

        UI Page :

        Capture.JPG

        Thanks in advance

        J Offline
        J Offline
        JonB
        wrote on 29 Dec 2022, 12:48 last edited by
        #3

        @Ramkumar-Mohan
        It is an odd thing to want to do to actually cause an "auto click". Rather, since that would just emit the signal to cause any attached slot(s) to run, simply call the desired slot method(s) yourself explicitly in code.

        R 1 Reply Last reply 29 Dec 2022, 13:51
        0
        • J JonB
          29 Dec 2022, 12:48

          @Ramkumar-Mohan
          It is an odd thing to want to do to actually cause an "auto click". Rather, since that would just emit the signal to cause any attached slot(s) to run, simply call the desired slot method(s) yourself explicitly in code.

          R Offline
          R Offline
          Ramkumar Mohan
          wrote on 29 Dec 2022, 13:51 last edited by
          #4

          @JonB

          I wrote this code,

          #include "mainwindow.h"
          #include "ui_mainwindow.h"
          #include <QCryptographicHash>
          #include <QDebug>
          #include "qaeswrap.h"
          #include <memory.h>
          #include "aes.h"
          #include <QThread>
          
          
          MainWindow::MainWindow(QWidget *parent) :
          QMainWindow(parent),
          ui(new Ui::MainWindow)
          {
             ui->setupUi(this);
             on_pushButton_clicked();
          }
          
          MainWindow::~MainWindow()
          {
             delete ui;
          }
          
          void MainWindow::on_pushButton_clicked()
          {
               qDebug()<<"Enter";
          }
          

          I have called the pushbutton ui->setupUi(this) below. What I need is button call after Mainwindow is run. In this code Mainwindow is displayed after button call.

          J T 2 Replies Last reply 29 Dec 2022, 13:53
          0
          • R Ramkumar Mohan
            29 Dec 2022, 13:51

            @JonB

            I wrote this code,

            #include "mainwindow.h"
            #include "ui_mainwindow.h"
            #include <QCryptographicHash>
            #include <QDebug>
            #include "qaeswrap.h"
            #include <memory.h>
            #include "aes.h"
            #include <QThread>
            
            
            MainWindow::MainWindow(QWidget *parent) :
            QMainWindow(parent),
            ui(new Ui::MainWindow)
            {
               ui->setupUi(this);
               on_pushButton_clicked();
            }
            
            MainWindow::~MainWindow()
            {
               delete ui;
            }
            
            void MainWindow::on_pushButton_clicked()
            {
                 qDebug()<<"Enter";
            }
            

            I have called the pushbutton ui->setupUi(this) below. What I need is button call after Mainwindow is run. In this code Mainwindow is displayed after button call.

            J Offline
            J Offline
            JonB
            wrote on 29 Dec 2022, 13:53 last edited by JonB
            #5

            @Ramkumar-Mohan said in How to auto click pushButton in ui after running the mainwindow ?:

            after Mainwindow is run

            I suspect you mean after it is shown? Override its QWidget::showEvent(QShowEvent *event) and call on_pushButton_clicked() from there.

            Another alternative is to use QTimer::singleShot() and call it from within a slot attached to that, so after a delay.

            1 Reply Last reply
            2
            • R Ramkumar Mohan
              29 Dec 2022, 13:51

              @JonB

              I wrote this code,

              #include "mainwindow.h"
              #include "ui_mainwindow.h"
              #include <QCryptographicHash>
              #include <QDebug>
              #include "qaeswrap.h"
              #include <memory.h>
              #include "aes.h"
              #include <QThread>
              
              
              MainWindow::MainWindow(QWidget *parent) :
              QMainWindow(parent),
              ui(new Ui::MainWindow)
              {
                 ui->setupUi(this);
                 on_pushButton_clicked();
              }
              
              MainWindow::~MainWindow()
              {
                 delete ui;
              }
              
              void MainWindow::on_pushButton_clicked()
              {
                   qDebug()<<"Enter";
              }
              

              I have called the pushbutton ui->setupUi(this) below. What I need is button call after Mainwindow is run. In this code Mainwindow is displayed after button call.

              T Offline
              T Offline
              tapsbrighton
              wrote on 31 Dec 2022, 18:41 last edited by
              #6

              @Ramkumar-Mohan You could make the on_pushButton_clicked() a public slot and then call it from your main.cpp like:

              MainWindow w;
              w.show();
              w.on_pushButton_clicked();
              
              SGaistS 1 Reply Last reply 31 Dec 2022, 19:34
              0
              • T tapsbrighton
                31 Dec 2022, 18:41

                @Ramkumar-Mohan You could make the on_pushButton_clicked() a public slot and then call it from your main.cpp like:

                MainWindow w;
                w.show();
                w.on_pushButton_clicked();
                
                SGaistS Offline
                SGaistS Offline
                SGaist
                Lifetime Qt Champion
                wrote on 31 Dec 2022, 19:34 last edited by
                #7

                @tapsbrighton that will the same effect as calling the method in the constructor. The widget is not show once it's constructed but once show has been called and the event loop is running.
                The suggestion of @JonB is better. If visual presence is required then using the showEvent method is the way to go.

                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

                1/7

                29 Dec 2022, 12:30

                • Login

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