How can I have a pause between looped plays? I do have a .wav file, and i do want to play it continuously, but I want an X ms pause between each playback. I know I can edit the .wav file and add 'quiet' at its end, but I wanted to know if it can be done programmatically, without using timers.
You definitely want to use a singleShot QTimer to do a pause in Qt. There are some other mechanisms in Qt that use a QTimer internally. Some of the alternatives may make it a little prettier, but often are in a larger framework and for a different use case.
Some alternatives that might fit your application include:
You could start a sequential animation that uses a QPauseAnimation, with internally uses some sort of QTimer.
You could poll QTime::currentTime() if you are checking something often inside a loop or a QThread. You could also do the same polling with QTime::elapsed().
You could #include "windows.h" or the appropriate library for your platform and command a sleep, if you don't mind the GUI hanging up during the sleep.
You could set up a QStateMachine and have a transition animation/timer built in to happen between when your video stops and when you want to do the next action, or have something else show up or start.
Hope that helps. Good luck.
Related
I'm porting over a OpenGL application from a win32 project to a Qt project.
In the win32 project I simply had a mainloop that kept executing the next frame as soon as the previous was finished.
In the tutorials I find about Qt and OpenGL, they're using timers to control the update of the scene. However, I'd like to render as many frames as frequent as possible - how to do that?
However, I'd like to render as many frames as frequent as possible - how to do that?
Not with Qt. Qt takes full control of the event loop thus not giving you any means of directly performing idle operations. The standard way to perform idle operations in Qt is to use a QTime with a timeout of 0.
Another possibility was using a second thread just for OpenGL operations. However to work reliably you should create a dedicated OpenGL context for that one thread.
Qt simply is not the right toolkit for those kinds of program where the primary task of the main loop is rendering pictures and input and event management are only secondary.
Try calling update() at the end of your paint handler.
That will queue up another repaint event.
The intrinsic principles of Qt and OpenGL are not about rendering as soon as possible, but it's rendering whenever we want (huge difference).
You have to ask for the system max framerate (search for samples in OpenGL i'm sorry I don't rememember where I saw that). Once you've done that, just create a timer based on this max framerate, so you'll get what you want.
However, I'd like to render as many frames as frequent as possible - how to do that?
Use QTimer with zero interval.
I'm currently writing a programme which has a function to hash a number of files in the background. I've read the Qt4 documentation a number of times over and I still can't really figure out which threading option is best for this.
http://doc.qt.io/qt-5/thread-basics.html
There's really no need to update the GUI when it's done with each file, I just don't wish to block the GUI and I really only need a single signal/slot connection upon completion. I'm thinking of extending QThread for a hashing thread. Does this sound reasonable/right?
I have this article bookmarked as it nicely illustrates the use of QThread and highlights some common misconceptions about it. Sample code available, which runs without blocking the GUI. Sample is hosted on RapidShare, but they seem to have implemented some sort of timed waiting period since I last used it.
This sounds like a good place to use the QtConcurrent::map() function. The map function can apply the same operation to a container of objects, in your case, files. Once you start the map function, you can create a QFutureWatcher and connect to its finished signal to be notified when all of the work is done.
I want to run an LED pattern again and again using the for(), and without interrupting the other code that is running. But have encountered the problem of using delay() too much.
So, the BlinkWithoutDelay example repeats only one thing: turn the LED on, and off every second. If I were to do it with a pattern (and not just turning the LED on and off), how would I do it?
The problem is with millis()
What other options are there of running a pattern without using delay?
Agree your code would be nice to see, to underdatnd what you're trying to do. Assuming that the changes come LESS often than a loop without delay()'s in it, you can either use interrupts (actually very easy to set up on the arduino) or a library called Metro that lets you trigger timed events without using delay() or interrupts.
You've two options if you want to display a sequence, and have something else going on in the background.
First, you could sprinkle your sequence within the main loop(). It looks like a good few of the "LED chasers" and "KnightRider" effects on the internet are coded like this.
void loop () {
// do something
digitalWrite();
// do something
digitalWrite();
}
Or, you can use timer interrupts. This is a bit trickier to set up, but again, a quick internet search should bring up loads of examples. In this case, run the timer on the Arduino and set up the interrupts to trigger every xns. This will let the main loop do what it's doing, and every xns there will be a quick interruption to update your sequence.
If you want to run a time sensitive pattern, you might want to try using timer interrupts.
The tutorial here has a pretty good explanation and several examples of how to use the Arduino timer interrupts.
If you want to run the code while the led pattern is going on, I don't think it is possible on the arduino. That requires parallel processing of the code.
another quick question, I want to make simple console based game, nothing too fancy, just to have some weekend project to get more familiar with C. Basically I want to make tetris, but I end up with one problem:
How to let the game engine go, and in the same time wait for input? Obviously cin or scanf is useless for me.
You're looking for a library such as ncurses.
Many Rogue-like games are written using ncurses or similar.
There's two ways to do it:
The first is to run two threads; one waits for input and updates state accordingly while the other runs the game.
The other (more common in game development) way is to write the game as one big loop that executes many times a second, updating game state, redrawing the screen, and checking for input.
But instead of blocking when you get key input, you check for the presence of pending keypresses, and if nothing has happened, you just continue through your loop. If you have multiple input sources (keyboard, network, etc.) they all get put there in the loop, checking one after another.
Yes, it's called polling. No, it's not efficient. But high-end games are usually all about pulling the maximum performance and framerates out of the computer, not running cool.
For added efficiency, you can optionally block with a timeout -- saying "wait for a keypress, but no longer than 300 milliseconds" so you can continue on with your loop.
select() comes to mind, but there are other ways of waiting or checking for input as well.
You could work out how to change stdin to non-blocking, which would enable you to write something like tetris, but the game might be more directly expressed in an event-driven paradigm. Maybe it's a good excuse to learn windows programming.
Anyway, if you want to go the console route, if you are using the microsoft compiler, then you should have kbhit() available (via conio.h) which can tell you whether a call to fgetc on stdin would block.
Actually should mention that the MinGW gcc compiler 3.4.5 also supports kbhit().
I'd like to know when my application is idle so that I can preload some content. Is there an event or something similar implemented in PyQt?
(I could also do it with threads, but this feels like being too complicated.)
You have at least two different options, you can use a thread or use a timer. Qt's QThread class provides a priority property that you can set to make it only process when no other threads are running, which includes the GUI thread. The other option is a single shot timer. A QTimer with a timeout of 0 milliseconds puts an event on the back of the event queue so that all events and synchronous functions already active or scheduled will be processed first.
In code, the two options would look like the following:
// (1) use idle thread processing
MyQThreadSubclass idleThread;
idleThread.run(QThread::IdlePriority);
// (2) use QTimer::singleShot
QTimer::singleShot(0, receiver, SLOT(doIdleProcessingChunk));
If you go with the single shot QTimer, be careful how much processing you do as you can still block the Gui. You'd likely want to break it into chunks so that GUI won't start to lag:
// slot
void doIdleProcessingChunk() {
/* ... main processing here ... */
if (chunksRemain())
QTimer::singleShot(0, receiver, SLOT(doIdleProcessingChunk));
}
Obviously, the above is C++ syntax, but to answer with respect to PyQt, use the single shot timer. In Python, the global interpreter lock is basically going to render much of your concurrency pointless if the implementation being called is performed within Python.
You also then have the choice of using Python threads or Qt threads, both are good for different reasons.
Have a look at QAbstractEventDispatcher. But ... I still suggest to use a thread. Reasons:
It will be portable
If you make a mistake in your code, the event loop will be broken -> You app might hang, exit all of a sudden, etc.
While the preloading happens, your app hangs. No events will be processed unless you can preload the content one at a time, they are all very small, loading takes only a few milliseconds, etc.
Use a thread and send a signal to the main thread when the content is ready. It's so much more simple.