QPropertyAnimation modifying interval - c++

I'm using a QPropertyAnimation object to move some controls in the following way:
QPropertyAnimation* animation = new QPropertyAnimation(message_, "pos");
animation->setDuration(2000);
animation->setStartValue(current_pos);
animation->setEndValue(new_pos);
animation->setEasingCurve(QEasingCurve::Linear);
animation->start(QAbstractAnimation::DeleteWhenStopped);
asl::checkedConnect(animation, SIGNAL(finished()), this, SLOT(slotScrollingFinished()));
The object works AFAIK by executing every few milliseconds and modifying the value of the pos property until the duration expires and the end value is reached.
I was wondering if there is a way to modify the internal QPropertyAnimation object's interval timer to change the amount of times it will access the pos property within this 2 second duration.
I can't see anything in the docs.

You can do this:
#include <qt/src/corelib/animation/qabstractanimation_p.h>
QUnifiedTimer::instance()->setTimingInterval(new_interval);

Related

How to use signal to execute any function with arguments?

Suppose I have a QPropertyAnimator animating (moving), say, a button - slightly to the left over the course of 10 seconds.
When the button reaches its destination on the other side of the window it should change its text to "banana" using the QLineEdit::setText() function.
If the QLineEdit::setText() function is issued directly after the animation start;
QPropertyAnimator *animator = new QPropertyAnimator(someButton, "pos");
animator->setDuration(10000);
animator->setStartValue(*current position of the button*);
animator->setEndValue(*current position of the button with x-100*);
animator->start();
QLineEdit::setText(QString("Banana"));
The text changes before it has the chance to start moving. Luckily, QPropertyAnimator emits a signal when the animation is completed - aptly titled finished().
One would like to just be able to:
connect(animator, SIGNAL(finished()), someButton, SLOT(setText("Banana")));
But since you can't pass an argument to a slot that won't work.
How do I change the text after the animation is complete without creating different "proxy" functions (slots) to do it without arguments?
As already others told, you should use lambda, in your case,
connect( animator, &QPropertyAnimator::finished, [&]()
{
m_lineEdit->setText( QString("Banana") );
} );

Qt : how to automate looped slider movement in the background?

What's the most straightforward approach to have a slider move in a loop at a given speed when a button is pressed? I'm guessing it involves forking a thread that sends the appropriate signals periodically to the slider. Is there an canonical approach example for doing this?
I would suggest to use QPropertyAnimation to do the job. just set the start value, the end value and the curve you want the value to change
QPropertyAnimation *animation = new QPropertyAnimation(slider,"sliderPosition");
//set the duration (how long the animation should run - will change value faster when shorter)
animation->setDuration(1000);
//set the start value - in this case check if value in range of the slider
animation->setStartValue(slider->minimum());
//same as start value
animation->setEndValue(slider->maximum());
//easingCurve defines if it goes straight or bouncing n stuff
animation->setEasingCurve(QEasingCurve::OutCubic);
// as coyote mentioned, you can loop the animation as well (credit him as well ;))
// -1 defines to run forever
animation->setLoopCount(loopCount)
animation->start();
This would just be a matter of updating the slider's position on a timer. So, create a timer and on each update, call QSlider::setValue.
When the value is at maximum, set it back to the minimum and continue.
QSlider* pSlider = new QSlider;
QButton * pButton = new QButton("Go");
QTimer* pTimer = nullptr; // C++ 11 nullptr
// On button click, start a timer
connect(pButton, &QButton::clicked(), [=](){
// exit if already running
if(pTimer)
return;
pTimer = new QTimer;
connect(pTimer, &QTimer::timeout, [=](){
if(pSlider->value()+1 > pSlider->maximum())
pSlider->setValue(pSlider->minimum());
else
pSlider->setValue(++pSlider->value());
});
pTimer->start(1000); // update every second
});
You can use QTimer. Minimal example:
QSlider *sl = new QSlider;
QTimer *ttt = new QTimer;
sl->setValue(0);
connect(ttt,&QTimer::timeout,[=]() {
sl->setValue(sl->value() + 5);
});
sl->show();
ttt->start(500);
I used here C++11 (CONFIG += c++11 to .pro file) and new syntax of signals and slots, but of course you can use old syntax if you want.
There's no canonical approach to my knowledge.
The timers are given in the other answers, but you can also use the animation framework, and adjust the speed by adjusting the duration of the animation.
You can set the loopcount to how many times you want the animation to run, for example 1000000 to make it run a long time.

Using QTimer for displaying timer

I created a class called aTimer which inherits from QTimer. I want to be able to store the time elapsed into a variable called TimeElapsed of type int. I then want to have the timer automatically started when the main window opens and for it to display the time elapsed therein.
I think I'm using the wrong timer, and I am quite confused as to which tools to be using within Qt because there are different ways of handling time. Suffice it to say that I want a stop-watch kind of module that allows me to start and stop time manually without a cap (or an interval with the case of Timer). How shall I proceed? So far, attempts to use QTimer are fruitless.
You do not really need a derived class for this task. I'd probably use a QTimer and a QElapsedTimer.
Create them in your main window constructor and set the QTimers interval according to how often the time should be updated. Also connect its timeout() signal to a function updating the displayed value. In this function you can get the elapsed time from the QElapsedTimer and update the display.
// *.h
QTimer* timer;
QElapsedTimer *eltimer;
// *.cpp
constructor(){
this->timer = new QTimer(this);
this->timer->setInterval(1000);
connect(this->timer, SIGNAL(timeout()), this, SLOT(update_ui()));
this->timer->start();
this->eltimer = new QElapsedTimer(this);
this->eltimer->start();
}
SLOT update_ui(){
qint64 msecs_elapsed = this->eltimer->elapsed();
// Insert value into ui object
}
Of course you can create some buttons to start() and stop() the QTimer

Qt - Creating a notification at a certain time (PC)

For school I have to create a calendar program for the PC with Qt,
this calendar also has to give notifications a certain time before some scheduled appointment starts.
But i have trouble finding out how I exactly can create this notifications without using a while loop (which will stop my program), I would be really thankfully if someone can help me a bit with that certain part of my project.
Thank you,
Dennis
You can create a QTimer instance with interval set to 1 second. Then connect to QTimer::timeout() signal and in the slot you can check whether there is any appointment approaching, something like that:
void YourClass::slotNameForTimeoutSignal()
{
static const int fifteenMinutes = 15 * 60;
foreach (const Appointment& app : allAppoitments)
{
if ((app.getStartUnixTime() - QDateTime::currentDateTime().toTime_t()) <= fifteenMinutes)
{
notifyAboutTheAppointment(app); // implement this method to display notification
}
}
}
This code assumes you have some kind of Appointment class/structure which holds unixtime when the appoitment starts. The timer setup is easy. Somewhere in your application initialization create a timer (which should be a member field in your class), set it up and run:
YourClass::YourClass()
{
timer = new QTimer(this);
connect(timer, SIGNAL(timeout()), this, SLOT(slotNameForTimeoutSignal()));
timer->setInterval(1000);
timer->setSingheShot(false);
timer->start();
}
If you didn't know it yet, the unixtime is a time format, which is a integer number - a number of seconds that passed since the begining of 1970. Here's more details: http://en.wikipedia.org/wiki/Unix_time
Also if you're not familar with signals/slots in Qt, you should really read first about those in Qt documentation.
You can subclass QCalendarWidget to create your GUI i.e. the user can select the date when he wants the appointment. Use the QTimer to trigger the SIGNAL at your precise appointment time.
Now you can get the current date and time using QDateTime.
1. Check if the appointment is on the same date as current.
2. If yes jump to step no 4.
3. Set a timer using QTimer to emit a SIGNAL after 24 hours and connect this SIGNAL to your custom SLOT which will check if the current date is same as appointment date. Continue this step until your current date is same as appointment date.
4. Calculate the time difference between your appointment and current time and set this difference to a QTimer which will emit a SIGNAL at the required time.
5. This emitted SIGNAL would be connected a SLOT in which you are doing whatever is needed when the appointment is reached
I hope this helps you get in track.
Giving code would be like solving your homework which I don't want to do.

How do I progressively load a widget in QT?

I have a custom widget which displays many items in rows:
void update(){ //this is a SLOT which is connected to a button click
QVBoxLayout *layout = this->layout();
if (layout == NULL){
layout = new QVBoxLayout;
this->setLayout(layout);
} else {
QLayout_clear(layout); //this is a function that I wrote that deletes all of the items from a layout
}
ArrayList *results = generateData(); //this generates the data that I load from
for (int i = 0; i < results->count; i++){
layout->addWidget(new subWidget(results->array[i]));
}
}
The problem is that there are about 900 items and a profile reveals that simply adding the child object to the layout takes 50% of the time (constructing takes the other 50%). Overall it takes about 3 seconds to load all of the items.
When I click on the button to load more data, the entire UI freezes for the 3 seconds and then all of the items appear together when everything is done. Is there a way to progressively load more items as they are being created?
The first trick is, as Pavel Zdenek said, to process only some of the results. You want to process as many together so that the overhead (of what we're going to do in the next step) is low, but you don't want to do anything that would make the system seem unresponsive. Based on extensive research, Jakob Nielsen says that "0.1 seconds is about the limit for having the user feel that the system is reacting instantaneously", so as a rough estimate you should cut your work into roughly 0.05 second chunks (leaving another 0.05 seconds for the system to actually react to the user's interactions).
The second trick is to use a QTimer with a timeout of 0. As the QTimer documentation says:
As a special case, a QTimer with a timeout of 0 will time out as soon
as all the events in the window system's event queue have been
processed. This can be used to do heavy work while providing a snappy
user interface.
So that means that a timer with a timeout of 0 will be executed next, unless there is something else in the event queue (for instance, a mouse click). Here's the code:
void update() {
i = 0; // warning, this is causes a bug, see below
updateChunk();
}
void updateChunk() {
const int CHUNK_THRESHOLD = /* the number of things you can do before the user notices that you're doing something */;
for (; i < results->count() && i < CHUNK_THRESHOLD; i++) {
// add widget
}
// If there's more work to do, put it in the event queue.
if (i < results->count()) {
// This isn't true recursion, because this method will return before
// it is called again.
QTimer::singleShot(0, this, SLOT(updateChunk()));
}
}
Finally, test this a little bit because there's a gotcha: now the user can interact with the system in the "middle" of your loop. For instance, the user can click the update button while you're still processing results (which in the above example means that you would reset the index to 0 and reprocess the first elements of the array). So a more robust solution would be to use a list instead of an array and pop each element off the front of the list as you process it. Then whatever adds results would just append to the list.
#Adri is generally right, the twist is that the "another thread" must be the UI thread again. The point is to allow UI thread's event loop to keep spinning. The fast and dirty way is to put QCoreApplication::processEvents() in your for() cycle. Dirty because, as the doc says, it should be called "ocassionally". It might have some overhead even if there are no UI events, and you are messing Qt's performance optimization as to when and how often spin the loop. Slightly less dirty would be to call it only ocassionally, after chunks of result.
Cleaner and proper way is to create a private slot, which pops one result element (or chunk, to speed up), adds to the layout and increments index. Then it will recall itself until end of results. The gotcha is to define connect() with forced connection type Qt::QueuedConnection, so it will get deferred after already queued UI events (if any).
And because you run in only one thread, you don't need any locking over results.
Adding example per OP's request:
While #TomPanning solution is correct, it kind of hides the real solution behind QTimer which you don't need - you don't need any timing, you just need a specific non-timer behavior upon specific parameter value. This solution does the same thing, minus the QTimer layer. On the other hand, #TomPanning has a very good point about the plain ArrayList not being very good data storage, when interaction can happen in between.
something.h
signals: void subWidgetAdded();
private slots: void addNextWidget();
ArrayList* m_results;
int m_indexPriv;
something.cpp
connect(this,SIGNAL(subWidgetAdded()),
this,SLOT(addNextWidget(),
Qt::QueuedConnection);
void addWidget() {
// additional chunking logic here as you need
layout->addWidget(new subWidget(results->array[m_indexPriv++]));
if( m_indexPriv < results->count() ) {
emit subWidgetAdded(); // NOT a recursion :-)
}
}
void update() {
// ...
m_results = generateData();
m_indexPriv = 0;
addNextWidget(); // slots are normal instance methods, call for the first time
}