Skip to content

Commit f462230

Browse files
committed
Modify fiber_qt doc.
1 parent 52264c2 commit f462230

File tree

2 files changed

+47
-3
lines changed

2 files changed

+47
-3
lines changed

fiber_qt.md

Lines changed: 47 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -141,14 +141,58 @@ void MainWindow::onStartServer() {
141141

142142
必须保证在界面程序退出前停止协程调度器,否则界面程序无法正常退出,该步骤也非常重要。可以在主界面处理类里重载基类的 ` void closeEvent(QCloseEvent *event);` 方法,在该方法里停止协程调度器,如下:
143143
```c++
144-
void MainWindow::closeEvent(QCloseEvent *event)
145-
{
144+
void MainWindow::closeEvent(QCloseEvent *event) {
146145
acl::fiber::schedule_stop(); // 停止协程调度器
147146
event->accept(); // 接受关闭事件
148147
}
149148
```
150149
151-
## 2.4、小结
150+
### 2.3.4、在界面线程中下载数据
151+
152+
点击主界面中点击HTTP下载按钮,在事件处理函数中创建协程从后端HTTP服务器下载数据,过程如下:
153+
```c++
154+
void MainWindow::onUrlGet() {
155+
...
156+
go[this] {
157+
const char *url = "http://www.baidu.com/";
158+
acl::http_request req(url);
159+
if (!req.request(nullptr, 0)) {
160+
printf("Send HTTP request failed\r\n");
161+
return;
162+
}
163+
acl::string body;
164+
if (!req.get_body(body)) {
165+
printf("Get HTTP body error\r\n");
166+
return;
167+
}
168+
qDebug() << "Got body:" << body.c_str();
169+
...
170+
};
171+
}
172+
```
173+
174+
### 2.3.5、在协程中延迟创建容器
175+
176+
如果想某个容器延迟创建,不必借助定时器,直接在协程中就可以轻松实现:
177+
```c++
178+
void MainWindow::delayCreate() {
179+
go[this] {
180+
acl::fiber::delay(5000); // 休眠 5 秒
181+
InputDialog dialog(this);
182+
dialog.exec();
183+
};
184+
qDebug() << "Fiber was created to create one window after a while";
185+
}
186+
```
187+
188+
## 2.4、效果展示
189+
190+
编译运行 acl/lib_fiber/samples-gui/QtFiber/ 工程,可以得到以下运行界面:
191+
![fiber_qt](/img/fiber_qt.jpg)
192+
- 在前面窗口中,右边请求HTTP服务器时的HTTP请求头,右连接为后端服务器返回的HTTP响应头,该下载过程中在协程中进行,运行结果显示在主界面上;
193+
- 窗口下方的进度条为客户端协程与服务端协程交互时的交互进度展示。
194+
195+
## 2.5、小结
152196

153197
以上便是如何编译集成 Acl 协程到 QT 界面程序的方法,主要的要点是:
154198
- 需要使用 vc2019 编译 Acl 的动态库,并集成至 QT 界面程序的工程文件中;

img/fiber_qt.jpg

254 KB
Loading

0 commit comments

Comments
 (0)