Difference between...? A few noobish questions
-
Hey guys,
So I'm just starting to learn C++ and I decided to learn it through using the Qt GUI framework because things are a lot more automated. I'm fully fluent in VB.NET and C# (which is why I'm finding C++ quite easy to learn - they're very similar). I just have a couple of questions I was hoping someone can answer. :)
I'm making a GUI that allows you to write disc images to USB using dd on a Mac. The application starts out with a dialog asking for the sudo password, and when it is entered correctly, shows the main window where an image can be selected and written to USB.
I need to show the main window while closing the sudo dialog. My first question is, what is the difference between these two sets of code? Why does the first one do what I want, but the second one closes the whole application and never shows the main window? These sets of code are in the "OK" button's pressed slot on the sudo dialog.
The one that works:
@ MainWindow *main = new MainWindow;
main->show();
this->hide();@The one that doesn't:
@ MainWindow main;
main->show();
this->hide();@Secondly, I have the whole main window functioning, but my problem is that the commands I'm running using QProcess freeze the GUI. I would like the GUI to stay active while the commands are being run so the progress bar can be updated and so the status bar at the bottom can be updated by what is currently going on. I haven't programmed the progress bar yet (as seen in the code below), but I have programmed the status bar, however it doesn't get updated until the processes have completed (the very end of the writing). This is the code I have so far for the actual "writing" of the USB drive:
@void MainWindow::on_btnWrite_clicked()
{
string path = ui->txtImagePath->text().toStdString();
string drive = ui->cboDrives->currentText().toStdString();
QProcess unmount;
QProcess write;//do not procceed if all required fields are not filled if (ui->txtImagePath->text() == "" && ui->cboDrives->currentText() != "") { QMessageBox::critical(this, "Error", "No image has been selected."); return; } else if (ui->cboDrives->currentText() == "" && ui->txtImagePath->text() != "") { QMessageBox::critical(this, "Error", "No drive has been selected."); return; } else if (ui->txtImagePath->text() == "" && ui->cboDrives->currentText() == "") { QMessageBox::critical(this, "Error", "No image or drive has been selected."); return; } QMessageBox::StandardButton reply; reply = QMessageBox::warning(this, "Warning", "Are you sure you want to proceed? Anything on the drive you have selected will be overwritten!", QMessageBox::Yes | QMessageBox::No); if (reply == QMessageBox::Yes) { ui->statusBar->showMessage("Unmounting partitions..."); unmount.start(QString::fromStdString("diskutil unmountDisk " + drive)); //unmount partitions on selected drive ui->statusBar->showMessage("Writing image to disk..."); drive.insert(5, "r"); QString command = "sudo dd if=\"" + QString::fromStdString(path) + "\" of=" + QString::fromStdString(drive) + " bs=1m"; write.start(command); write.waitForFinished(-1); }@
Thanks in advance!!
-
Hi and welcome to devnet,
@
void MyWidget::myCoolFunction()
{
MainWindow main; <- allocated on the stack
main.show();
this->hide();
} <- main goes out of scope here and doesn't exist anymore@@
void MyWidget::myCoolFunction()
{
MainWindow *main = new MainWindow; <- allocated on the heap
main->show();
this->hide();
} main survives, but you have a memory leak since you don't tell main to delete on close@@
write.start(command);
write.waitForFinished(-1); <- you are actually telling write to freeze the current thread until it has done its job@Hope it helps
-
[quote author="SGaist" date="1411331362"]Hi and welcome to devnet,
@
void MyWidget::myCoolFunction()
{
MainWindow main; <- allocated on the stack
main.show();
this->hide();
} <- main goes out of scope here and doesn't exist anymore@@
void MyWidget::myCoolFunction()
{
MainWindow *main = new MainWindow; <- allocated on the heap
main->show();
this->hide();
} main survives, but you have a memory leak since you don't tell main to delete on close@@
write.start(command);
write.waitForFinished(-1); <- you are actually telling write to freeze the current thread until it has done its job@Hope it helps[/quote]
Thanks for clarifying!
However, the end part about telling it to freeze the current thread is needed. Without it, QProcess tries to execute too many commands at once and the commands are destroyed in the process.
-
[quote]Without it, QProcess tries to execute too many commands at once and the commands are destroyed in the process.[/quote]
You can move this long operation to a thread and when the operation is complete notify MainWindow about it.
-
Your QProcesses suffer the same syndrome as your main variable