我为按钮有以下代码:
emit busySignal();
QString program = "cmd";
QStringList arguments;
arguments << "";
QProcess *myProcess = new QProcess(this);
myProcess->start(program, arguments);
myProcess->write("cd C:\\Qt\\5.11.2\\mingw53_32\\bin\\\n");
myProcess->waitForBytesWritten();
myProcess->write("windeployqt C:\\Users\\BRSLV\\Desktop\\qml_collect_project\n");
myProcess->waitForBytesWritten();
myProcess->waitForFinished();
myProcess->close();
emit readySignal();
以及以下qml代码:
Model
{
id : model
onBusySignal: busy.running = true
onReadySignal: busy.running = false
}
BusyIndicator {
id: busy
running: false
}
问题是-信号由于某些原因无法正常运行:仅在QProcess完成后,busySignal()才会影响qml。有人可以告诉我如何解决吗? 谢谢。
答案 0 :(得分:3)
waitForXXX方法正在阻塞,因此它们将冻结GUI,从而阻止执行异步任务,例如信号传输,因此解决方案是使用信号来知道何时通知任务已完成。
另一方面,可以使用setWorkingDirectory()
代替cd
命令来减少代码。
main.cpp
#include <QGuiApplication>
#include <QProcess>
#include <QQmlApplicationEngine>
class ProcessManager: public QObject
{
Q_OBJECT
public:
ProcessManager(QObject *parent=nullptr):
QObject(parent)
{
connect(&m_process, QOverload<int>::of(&QProcess::finished), this, &ProcessManager::readySignal);
}
Q_INVOKABLE void start_process(){
emit busySignal();
const QString program = "cmd";
m_process.setWorkingDirectory("C:\\Qt\\5.11.2\\mingw53_32\\bin");
m_process.start(program, {"windeployqt", "C:\\Users\\BRSLV\\Desktop\\qml_collect_project"});
}
signals:
void busySignal();
void readySignal();
private:
QProcess m_process;
};
int main(int argc, char *argv[])
{
QCoreApplication::setAttribute(Qt::AA_EnableHighDpiScaling);
qmlRegisterType<ProcessManager>("com.utils", 1, 0, "ProcessManager");
QGuiApplication app(argc, argv);
QQmlApplicationEngine engine;
engine.load(QUrl(QStringLiteral("qrc:/main.qml")));
if (engine.rootObjects().isEmpty())
return -1;
return app.exec();
}
#include "main.moc"
main.qml
import QtQuick 2.9
import QtQuick.Window 2.2
import QtQuick.Controls 2.5
import com.utils 1.0
Window {
visible: true
width: 640
height: 480
title: qsTr("Hello World")
ProcessManager{
id: pm
onBusySignal: busy.running = true
onReadySignal: busy.running = false
}
BusyIndicator {
id: busy
running: false
}
Button{
anchors.top: busy.bottom
text: "start process"
onClicked: pm.start_process()
}
}