- Qt5 C++ GUI Programming Cookbook
- Lee Zhi Eng
- 261字
- 2021-07-02 12:17:58
How it works...
The preceding example is a very simple application that showcases the use of lambda expressions to connect a signal with a lambda function or a regular function without declaring any slot function, and thus does not need to be inherited from a QObject class. This is especially useful for calling asynchronous processes that are not under UI objects.
Lambda expressions are functions that get defined within another function anonymously, which is quite similar to the anonymous functions in JavaScript. The format of a lambda function looks like this:
[captured variables](arguments) {
lambda code
}
You can insert variables into a lambda expression by placing them into the captured variables part, like we did in the example project in this recipe. We capture the QNetworkReply object called reply, and the QString object called html, and put them in our lambda expression.
Then, we can use these variables within our lambda code, as shown in the following code:
[reply, html]() {
html->append(QString(reply->readAll()));
}
The argument part is similar to an ordinary function, where you input values to the arguments and use them within your lambda code. In this case, the values of bytesReceived and bytesTotal are coming from the downloadProgress signal:
QObject::connect(reply, &QNetworkReply::downloadProgress,
[reply](qint64 bytesReceived, qint64 bytesTotal) {
qDebug() << "Progress: " << bytesReceived << "bytes /" << bytesTotal << "bytes";
});
You can also capture all variables that are used in your function using the equals sign. In this case, we captured the html variable without specifying it in the captured variable area:
[=]() {
printHTML(*html);
}
- 疑難雜癥中的心身疾病
- 動(dòng)態(tài)對(duì)比增強(qiáng)磁共振成像
- Building RESTful Web Services with Java EE 8
- 謠言背后的健康真相
- 眼睛健康,自己查(第二版)
- 醫(yī)院醫(yī)療質(zhì)量標(biāo)準(zhǔn)化管理手冊(cè)
- Qt5 C++ GUI Programming Cookbook
- 臨床神經(jīng)認(rèn)知及社會(huì)功能評(píng)估手冊(cè)
- 實(shí)用肝膽病臨床手冊(cè)
- Practical PowerShell Security and Compliance Center
- 分?jǐn)?shù)階微積分圖像修復(fù)模型在醫(yī)學(xué)CT圖像金屬偽影去除中的應(yīng)用
- 中國(guó)臨床腫瘤學(xué)會(huì)(CSCO)常見(jiàn)惡性腫瘤診療指南2021:上冊(cè)
- 放射診斷學(xué)讀片:北京協(xié)和醫(yī)院放射十年影像薈萃
- 醫(yī)療損害判例評(píng)析
- 麻醉安全與質(zhì)量管理