QTimer help needed
-
Hello.
I am having trouble with QTimer. I don't know how to use it.
@void CFrameWnd::Taimeris()
{
QTimer *timer = new QTimer(this);
timer->setInterval(1000);
connect(timer, SIGNAL(timeout()), this, SLOT(Metodas()));
timer->start();
}void CFrameWnd::Metodas()
{
int y = rand() %1 + 100;
int x = -5;
QMovie *movie = new QMovie(":/Antis/paveiksl/duck1111.gif");
QLabel *processLabel = new QLabel(this);
processLabel->setMovie(movie);
movie->start();
processLabel->setGeometry(x,y,55,39);
for(int i = 1; i < 700; i++){ // judejimo ciklas
processLabel->move(x+i,y); // judejimas
// if(processLabel->x == 700)
// QApplication::exit();
}
}@There are my function to use QTimer. Tell me what i do wrong? I want that my object processLabel would be moving from point X = -5 and random point Y on X axis to the last point on X axis. The movement should be visible in screen like flying duck, but when program executes the duck is just shown in final point...
-
you're using your QTimer to let the duckfly after 1s. Problem is your duck fly very quickly (in fact, as quick as your loop can happen), so you see it only at the end
What happens:
- when you call Taimeris, it starts a timer
- 1s after, the timer is elapsed and metodas is called
- synchronously, it will start the movie, then fly the duck as quick as possible to it's new location (synchronously, meaning no event, nothing can be catched in the mean time).
To do what you want to do, you might want to have a look at "QTimeLine":http://doc.qt.nokia.com/4.6/qtimeline.html
-
"QPropertyAnimation":http://doc.qt.nokia.com/latest/qpropertyanimation.html would be even better for this use case. Something along the lines shown in the docs would suffice:
@
QPropertyAnimation *animation = new QPropertyAnimation(myWidget, "geometry");
animation->setDuration(10000);
animation->setStartValue(QRect(0, 0, 100, 30));
animation->setEndValue(QRect(250, 250, 100, 30));animation->start();
@ -
.
-
Hello everyone.
I try to create duck hunt game and have several questions.
First of all i use a function to create duck object and set it in layout.@void CFrameWnd::SukurtiAnti()
{QMovie *movie = new QMovie(":/Antis/paveiksl/duck1111.gif"); QLabel *processLabel = new QLabel(this); processLabel->setMovie(movie); movie->start(); ui->gridLayout->addWidget(processLabel); ui->gridLayout->setAlignment(Qt::AlignLeft);
}
@
Secondly in constructor i have created QTimer.
@
CFrameWnd::CFrameWnd(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::CFrameWnd){
ui->setupUi(this);
SukurtiAnti();QTimer *timer = new QTimer(this); parametras = 0; timer->setInterval(1/30); connect(timer, SIGNAL(timeout()), this, SLOT(Metodas())); timer->start(1000);
}@
@
void CFrameWnd::Metodas()
{
parametras += 1;
QMovie *movie = new QMovie(":/Antis/paveiksl/duck1111.gif");
QLabel *processLabel = new QLabel(this);
processLabel->setMovie(movie);
movie->start();
ui->gridLayout->addWidget(processLabel);
processLabel->move(parametras,20);
// qDebug() << parametras;// if(processLabel->x == 700)
// QApplication::exit();// jeigu mirus y asyj krenta
}@I don’t know how to do better with object create and movement. First of all, if i create object in void SukurtiAnti() i don’t know how to connect it with QTimer to create object, secondly if i create object in void Metodas() then Qtimer creates new duck every second, but that it is not what i need.
I need that the duck will be created and will be moving from one point on X axis until it reach X value 700, then program should quit, but i just need to make duck movement and make it visible to my eyes .
Any advices are appreciated.
Cheers
-
Do you read the suggestions people gave you?
florent.revelut and ZapB (and me in an other topic) gave you a solution for your problem, so why dont you try that first?
If you, for some reason, want to stick with the timer idea, then you use the timeout signal as you did.nb. try a little with the "parametras". +1 is not that much on a large screen.
-
First of all, thanks for advices, but teacher requires QTimer :)... That is why i am using it.
try a little with the “parametras”. +1 is not that much on a large screen. <- i didn't understand this sentence..
You all gave me useful advices, but i still don't understand how to move my duck on the screen.. how to make it moving ...
-
In function "Metodas" :
- You set "parametras" to parametras += 1. (line 3) But you dont use a loop to increase "parametras". So "parametras" = always 1 in your function, because in your main you set it to 0 and in the function you set it to 1.
- In line 9 you "move" the label to (int x = "parametras" = 1, int y = 20).
With other words,
You put the label at a "fixed" coordinate when your timer is finished. After that you dont do anything, so it stays that way.
Make a loop with an incement of "parametras". And keep in mind that if my screen is 2000 pixels width i am not really see an increment of 1 pixel. -
Thanks for reply!!!
But when i execute program object is not moving, just objects are generating i wonder how to make just one object and make it moving ? ? ?
Cheers!
-
But parametras += 1 ====== parametras + 1 and when qtimer is initialized parametras keep growing until i close program... or i misunderstood something?
-
Sometime, I prefer not answering than being rude.
-
sorry for me... i am newbie at qt and trying to do this work..
-
Sorry, i was thinking about a singleshot :).
I see that you create a new label in your function "Metodas", this means that you always make a new label when your timer finishes, I dont think that that is what you want.Just create the timer and label in your constructor and move it in your function.
here is a little example wich works:
In ".h":
@
int xas;
int yas;
QLabel *l;
@In constructor:
@
l = new QLabel(this);
l->setText("Move Please?");
l->setGeometry(0,0,200,20);
l->show();
xas = 1;
yas = 1;
QTimer *t = new QTimer(this);
connect(t,SIGNAL(timeout()),this,SLOT(Metodas()));
t->setInterval(1/30);
t->start(50);
@
in metodas:
@
xas +=1;
yas +=1;
l->move(xas,yas);
@ -
Very big thanks to you!!!! You are genius !
-
And the last question... :) Is there a possibility to make y axis random ? I need that duck would be moving x axis as you mentioned, but i need that it would appear in random place on y axis .
I have tried
@ yas = rand()+240; @
but the duck appears in the value of y axis = 240. Is there a way to make it random? -
ty!!!
-
audrensitas: You do not believe in reading, do you? It is really easy to pick up example code on google when you already know the function name!
You know that the idea behind homeworks is that the pupil learns something? That includes how to find background information. Bugging people on the internet with really basic questions is not the best way to accomplish that in my opinion.
-
And perhaps even more important is then the emerging understanding from reading and digesting such examples. This then allows you to apply techniques and particular building blocks in other situations. Learning should also be fun. Do not be afraid to experiment on your own.