我有以下代码实现QtConcurrent::run()与QFutureWatcher启动fetch()运行shell进程的函数.完成后,我想调用该writeDesc函数,但它永远不会被调用.
void MyClass::on_fetchButton_clicked()
{
QFuture<void> fetcher;
QFutureWatcher<void> watcher;
connect(&watcher, SIGNAL(finished()), this, SLOT(writeDesc()));
fetcher = QtConcurrent::run(this, &MyClass::fetch);
watcher.setFuture(fetcher);
}
void MyClass::fetch()
{
[...]
qDebug()<<"Thread done";
}
void MyClass::writeDesc()
{
qDebug()<<"Slot called";
[...]
}
Run Code Online (Sandbox Code Playgroud)
fetch()工作正常,但程序只显示调试消息,Thread done而不是Slot called.为什么不writeDesc被召唤?
我是Qt的初学者.我需要从桌面应用程序中的按钮调用命令行程序.该计划下载YouTube视频.我还需要从中读取标准错误.我写了以下代码:
void YoutubeDL::on_downloadButton_clicked()
{
[...]
QProcess p;
p.startDetached("youtube-dl -f " + get + " " + ui->urlBox->text());
QString perr = p.readAllStandardError();
if (perr.length())
ui->descBox->setText("Error during download.\n" + perr);
else
ui->descBox->setText("Download completed!");
}
Run Code Online (Sandbox Code Playgroud)
然而,stderr读取不会发生.
在另一方面,如果我使用非分离p.start(),然后waitForFinished(-1)再我可以读取标准错误,但是GUI冻结,直至下载完成.
怎么解决这个问题?
一个相关的问题:我还想要一些方法能够实时读取下载过程的输出,以便我可以在GUI中显示它.youtube-dl给出了这样的进度报告:
[download] 0.0% of 2.00MiB at 173.22KiB/s ETA 00:12
[download] 0.1% of 2.00MiB at 105.01KiB/s ETA 00:19
[download] 0.3% of 2.00MiB at 96.86KiB/s ETA 00:21
[download] 0.7% of 2.00MiB at 105.23KiB/s ETA 00:19
[download] 1.5% of 2.00MiB at 100.29KiB/s ETA …Run Code Online (Sandbox Code Playgroud) 我正在尝试从文件中读取有向图邻接列表.我将它存储在每个节点的映射中,并将其存储到与其连接的节点的向量中.下面是连接到节点1的节点的示例输入行.
1 37 79 164 155 32 87 39 113 15 18 78 175 140 200 4 160 97 191 100 91 20 69 198 196
Run Code Online (Sandbox Code Playgroud)
我有以下代码成功编译但在运行时,在下面指出的循环中给出了分段错误.
typedef map<int, vector<int> > adjList;
ifstream file;
file.open("kargerMinCut.txt", ifstream::in);
string line;
adjList al;
while(!file.eof())
{
getline(file, line);
stringstream buffer(line);
int num;
buffer >> num;
al.insert(make_pair(num, adjList::mapped_type()));
// the below loop causes segmentation fault
while (!buffer.eof())
{
buffer >> num;
al.end()->second.push_back(num);
}
}
Run Code Online (Sandbox Code Playgroud)
我是STL的新手所以我可能会遗漏一些明显的东西,但请帮助我.