当前位置:网站首页>1. Qtimer:: how to transfer parameters from singleshot, 2. Qmetaobject:: invokemethod how to transfer values with functions
1. Qtimer:: how to transfer parameters from singleshot, 2. Qmetaobject:: invokemethod how to transfer values with functions
2022-07-22 18:58:00 【Moon cave】
1.QTimer::singleShot How to transmit reference
QString p = "pink";
QTimer::singleShot(1000, this, [&,p](){
print(p);
}
);
Similarly, multiple parameters can be passed
advantage : You can pass in multiple parameters
2.QMetaObject::invokeMethod How to pass values with functions
QString d = "prr";
QMetaObject::invokeMethod(obj, [&,d](){
obj->print(d);
});
advantage :
1. Don't need to rely on QObject*, const char *member, arg1, arg2,arg3 Methods
2. Object function parameter changes , When the function name changes , You can make mistakes when compiling
It's all used lamda function , however lamda The function itself will have additional memory overhead , Up to the old code 5 times , As far as possible with QString perhaps qt Self contained small variable
Construction and Deconstruction of test objects , Define the following use cases
static int sqr = 0;
class XString{
public:
XString()
{
key = ++ sqr;
qDebug() << __FUNCTION__ << __LINE__ << key;
}
XString(const XString &other)
{
key = ++ sqr;
this->pink = other.pink;
qDebug() << __FUNCTION__ << __LINE__ << key;
}
~XString(){
qDebug() << __FUNCTION__ << __LINE__ << key;
}
QString getPink() const { return pink; }
void setPink(const QString &value){ pink = value; }
private:
QString pink;
int key;
};
class XObject : public QObject {
Q_OBJECT
public:
XObject(XString m, QObject *parent = nullptr)
: QObject(parent),m(m) {}
XString m;
public slots:
void trigger(){emit signal_trgg(m);}
signals:
void signal_trgg(XString p);
};
Use cases 1
XString p;
p.setPink("pink");
QTimer::singleShot(1000, this, [&,p](){
//this Function of
}
);
function :
XString 18 1
XString 24 2
XString 24 3
XString 24 4
XString 24 5
~XString 28 4
~XString 28 3
~XString 28 2
~XString 28 1
~XString 28 5
Use cases 2
XThread *thread = new XThread(this);
XString p;
p.setPink("pink");
XObject *o = new XObject(p, 0);
connect(o, SIGNAL(signal_trgg(XString)), this, SLOT(print(XString)));
connect(thread, SIGNAL(finished()), o, SLOT(deleteLater()));
QTimer::singleShot(1000, o, SLOT(trigger()));
o->moveToThread(thread);
thread->start();
function :
XString 18 1
XString 24 2
XString 24 3
~XString 28 2
~XString 28 1
XString 24 4
XString 24 5
~XString 28 4
~XString 28 5
~XString 28 3
Multithreaded call
XString d;
d.setPink("prr");
qDebug() << __LINE__ << d.getPink() << QThread::currentThreadId();
QMetaObject::invokeMethod(obj, [&,d](){
qDebug() << __LINE__ << d.getPink() << QThread::currentThreadId();
});
function :
XString 18 1
63 "prr" 0x1fa0
XString 24 2
XString 24 3
XString 24 4
~XString 28 3
~XString 28 2
65 "prr" 0x27dc
~XString 28 1
~XString 28 4
边栏推荐
- 用LaTeX写论文时如何加资助信息
- Flink learning notes (VI) Flink's time and window
- 1. Create a dynamic library of functions, 2. Howto create and deploy a sample DLL using MinGW
- 1. Where is the date for qdate (), 2. QT_ Usage Summary of version
- Leetcode 105. constructing binary trees from preorder and inorder traversal sequences
- C语言 static和extern知识点
- Bull column - blog summary
- (c语言)数组是一种特殊的指针?
- JVM调优实战-从零开始 | 项目有关JVM调优总结
- Qt | boîtes de dialogue modales et non modales qdialog
猜你喜欢
JVM-系统优化
mysql执行过程以及顺序
Upgrade win10sp1 to the latest version; Qt5.9.6 static compilation (network is valid)
Leetcode 105. constructing binary trees from preorder and inorder traversal sequences
Learning to Incorporate Structure Knowledge for Image Inpainting
Thymeleaf中一个页面怎么嵌套另一个页面,关于页面嵌套,标签告诉你应该知道的
VLFeat、pydot配置
逻辑回归中的损失函数
Caching-sha2-password problem occurred when connecting mysql8.0
PTA basic question 7-23 currency conversion (20 points) (true)
随机推荐
Over the weekend, I had a dinner with the technology gurus and talked about the "golden nine and silver ten" peak of the software testing industry [the trend of involution has been formed]
There is no session in the tensorflow module
PCV、PIL、Pillow安装
Vlfeat, pydot configuration
LeetCode 720. 词典中最长的单词
Tcpdump 简单用法
mysql执行过程以及顺序
【链表技巧汇总】876.链表的中间节点
程序员面试金典面试题 01.03. URL化
Sprintf rewriting of QT; The content under QT is scaled according to the scaling of the interface (without changing the font size)
leetCode笔记
Latex如何写引用时将作者名字缩写为et al
Log4j log configuration details
MySQL statement execution order
To achieve the effect of budget DLL, QT embeds the third-party window into the program, excel operation, database foreign keys, and determines whether the program is started
Leetcode 105. constructing binary trees from preorder and inorder traversal sequences
PTA 习题8-8 判断回文字符串
Flink learning notes (III) Flink installation and deployment
交叉熵损失函数
JSON序列化对象时,如何返回有空值的带属性名称json字符串?