- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我想使用 QTimer
更新派生的 QSplashScreen
,它绘制进度条(使用绘画命令,而不是小部件)以估计程序何时开始运行.
根据需要,这发生在 QCoreApplication
的 exec
调用之前。我已经让它工作(仅在 Release模式下)在 X11 和 Windows 上,方法是将计时器放在第二个线程中,并在启动画面中调用一个函数来更新进度并重新绘制小部件。但是,这在 Debug模式下不起作用,因为它会产生以下错误:
“QCoreApplication::sendEvent 中的 ASSERT 失败:“无法将事件发送到不同线程拥有的对象。”
我并不真的担心这个断言,因为代码在发布时不会崩溃,它只是一个启动画面,但是我需要能够在调试中运行程序,所以我想要么 a) 重构代码,这样它就不会触发断言,或者 b) 取消这个特定的断言。
update()
而不是 repaint()
。这不会导致断言,但它也不会重新绘制,因为主线程太忙于加载共享库等,并且在我准备好调用 finish 之前不会处理计时器事件
在初始屏幕上。QTimer
。结果同上。QT::QueuedConnection
。同样的结果。#include <QApplication>
#include <QtGui>
#include <QTimer>
#include <QThread>
#include "mySplashScreen.h"
#include "myMainWindow.h" // contains a configure function which takes a
// LONG time to load.
int main( int argc, char* argv[] )
{
// estimate the load time
int previousLoadTime_ms = 10000;
QApplication a(argc, argv);
MySplashScreen* splash = new MySplashScreen(QPixmap(":/splashScreen"));
// progress timer. Has to be in a different thread since the
// qApplication isn't started.
QThread* timerThread = new QThread;
QTimer* timer = new QTimer(0); // _not_ this!
timer->setInterval(previousLoadTime_ms / 100.0);
timer->moveToThread(timerThread);
QObject::connect(timer, &QTimer::timeout, [&]
{
qApp->processEvents(); splash->incrementProgress(1);
});
QObject::connect(timerThread, SIGNAL(started()), timer, SLOT(start()));
timerThread->start();
splash->show();
a.processEvents();
myMainWindow w;
QTimer::singleShot(0, [&]
{
// This will be called as soon as the exec() loop starts.
w.configure(); // this is a really slow initialization function
w.show();
splash->finish(&w);
timerThread->quit();
});
return a.exec();
}
#include <QSplashScreen>
class MySplashScreen : public QSplashScreen
{
Q_OBJECT
public:
MySplashScreen(const QPixmap& pixmap = QPixmap(), Qt::WindowFlags f = 0)
: QSplashScreen(pixmap, f)
{
m_pixmap = pixmap;
}
virtual void drawContents(QPainter *painter) override
{
QSplashScreen::drawContents(painter);
// draw progress bar
}
public slots:
virtual void incrementProgress(int percentage)
{
m_progress += percentage;
repaint();
}
protected:
int m_progress = 0;
private:
QPixmap m_pixmap;
};
#include <QMainWindow>
class myMainWindow : public QMainWindow
{
public:
void configure()
{
// Create and configure a bunch of widgets.
// This takes a long time.
}
}
最佳答案
问题是因为设计落后了。 GUI 线程不应进行任何加载。 GUI 线程的一般方法是:不在 GUI 线程中工作。您应该生成一个工作线程来加载您需要加载的内容。它可以将事件(或使用排队连接调用槽)发布到 GUI 线程及其初始屏幕。
当然,工作线程不应该创建任何 GUI 对象——它不能实例化从 QWidget
派生的任何东西。不过,它可以实例化其他东西,因此如果您需要任何获取成本高昂的数据,请在工作线程中准备它,然后在数据可用后在 GUI 线程中廉价地构建一个 QWidget
.
如果您的延迟是由于库加载造成的,那么请明确地在工作线程中加载所有库,并确保它们的所有页面都驻留在内存中 - 例如通过在加载后读取整个 .DLL它作为一个图书馆。
可以在工作线程中调用 MyMainWindow::configure()
,只要它不调用任何 QWidget
方法或构造函数即可。它可以进行 GUI 工作,只是在屏幕上不可见。例如,您可以从磁盘加载 QImage
实例,或在 QImage
上绘画。
This answer提供了几种在不同线程中执行仿函数的方法,GCD-style .
如果您正在构建构建成本高昂的小部件,或者构建其中的许多小部件,则可以确保事件循环可以在每个小部件的实例化之间运行。例如:
class MainWindow : public QMainWindow {
Q_OBJECT
QTimer m_configureTimer;
int m_configureState = 0;
Q_SLOT void configure() {
switch (m_configureState ++) {
case 0:
// instantiate one widget from library A
break;
case 1:
// instantiate one widget from library B
...
break;
case 2:
// instantiate more widgets from A and B
...
break;
default:
m_configureTimer.stop();
break;
}
}
public:
MainWindow(QWidget * parent = 0) : QMainWindow(parent) {
connect(&m_configureTimer, SIGNAL(timeout()), SLOT(configure()));
m_configureTimer.start(0);
//...
}
};
关于c++ - 从线程重绘启动画面/禁用断言,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28905258/
我有一个表,用于测量数据集中两个不同值的分布百分比(我有一个计数,替换为 Percentage )。我只想在仪表板中发布 85% 的值。这样做的步骤是什么? .现在,如果我过滤掉 NO 值, YES
我想平均N通过Type对于 Inst 的值 我的数据设置为: Type, Inst, N A, A1, 4 A, A1, 13 A, A1, 13 A, A2, 4 A, A3, 5 B, B1, 4
当我使用 Node 的 http2 库(仅支持 HTTPS,不支持 HTTP)进行开发时,当我在 Chrome 中打开 localhost 时,我会看到一个警告屏幕: Your connection
我想创建一个具有响应式布局的导航栏菜单。当网站显示在显示器上时,我想播放导航栏。当网站在手机上显示时,我会显示最终显示导航栏的菜单图标。 我该怎么做? 最佳答案 试试这个,在移动设备上,导航栏不仅会显
我聘请了一名程序员为我创建一个 iPhone 应用程序。该应用程序的目的是拍照并将其上传到服务器。我们想制作一个特殊用途的屏幕,以便在上传照片之前查看照片。这个专门开发的屏幕将具有至关重要的缩放功能。
我在 Tableau 上有一个我无法解决的简单问题。 我想显示一个图表,显示随时间变化的度量。我想将用户通过参数选择的一个客户端与未选择的所有其他客户端进行比较。该图将显示具有 2 种不同颜色的两条线
我使用 Python 脚本从 3 个不同的 RDS 执行一系列复杂查询,然后将数据导出到一个 CSV 文件中。我现在正在尝试找到一种方法,每周将使用这些数据的仪表板自动发布到 Tableau 服务器中
我在工作中使用 tableau 来处理各种数据类型,包括敏感的个人数据,这些数据只能以聚合格式共享。我试图找到一种方法来保护私有(private)信息,方法是在单元格值小于 5 时隐藏它。这样,当用户
我最近开始在网站上嵌入 Tableau 可视化效果,并遇到了在浏览器中直接使用 Control + P 打印它们的问题。大多数完全扭曲,如果有的话。我做了一些挖掘,发现这是一个已知问题: http:/
例如,此 URL 包含十几个项目:https://tableautest.domain.uk/t/CustomerSharing/view/projects 在每个项目中都有几个工作簿。每个工作簿中都
我正在研究如何使用 Tableau 连接到 Cloudera Hadoop。我提供服务器和端口详细信息并使用“Impala”进行连接。我能够成功连接,选择默认模式并选择所需的表。 在此之后,当我将维度
我正在尝试将 Tableau 工作表嵌入到我的 ReactJS 应用程序中。我有一个包含报告名称列表的菜单(在 react 中),当单击菜单项时,它会更新包含报告名称的状态。我决定使用 tableau
我有以下问题!我有一个这样的表: Data Source 我想创建一个可以获取 apl_id 的字段(我想这是一个字段),有一些我想要的 service_offered。 上表中的示例。如果我想要只有
我有一个航类延误数据电子表格,我正在处理一个显示每个机场总延误时间的工作表。我想过滤维度“机场”,即根据每个机场的起飞次数创建机场类别,“小型”、“中型”和“大型”,这是通过计算维度“航类号”计算得出
我想创建一个带有过滤器的表格,用于选择和比较事物: 假设我有一个变量 Var,包含值 A、B、C、D、E。我想要一个过滤器,以便用户可以选择 A B C D 之一,同时 E 始终被选中。这样选中的E和
我是一名优秀的程序员,十分优秀!