PDA

View Full Version : Problem with operating of QTimer



sudheer168
8th October 2008, 07:46
Hi,
I have a problem with QTimer.In my application there is a main window and two Pushbuttons on main window.If I click one button a QTimer *timer will starts calling one function for every 10ms.It is declared in .h file of mainwindow as public .If I click second Pushbutton dialog will opens.Here in the dialog class I want to stop the timer which was started in mainwindow class.The code i used is shown below.



mainwin owin;
owin.timer->stop();


I wrote this code in dialog constructor.But it showing some run time error as "Unhandled exception at 0x670df703 in lpsc-jul16th.exe: 0xC0000005: Access violation reading location 0x56530908." and "There is no source code available for the current location." when i click the second Pushbutton to open the dialog.
So please help me to solve this problem.

With Regards,
Sudheer.

wysota
8th October 2008, 07:48
You surely can't construct a new object and try to call its nonexisting members. You have to access the original object where you created the timer and stop it there. Better yet use signals and slots between both objects, it'll probably be much easier than trying to access the object manually.

sudheer168
8th October 2008, 08:50
Hi,
Thanks for your quick reply . I once again posted my clear code what I want .

mainwin.cpp


#include "mainwin.h"

mainwin.h::lpsu_application(QWidget *parent)
: QMainWindow(parent)
{
setupUi(this);
QMainWindow::showMaximized();

}
void mainwin.h::on_Pushbutton1_Button_clicked()
{
timer = new QTimer(this); // it is decleare in .h file as QTimer *timer as public
connect(timer, SIGNAL(timeout()), this, SLOT(Averagedata()));
timer->start(10);
}
void mainwin.h::on_Pushbutton2_Button_clicked()
{
channel dialog(this);
dialog.exec();
}




channel.cpp



#include "channel.h"

temperature_pane::temperature_pane(QWidget *parent)
: QDialog(parent)
{
setupUi(this);

mainwin oman; // included "mainwin.h" in .h file of this class
oman.timer->stop();
}


So here i want to stop the timer started in mainwin.cpp in the channel.cpp.But if i click on Pushbutton2 the execution was stoping and showing some error which I posted in previous post.

So please help me to solve the pronlem
with Regards
Sudheer.

wysota
8th October 2008, 20:58
As I said, your problem is strictly C++ related. In line #8 of the second snippet you provided you create a new object and try to reference it's "timer" member which at that point is uninitialized because you initialize it later in a method that is never called on that object. If you put a newspaper on a table in your house, you can't walk into any other existing house and expect the same newspaper lying on the table.