PDA

View Full Version : QMainWindow Dragging is suspending QTimer



zaferaltug
10th February 2009, 10:56
Hi.
I have a class that inherits QMainWindow.And that class has a QTimer as an attribute.Qtimer starts at construction.When I do a mouse press or a drag event on that QMainWindow or a child widget of that mainwindow the timer is suspending.That suspending suspends my drawing in a QGraphicview.I understand the stopping of drawing at dragging or etc..But if timer stops,my GraphicItem falls behind the position which GraphicItem must be there at the time.
So does anybody know a way how I can prevent the application from freezing of QTimer?
Regards..

talk2amulya
10th February 2009, 11:49
Please share the relevant code

zaferaltug
10th February 2009, 13:52
//mainclass.h
#include <QMainWindow>
#include <QGraphicsScene>
#include <QGraphicsView>
#include <QGraphicsLineItem>
#include <qtimer.h>
#include "ui_mainclass.h"

class MainClass : public QMainWindow
{
Q_OBJECT

public:
MainClass(QWidget *parent = 0);
~MainClass();
QTimer timer;
QGraphicsScene scene;
QGraphicsView *view;
QGraphicsLineItem *line;
public slots:
void moveLine();
void openWindow();

private:
Ui::MainClassClass ui;
};

//mainclass.cpp
#include "mainclass.h"
#include "window.h"
#include <QPen>



MainClass::MainClass(QWidget *parent)
: QMainWindow(parent)
{
QPen pu(Qt::red);
pu.setCapStyle(Qt::RoundCap);
pu.setJoinStyle(Qt::RoundJoin);
pu.setWidth(7);
ui.setupUi(this);
line=new QGraphicsLineItem(100,100,200,100);
line->setPen(pu);
line->setFlag(QGraphicsItem::ItemIsMovable);
scene.addItem(line);
//QObject::connect(&timer, SIGNAL(timeout()), &scene, SLOT(advance()));
QObject::connect(&timer, SIGNAL(timeout()), this, SLOT(moveLine()));
timer.start(30);
scene.setSceneRect(0, 0,80000, 600);
view=new QGraphicsView(&scene,this);
view->setGeometry(0,50,800,620);
connect(ui.actionOpen,SIGNAL(triggered()),this,SLO T(openWindow()));

for(int i=0;i<200;i++)
{
pu.setColor(Qt::green);
pu.setWidth(3);
QGraphicsLineItem *templine=new QGraphicsLineItem(30*i,110,30*i,120);
QGraphicsSimpleTextItem *text=new QGraphicsSimpleTextItem(QString().setNum(30*i));
text->setPos(30*i,130);
templine->setPen(pu);
scene.addItem(templine);
scene.addItem(text);
}


}

void MainClass::moveLine()
{
line->moveBy(1,0);
view->centerOn(line);
}
MainClass::~MainClass()
{

}
void MainClass::openWindow()
{
mywindow* mywind=new mywindow();
mywind->show();
}

//main.cpp

#include <qapplication.h>
#include "mainclass.h"
#include <stdlib.h>
int main(int argc, char** argv)
{
QApplication app(argc,argv);
MainClass m;
m.resize(m.sizeHint());
m.setCaption("QTimer");
m.setGeometry(40,40,810,610);
m.show();
QObject::connect( qApp, SIGNAL(lastWindowClosed()), qApp, SLOT(quit()) );
return app.exec();
}
(mywindow is a simple widget).
Here is an example that's code is similar with my application.But I see it is working.In my application I use a code like above.But in my code that isn't working.my code is very long to put it here.
My application is a simulator management screen so,it must work sychronously with simulator engine.If my timer stops or suspend for any resason the system gives error.
So I'll be waiting for any reply.
Regards.

zaferaltug
10th February 2009, 14:23
I found the problem.If time out span of the timer is under 15 miliseconds,the widget is suspending the timer during the dragging or etc.In the example above it is 30 ms,but in my application it was 10 ms,now I've rised it up to 20 ms it is working well.
Regards.