App development : Overlay widget and threading

I’m slowly getting my head in to the world of Qt, but one thing I’m struggling with is getting the overlay spinner working in a simple app.

Could someone show me how to add the overlay widget spinner to the starter-app?

From what I’ve discovered, I imagine I need to start my “slow” method in a separate thread to the main thread otherwise the overlay widget won’t show.

Rather than rolling my own threading, I took a look at using the shotgun-utils task_manager, but I have a feeling it’s too specific to SG methods to be used for my arbitrary method, is that true? or is there a simple example I could use to roll my own async function?

So,
would you lovely people at SG be able to flesh out a really simple fork of the starter-app showing how to implement the overlay spinner whilst an arbitrary slow function is being run? (the method could just be a wait() and then return a value and then update a QtLabel with the dummy value).

I think it would be great to have an example of how to make use of arbitrary async methods using the shotgun-utils framework.

Thanks in advance!
Patrick

PS : I don’t want an example showing a simple SG query using the framework… can probably figure that out, I’m interested in how we can roll our own async methods.

1 Like

Hey Patrick,

We have a Toolkit app called tk-multi-demo, which provides small snippets of code that show how to use many of the Toolkit components we’ve created over the years. There’s one for the spinner widget and another one for the overlay widget.

You can launch it from tk-desktop if you add the following to it’s environment:

tk-multi-demo:
   type: app_store
   version: v1.2.0
   name: tk-multi-demo

or you can launch it via the tank command on the command line using tank demos. I’ll check and see why it wasn’t added to the desktop’s environment. That seems like an oversight to me.

The widgets know nothing about threading, they are simply switching from one state to another on demand.

Unfortunately I can’t think of a simple piece of code right now that shows how to do that back and forth communication between the background thread and the spinner. This page might be a good place to start. Since you can only modify Qt UI objects from the main thread, understanding how slots and signals work in Qt is super important to ensure that your async job asks the GUI to update in a safe manner.

A simpler option could be to rely on Toolkit’s engine.execute_in_main_thread and engine.async_execute_in_main_thread, which allows to run arbitrary code in the main UI thread.

I hope this helps!

JF

1 Like

Thanks,
I managed to get things working without the overlay spinner, I just used a normal progress-bar which I could update with signals and slots from another QtThread. It seems to work well.
Are the methods you mention for executing in the main thread an alternative to setting up signals and slots?
Now that I’ve got normal QtThreads working with signals and slots, I’ll probably be able to get the overlay spinner to work, but as I’ve found an alternative, I probably won’t get around to testing that any time soon.
Thanks again.
p.

Hi Patrick,
Yes they are. They are an easy way to run a piece of code in the main thread. execute_in_main_thread allows you to run code in the main thread and get the result from the function back (or the raise exception) and async_execute_and_forget allows you to send a message and not wait for the result, aka fire and forget.
JF