QProcess and powershell - checking if the disk is clean
-
@Piotrek102
Did you see the UPDATE I added to my earlier post, aboutQStorageInfo
? If that gives you the same information as you want from your PS script, it will be easier to use than running a script and parsing the output. Up to you..... -
I use my own function to work with QProcess:
#include "mainwindow.h" #include "ui_mainwindow.h" bool process_ok; QStringList to_return; void MainWindow::encodingFinished() { process_ok=true; } void MainWindow::readyReadStandardOutput() { to_return << process->readAllStandardOutput(); } QStringList MainWindow::RunCommand(QString cmd, QStringList commands) { to_return.clear(); process_ok=false; process->start(cmd, commands); while(process_ok!=true) delay_MSec(100); return to_return; }
So to call the ps1 script I use it like this:
QStringList returned; command = qApp->applicationDirPath()+"/scripts/getdisk.ps1"; commands << command; returned = RunCommand("powershell.exe", commands);
-
@JonB
I will check it, thank you for the hint -
@Piotrek102 said in QProcess and powershell - checking if the disk is clean:
while(process_ok!=true) delay_MSec(100);
You must/should not do this sort of thing in Qt programming. If you really want to wait, you must use
QProcess::waitForFinished()
. -
@JonB
That's right, I know it, but QProcess :: waitForFinished () was freezing my gui. this feature doesn't do that and works fine, I have tested it multiple times in my other programs. I know this is not correct but it works.void MainWindow::delay_MSec(unsigned int msec) { QTime _Timer = QTime::currentTime().addMSecs(msec); while( QTime::currentTime() < _Timer ) QCoreApplication::processEvents(QEventLoop::AllEvents, 100); }
To be clear, this is not my function, I found it on the internet
@JonB
As for your suggestion, QStorageInfo is what I need! It works very well. I have only one question. Is there also an option to specify a disk index - e.g. disk 0?foreach (const QStorageInfo &storage, QStorageInfo::mountedVolumes()) { if (storage.isValid() && storage.isReady()) { if (!storage.isReadOnly()) { qDebug() << "name:" << storage.name(); qDebug() << "fileSystemType:" << storage.fileSystemType(); qDebug() << "size:" << storage.bytesTotal()/1000/1000 << "MB"; qDebug() << "availableSize:" << storage.bytesAvailable()/1000/1000 << "MB"; //qDebug() << index of disk here too } } }
-
@Piotrek102 said in QProcess and powershell - checking if the disk is clean:
QProcess :: waitForFinished () was freezing my gui
void MainWindow::delay_MSec(unsigned int msec)
I did not know what was in your
delay_MSec()
. UsingprocessEvents()
like this is not ideal. The best way is no synchronicity/waits/loops/processEvents, rather let it run asynchronously and continue your code in your slot onQProcess::finished()
signal.If the code works for you fair enough. It's moot anyway if you choose to use
QStorageInfo
instead.Is there also an option to specify a disk index - e.g. disk 0?
I know no more than whatever is in
QStorageInfo
doc page. I have never used it :) [You can use an integer indexer intomountedVolumes()
instead offoreach
of that's all your "disk index - e.g. disk 0" is, I don't know what ordermountedVolumes()
returns them in?] -
@Piotrek102 Please read the documentation.
You can list mounted volumes using mountedVolumes() static method.
Also please drop
foreach
, it's obsolete. Just iterate over items in the list. -
@Piotrek102 Do this in a thread to avoid any delay call(==>as less as possible) since it may take a while to get all storage info. GUI is not blocked in this way.
-
This post is deleted!