How to call timerEvent(QTimerEvent *event) defined in separate class through startTimer(1000) function defined in another slot in that class only?
-
hi everyone!
Aim : To interface modem with my qt programme by making a separate gprs_modem classMethodology used: I have defined a separate class 'gprs_modem' , with all member functions defined in that class.
I am calling (and using) the members functions of that class in mainwindow by making object of that class.Challenge: One of the member function defined in gprs_modem class is
*void gprs_modem::timerEvent(QTimerEvent event)Steps which my programme supposed to execute :
- I want to call function1 (of gprs_modem class) through a pushbutton slot in mainwindow
- function1 will startTimer(1000)
- function1 should call timerEvent function of modem_class after each second
I am attaching my code alongwith for your kind reference.
// mainWindow.cpp
gprs_modem object1; //make object of gprs_modem class
void MainWindow::on_pushButton_clicked()
{ //calling function1 of gprs_modem Class so as to initiate timerobject1.function1();
}
//gprs_modem class ( my separate class )
void gprs_modem::function1()
{
startTimer(1000);
//now it should call QTimerEvent class defined below
}void gprs_modem::timerEvent(QTimerEvent *event)
{
// do something
} -
You can install event filter of gprs_modem class on MainWindow and then check inside MainWindow::eventFilter()
Code::
object1->installEventFilter(this); // Wherever you instantiate gprs_modem classand then in :
MainWindow::eventFilter(QObject *watched, QEvent *event)
{
if(watched == object1 && event-type() == QEvent::Timer)
{
// You can then either convert event to QTimerEvent or do your stuff directly.
}
} -
@Maaz-Momin ok...let me try it!
-
@Maaz-Momin i have posted my updated query, kindly have a look at it!
-
@SHUBHAM-SINGH-RAO What's your updated query?
-