Nominate our 2022 Qt Champions!

[SOLVED] I need the logic to pass data from one function to another to avoid global functions

  • Hi,

    I have a situation here that I cannot figure it out, ok let me re-phrase this, I could easily do this using a global variable but I'm trying to avoid it at all cost.

    What I have is a function which does two things, looks for a file in a bunch of directories and if it finds the file, it returns TURE and also sets a reference variable with the URL where the file was found...

    @// here is the searching function
    bool MainWindow::findFile(QString &URL)
    // searches for file...

    ...then I'm calling the function above in a second function where I'm setting some parameters if the file was found, and here is where my problem starts since I don't really need to set the URL in this function, here I only need to know whether the file was found or not; I need the URL to be set in a third function which will open the file when a button is clicked.

    @// here I'm using another reference variable to get the URL out since
    // it is not needed here
    void MainWindow::doesFileExists(QString &setURL)
    // I don't really need this here, I need it in another function
    QString getURL;

    setURL = getURL;

    if (findFile)
    // set some parameters...

    ... then I'm finally setting the URL in a third function where I originally wanted it

    @void MainWindow::on_pushButton_openFile()
    QString getPath;
    QDesktopServices::openUrl(QUrl("file:///"+ getPath));

    The above code works fine the only problem which to me is a huge problem, is the fact that I would like to search for the file only once and the way I currently have it it's searching for the file when calling the first function and every time the on_pushButton_openFile() function is called.

    In theory waht I really want to do is to be able to search for a file and if the file is found, set a QLabel to true to let the user know the result and also to set the URL so that if the user ever decides to open the file the program doesn't have to search for the file again.

    I hope that what I describe above makes sense.

    Thanks a lot.

Log in to reply