How to auto click pushButton in ui after running the mainwindow ?
-
wrote on 29 Dec 2022, 12:30 last edited by
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 :
Thanks in advance
-
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 :
Thanks in advance
wrote on 29 Dec 2022, 12:48 last edited by mpergand@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(). -
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 :
Thanks in advance
wrote on 29 Dec 2022, 12:48 last edited by@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. -
@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.wrote on 29 Dec 2022, 13:51 last edited byI 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.
-
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.
wrote on 29 Dec 2022, 13:53 last edited by JonB@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. -
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.
wrote on 31 Dec 2022, 18:41 last edited by@Ramkumar-Mohan You could make the
on_pushButton_clicked()
a public slot and then call it from yourmain.cpp
like:MainWindow w; w.show(); w.on_pushButton_clicked();
-
@Ramkumar-Mohan You could make the
on_pushButton_clicked()
a public slot and then call it from yourmain.cpp
like:MainWindow w; w.show(); w.on_pushButton_clicked();
@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.
1/7