QT重绘/重绘/更新/执行某些操作 [英] QT Repaint/Redraw/Update/Do Something

查看:130
本文介绍了QT重绘/重绘/更新/执行某些操作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是QT新手。我了解您可以强制刷新显示,但是我已经竭尽全力试图弄清楚如何。

I'm New to QT. I understand that you can force a display refresh, but I've pulled all my hair out trying to figure out how. Here is what I'm specifically trying to do.

我按下一个按钮(onClick信号事件),该按钮运行的代码会更改显示器上的图像(QLabel),等待输入,然后通过更改新图像(不同的QLabel)进行操作。我已经尝试了所有方法,并且在完成onclick信号事件代码之前,显示不会刷新。现在,我不等待用户输入,我正在使用usleep(〜500 ms)进行测试。

I press a button (onClick signal event), which runs code that changes an image (QLabel) on the display, waits for input, and then proceeds by changing a new image (different QLabel). I've tried everything and the display doesn't refresh until the onclick signal event code is complete. Right now, I'm not waiting for user input, I'm using usleep(~500 ms) for testing purposes.

从我读到的内容来看,QT是事件驱动的这意味着我基本上是在创建一堆事件,放入事件队列中,并在(onClick信号事件)返回到(主循环)/(事件处理程序)时执行。我不想等到函数完成后,如果我必须完全基于事件来完成此例程,这将使编程变得非常痛苦。

From what I read, QT is event driven meaning that I'm basically creating a bunch of events, that get put in a que, and executed when the (onClick signal event) returns to the (main loop)/(event handler). I don't want to wait until the function is complete, it's going to make programming extremely painful if I have to accomplish this routine entirely based on events.

强制刷新QLabel像素图。我已经尝试了一切。以下是我在onClick信号事件处理程序中尝试过的所有代码。 (upButton是QLabel的名称,它是一个像素图)

How can I force the QLabel pixmap to refresh. I've tried everything. Below is all the code I have tried in my onClick signal event handler. (upButton is the name of the QLabel which is a pixmap)

update();
repaint();
ui->upButton->setUpdatesEnabled(TRUE);
update();
repaint();
QPaintEvent paintevent(ui->upButton->childrenRegion());
QPaintEvent * test = &paintevent;
paintEvent(test);
this->changeEvent(test);
ui->upButton->update();
ui->upButton->repaint();
ui->upButton->repaint(ui->upButton->childrenRegion());
repaint();
QApplication::sendPostedEvents();
this->parentWidget()->update();
usleep(100000);

如您所见,此时我只是在黑暗中拍摄。我试图查看示例代码并完成所有作业,但我迷路了。感谢所有帮助,建议和/或示例代码。

As you can see, I'm just shooting in the dark at this point. I've tried to look at sample code and do all my homework, but I'm lost. Appreciate any help, advice, and or sample code.

推荐答案

我正在使用睡眠来模拟计算机的短暂时间等待事情发生。

I was using sleep to emulate a brief amount of time the computer was waiting for something to happen.

正如我在问题中指出的那样,我不想使用事件,因为完成一件非常简单的事情是很多不必要的工作。

As I stated in my question, I didn't want to use events because it's a whole lot of unnecessary work to accomplish something extremely simply.

此外,程序要继续执行所需要的事件是USB事件。由于我使用的是HID类设备,因此没有等待循环就无法将事件设置为发生。 USB HID类不允许设置中断,操作系统要求保护该设备。

Also, the 'event' that needs to take place for the program to continue, is a USB event. Since I'm using an HID class device, there is no way to set an event to happen without a wait loop. USB HID classes don't permit setting interrupts, the OS claims the device.

我设法使上面的内容起作用。我遍历了调试器,发现显示在睡眠功能之前会刷新。独立运行程序,我得到了随机结果,显示刷新了1%的时间。我摆脱了睡眠功能,并在其中添加了一些其他代码来模拟延迟,这很好。

I managed to get the above to work. I walked through the debugger and noticed the display would refresh before the sleep function. Running the program independently, I got random results with the display refreshing 1% of the time. I got rid of the sleep function, and added some other code in it's place to emulate a delay, and it was fine.

这是有可能的,不是禁止的,并且很容易做到以下几点:

Just for everyone's knowledge, this is possible, it's not forbidden, and it's easy to do with the following:

qApp->processEvents();

qApp是QApplication标头中的全局外部变量。

qApp is a global external variable in the QApplication header.

由于此USB事件使流程变得棘手,因此我偶然发现了QWaitCondition类。我打算开始一个等待USB事件的过程。我将等到进程释放等待条件以使例程继续执行。

Because this USB event is making my flow tricky, I stumbled upon the QWaitCondition Class. I was going to start a process waiting for the USB event. I would wait until the process releases the wait condition for my routine to continue.

但是,如果有人认为这是个坏主意,请大声说出来。非常感谢您对PiedPiper和敌对叉子的反馈。

But if anyone thinks this is a bad idea, please, speak out. I really do appreciate your feedback PiedPiper and Hostile Fork.

谢谢。

这篇关于QT重绘/重绘/更新/执行某些操作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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