GIF动画在参考QStyleSheet静 [英] Animated Gif static in QStyleSheet

查看:315
本文介绍了GIF动画在参考QStyleSheet静的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

,我想用自定义样式表一QProgressBar的外观。我想给它的动画,不确定的样子。所以我做了一个GIF动画在 QProgressBar ::块的背景坚持。

I'm wanting to customize the look of a QProgressBar with a stylesheet. I'd like to give it that animated, 'indeterminate' look. So I made an animated GIF to stick in the background of the QProgressBar::chunk.

据精细显示出来,但图像是静态的,没有动画。任何建议或解决方法?

It shows up fine, but the image is static, with no animation. Any suggestions or workarounds?

我在OS X 10.8上运行的PyQt 4.9.4。

I'm running PyQt 4.9.4 on OS X 10.8.

推荐答案

所以,这是你面对一个稍微复杂的问题。

So, this is a slightly more complex problem that you're facing.

该QProgressBar依赖于信息从一个循环或预定量重新present完成百分比。当你通过code设置的值,将Qt的设置进度条的值,重新绘制,因此显示更新的值。这实际上需要你的循环中处理的几毫秒。如果你不这样做,那么Qt的优化不会永远重绘。这是采用同步。

The QProgressBar relies on information from a loop or some determined amount to represent the percentage complete. When you set the value through your code, Qt will set the value of the progress bar, redraw it and thus show the updated value. This actually takes a few milliseconds of processing within your loop. If you didn't do this, then Qt's optimization wouldn't ever redraw. This is sychronous.

在不确定看,你会为(像一个AJAX的gif微调或酒吧)用于在网络上,因为过程实际上移出的客户端和服务器上正在处理,正在等待回应。客户端的过程中不禁止可言,因此它是自由地更新与电影的接口。

The "indeterminate" look that you are going for (like an AJAX gif spinner or bar) is used on the web because the process actually moved off the client-side and is being processed on a server and is waiting for the response. The client's process is not blocked at all, so it is free to update the interface with the movie.

您可以通过在使用的QLabel实现QMovie你要去的样子:

You can achieve the look you're going for by using QMovie in a QLabel:

movie = QMovie()
movie.setFileName('/path/to/ajax_loader.gif')
movie.start()

label = QLabel(parent)
label.setMovie(movie)

这会让你不确定的微调。然而,将仅只要事件循环处理事件播放。一旦你开始你的实际工作进程,它会阻止事件循环,使您的电影将开始播放。

This will make your indeterminate spinner. However, it will only play as long as the event loop is processing events. Once you start your actual worker process, it blocks the event loop so your movie will start playing.

您将需要真正泵的活动,在循环播放器才能正常更新。你可以这样做:

You'll need to actually "pump" the events to the player in your loop to get it to update. You can do this by:

app = QApplication.instance()

# show my label
label.show()

for obj in objs:
    # process stuff on my obj
    app.processEvents()

# hide the label
label.hide()

当然,这取决于你在你的循环做每个动作需要多长时间,你的微调/加载器影片将被卡住,直到事件被再次处理。

Of course, depending on how long each individual action you're doing in your loop takes, your spinner/loader movie will be "stuck" until the events are processed again.

真的,要达到你要的效果是最好的方法是用一个线程应用程序。您可以使用的QThread来执行你的所有行动并显示图像加载到用户,而它正在处理。这是比较类似的方式Ajax的工作原理 - 主要的Qt事件循环将继续运行作为你的工作线程处理一切 - 在时间未知量。这是异步的。

Really, the best way to achieve the look you are going for is with a threaded application. You can use QThread to perform all of your actions and display the loader image to the user while it is processing. This is more similar to the way ajax works - the main Qt Event Loop will continue running as your worker thread processes everything - in an unknown amount of time. This is asynchronous.

是这样的:

class MyThread(QThread):
    def run( self ):
       # perform some actions
       ...

class MyDialog(QDialog):
    def __init__( self, parent ):
        # initialize the dialog
        ...
        self._thread = MyThread(self)
        self._thread.finished.connect(self.refreshResults)

        self.refresh()

    def refresh( self ):
        # show the ajax spinner
        self._ajaxLabel.show()

        # start the thread and wait for the results
        self._thread.start()

    def refreshResults( self ):
        # hide the ajax spinner
        self._ajaxLabel.hide()

        # process the thread results
        ...

我,每当我做这样的东西在我的GUI库,我使用加载器部件。如果你想看到的code吧/使用它,它在 HTTP://dev.projexsoftware .COM /项目/ projexui 和类是XLoaderWidget(projexui.widgets.xloaderwidget)

I have a loader widget that I use whenever I do stuff like this in my GUI library. If you want to see the code for it/use it, its at http://dev.projexsoftware.com/projects/projexui and the class is XLoaderWidget (projexui.widgets.xloaderwidget)

的设置是与上述相同,但也只是:

The setup is same as above, but would just be:

from projexui.widgets.xloaderwidget import XLoaderWidget

class MyThread(QThread):
    def run( self ):
       # perform some actions
       ...

class MyDialog(QDialog):
    def __init__( self, parent ):
        # initialize the dialog
        ...
        self._thread = MyThread(self)
        self._thread.finished.connect(self.refreshResults)

        self.refresh()

    def refresh( self ):
        # show the ajax spinner
        XLoaderWidget.start(self)

        # start the thread and wait for the results
        self._thread.start()

    def refreshResults( self ):
        # hide the ajax spinner
        XLoaderWidget.stop(self)

        # process the thread results
        ...

这篇关于GIF动画在参考QStyleSheet静的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆