PDA

View Full Version : QT signal, function



dechiic
18th May 2015, 22:38
Hello all, im new and i want help ;).

I have my function for select file :


QString MainWindow::on_Browse_clicked()
{
QString fichier = QFileDialog::getOpenFileName(this, "Ouvrir un fichier", QString(), "Exe (*.exe)");
QMessageBox::information(this, "test", "Vous avez sélectionné :\n" + fichier);
QFileInfo fileInfo(fichier);
QDir dir = fileInfo.dir(); // Dossier du fichier
QString dirPath = fileInfo.filePath(); // Path vers le fichier
QString fileName = fileInfo.fileName(); // Nom du fichier (sans le path)
ui->lineEdit->setText(fichier);
return fichier;
}


And here my function for start the file :

void MainWindow::on_start_clicked(QString fichier)
{
QString open = QDesktopServices::openUrl(QUrl(fichier,QUrl::Toler antMode));
}

but it does not work, i have to connect my 2 buttons for get "QString fichier" variable ?
if yes how ?
Thanks all and sorry for my poor english :D !

ChrisW67
18th May 2015, 22:59
The push button called start does not have a signal clicked() that passes a string. The automatic connection mechanism never makes the connection so the button will never trigger this code.

You return a value from the browse slot, but this value has nowhere to go: slots cannot return values.

You should put the browse result in a member variable and access that from the click handler to get the value.

dechiic
19th May 2015, 00:07
thanks you, but i dont fully understand, my english is sad :s

i have make this :
QString MainWindow::on_Browse_clicked()
{
QString localtiondebol;
QString fichier = QFileDialog::getOpenFileName(this, "Ouvrir un fichier", QString(), "Exe (*.exe)");
QMessageBox::information(this, "Tipie launcher", "Vous avez sélectionné :\n" + fichier);
QFileInfo fileInfo(fichier);
QDir dir = fileInfo.dir(); // Dossier du fichier
QString dirPath = fileInfo.filePath(); // Path vers le fichier
QString fileName = fileInfo.fileName(); // Nom du fichier (sans le path)
ui->lineEdit->setText(dirPath);
localtiondebol = dirPath;
return localtiondebol;
}

my variable localtiondebol = the acess at the .exe, but i dont know how i can use "locationdebol" variable in my other function :


void MainWindow::on_start_clicked()
{
QString localtiondebol;
localtiondebol = on_Browse_clicked();
ui->lineEdit_3->setText(localtiondebol);
QDesktopServices::openUrl(QUrl(localtiondebol,QUrl ::TolerantMode));
}


i have try this
localtiondebol = on_Browse_clicked(); but its dont work

yeye_olive
19th May 2015, 11:27
No offense, but this is basic C++.

If 'localtiondebol' is meant to be set in a method and read in another, then it certainly should not be declared a a local variable in either method; it should be a non-static member of the class.

That being said, I wonder whether you need this variable at all. If I understand correctly, the Browse button lets the user select a file in a dialog and then puts its name in a QLineEdit (more precisely, ui->lineEdit). Later on, the start button does something with a file... but what file? Is it supposed to be the file originally selected in the dialog, or the file whose name appears in ui->lineEdit, which may have been manually altered by the user? In the latter case, all you need to do is read the text from ui->lineEdit in on_start_clicked().

dechiic
19th May 2015, 14:56
I did not know that I could retrieve the text of lineEdit.
it work well thanks you