- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
与 C# 类似,我使用了一个工作片段,例如:
QString text = "Hello";
QMetaObject::invokeMethod(m_ui.textEdit_ConnectionStatus, "setText", Qt:QueuedConnection, Q_ARG(QString, text));
...为了更改不是来自主线程的 GUI 元素。但是,它似乎与 textEdit 的 setPalette 的工作方式不同。
QPalette pal = palette();
pal.setColor(QPalette::Base, Qt:darkGreen);
QMetaObject::invokeMethod(m_ui.textEdit_ConnectionStatus, "setPalette", Qt:QueuedConnection, Q_ARG(const QPalette&, pal));
如何从另一个线程更改此 gui 元素的颜色?
最佳答案
OP 使用了 QMetaObject::invokeMethod()
它依赖于已注册的插槽(在 Qt4 中很常见)。 QTextEdit::setText()是这样一个插槽,但QTextEdit::setPalette()不是。
因此,QTextEdit::setPalette()
在运行时无法通过以字符串形式给出的名称找到。
在 Qt5 中,信号槽的概念得到了扩展,以支持通过编译时检查来连接信号和槽。
出于好奇,我查看了文档。并找到 QMetaObject::invokeMethod()它接受一个仿函数:
template <typename Functor, typename FunctorReturnType> bool QMetaObject::invokeMethod(QObject *context, Functor function, Qt::ConnectionType type = Qt::AutoConnection, FunctorReturnType *ret = nullptr)
This is an overloaded function.
Invokes the function in the event loop of context. function can be a functor or a pointer to a member function. Returns true if the function could be invoked. Returns false if there is no such function or the parameters did not match. The return value of the function call is placed in ret.
Note: This function is thread-safe.
This function was introduced in Qt 5.10.
// standard C++ header:
#include <chrono>
#include <thread>
// Qt header:
#include <QtWidgets>
// main application
int main(int argc, char **argv)
{
qDebug() << "Qt Version:" << QT_VERSION_STR;
QApplication app(argc, argv);
// setup GUI
QTextEdit qTextEdit(QString(
"<p>Hello world.</p>"
"<p>Hello Qt.</p>"
"<p>Hello Stack Overflow.</p>"));
qTextEdit.show();
// a separate thread to manipulate qTextEdit
std::thread threadPal([&qTextEdit]() {
using namespace std::chrono_literals;
const QColor qColors[] = { Qt::red, Qt::green, Qt::blue, Qt::white };
QColor qColor;
for (const QColor &qColor_ : qColors) {
std::this_thread::sleep_for(1s);
qColor = qColor_;
QMetaObject::invokeMethod(&qTextEdit, [&qTextEdit, qColor]() {
QPalette qPal = qTextEdit.palette();
qPal.setColor(QPalette::Base, qColor);
qTextEdit.setPalette(qPal);
});
}
});
// runtime loop
const int ret = app.exec();
// done
threadPal.join();
return ret;
}
输出:
qTextEdit
的所有访问。只
QMetaObject::invokeMethod()
的 lambda 内. qTextEdit
的引用在
threadPal
的仿函数中被捕获.用于提供
qTextEdit
的地址至
QMetaObject::invokeMethod()
作为上下文。这是制作
QMetaObject::invokeMethod()
所必需的。注意所提供的仿函数必须在不同的线程中执行(
qTextEdit
关联的 GUI 线程)。 (与
qTextEdit
本身相反,
qTextEdit
的地址在线程运行时是不可变的。因此,不 protected 访问是线程安全的。)
关于c++ - TextEdit setPalette 上的 Qt InvokeMethod,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63904176/
我有一个 java 对象的实例,假设有一个名为 myList 的 ArrayList 实例。 对于此特定实例,我想覆盖 invokeMethod 方法以(例如)记录调用该方法。 我可以做这样的事情:
我在项目中有基类A A继承了很多子类 public class A { public void Process(string myString1, int myInt1 )
我注意到 Groovy MetaClass 的一些奇怪行为,我想知道是否有人可以给我一些关于这里发生的事情的线索。 这工作正常: @Override Object invokeMethod(Strin
我想使用QMetaObject::invokeMethod来调用一个对象的方法(稍后它会在另一个线程中运行,然后invokeMethod就派上用场了)。我在 Python 3.3 上使用 PySide
我使用以下代码来避免使用 switch 语句来决定调用哪个方法,它只与我设置的 BindingFlags 标志一起工作,没有 InvokeMethod。 InvokeMethod 的实际含义是什么,为
我的类中有一个方法调用QMetaObject::invokeMethod。来自documentation我读到使用 Qt::DirectConnection 应该立即调用插槽。在我的代码中,我似乎体验
是否可以调用静态方法? 我正在使用: QMetaObject::invokeMethod(this ,strThread.toLatin1()
这是我要调用的方法的原型(prototype): const QString& FieldForm::getTitle(void) const; 我必须通过 Qt 函数调用此方法:invokeMeth
我正在尝试调试一段代码,该代码更新用于 Windows 服务的凭据,该服务未运行。它不会抛出异常,它只是没有通过检查以表明它已被应用。 MSDN对构造函数的描述如下: public Object In
我目前正在移植 FitNesse 的 Slim 服务器,但我现在有点卡住了。我得到的是像这样的字符串: ("id_4", "call", "id", "setNumerator", "20") ("i
我正在用 Qt 做一个项目,其中 invoke 方法在单独的线程上运行并从主线程调用。如果我将 QByteArray 作为 const 传递,它会构建并运行。但是,如果我删除它构建的 const 但在
这是 QMetaObject::invokeMethod doesn't find the method 的跟进.调用没有参数的方法是有效的。但是将前面的问题扩展到带参数的方法让我再次失败。 请参阅以
我正在开发一个小型 Flutter 应用程序,我在其中使用 native 库进行一些计算。 dart 和 java(在 android 上)之间的通信是双向的,为此使用 methodChannels。
我有一个父类(super class)Common,它继承自QObject。然后我得到了一个类Item,它继承自Common。 Common.h class Common : public QObje
我有以下代码: class A : public QObject { Q_OBJECT public: A() : QObject() { moveToThr
...从静态类和非主线程调用。 简而言之,我有一个类“sapp”,它有另一个静态类“tobj”作为静态成员。为了避免静态订单初始化失败,tobj 在 sapp 的方法中声明,该方法又返回 tobj 实
在 Groovy 中,invokeMethod 和 methodMissing 方法之间的主要区别是什么?是否有明确的指导原则,何时应该优先使用其中一个? 最佳答案 何时使用什么:始终使用 metho
除了QMetaObject::invokeMethod是否有任何类型安全的方式来异步调用方法/插槽(也就是在 GUI 线程中排队执行)? QMetaObject::invokeMethod没有对函数名
我想从下面给定的类调用groovy方法 package infa9 import java.io.BufferedReader; import java.io.IOException; import
我正在尝试找出 QMetaObject::invokeMethod 的用法。我有一个函数,它有一个参数(非 const QString),我希望它是输出,该函数没有返回值,对其调用 invokeMet
我是一名优秀的程序员,十分优秀!