Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

How To Let Code Know To Run Below Code When Signal Is Emitted



  • Basically what i am attempting to do is have my program read in data from a json format in real time, and whenever the program reads in the data it starts reading it over again to see if anything has been updated, im sure there is a better way of going about it than what i am doing but I basically need my program to re run the code starting at the for statement once 'emit processingDone();' is hit. I cant just make a simple function because the program has mutliple different settings to read in different data so it would be easier if i can just tell it to "Start here and do this code below me over again." Below is an example of my current code

       //Retrieves json format of data
        Json::Value chartData = IEX::stocks::chartYtd(symbolSearchedStd);
    
        //Reads in data from json(historical data 1 day delayed)
        for(Json::Value::ArrayIndex i = 0 ; i != chartData.size(); i++)
        {
            if(chartData[i].isMember("close"))
            {
                closePrice[i] = (chartData[i]["close"].asDouble());
                time[i] = chartData[i]["date"].asString();
                QString temp = QString::fromStdString(time[i]);
                timeInEpoch[i] = QDateTime::fromString(time[i].c_str(), Qt::ISODate).toSecsSinceEpoch();
    
                if((closePrice[i] == 0) && (i != chartData.size() - 1))
                {
                    closePrice[i] = closePrice[i-1];
                }
    
                if(closePrice[i] > maxAvg)
                {
                    maxAvg = closePrice[i];
                }
    
                else if(closePrice[i] < minAvg)
                {
                    minAvg = closePrice[i];
                }
            }
        }
    
        stockData(closePrice[n-1], closePrice[n-2]);
    
        //Assigns data to graph
        ui->stockGraph->graph(0)->setData(timeInEpoch, closePrice);
    
        //Set x axis range
        ui->stockGraph->xAxis->setRange(timeInEpoch[0], timeInEpoch[n-1]);
    
        //Set y axis range
        QCPRange yAxis(minAvg + 2, maxAvg + 2);
        yAxis.normalize();
        yAxis.center();
        ui->stockGraph->yAxis2->setRange(minAvg - 10, maxAvg + 10);
        ui->stockGraph->replot();
    
        //Signal that data is read in
        emit processingDone();
    
    

    Also im sure this is probably not the best way about going about this but I am extremly confused on how to use SSE Streaming and I am using the IEX Cloud API, is there also a way to rerun the for loop IF the json file is changed. You can see when i read in the json file at the top currently, but i wouldnt know how to get a signal from it if new data has been updated within the json.



  • @Laner107 said in How To Let Code Know To Run Below Code When Signal Is Emitted:

    I basically need my program to re run the code starting at the for statement once 'emit processingDone();' is hit.

    So put in a "while" loop!? Or just possibly put a queued (not direct!) connected slot on signal processingDone() which re-runs? Or maybe run this code on a QTimer every second or whatever?

    I cant just make a simple function because the program has mutliple different settings to read in different data so it would be easier if i can just tell it to "Start here and do this code below me over again."

    Don't understand what you are saying here.

    but i wouldnt know how to get a signal from it if new data has been updated within the json.

    I know nothing about this, but as you say it would be best if you can get some signal to say you need to do the re-read. As I said earlier, in the absence of this would a QTimer() say every second or whatever be usable, at least it would give time for the UI to run?



  • @JonB What i was saying is that the for statement is not dynamic, meaning that depending on the options the user selects there may be different values within the for statement, thus I cant just make it a function as it changes depending on which function it is in. I just need to figure out how to simply tell it to run the code beneath it without actually making it into a function and connecting it with a signal, i will give a shot with what you have said above though.



  • @Laner107
    I still don't really (at all!) understand what you are saying, maybe someone else will :)

    Purely at a guess, maybe you are saying some of the variables in the code block need to change if the user makes changes in the UI?? In that case, maybe the suggestion of re-running the block on a QTimer() instead of a tight loop will mean that you re-pick up from new UI values as you re-run the block each time....



  • Why don't you just put the for loop in a function?
    Then the function can get called when needed, and the options the user selects can be passed to the function.
    I don't really get why this isn't an option?


Log in to reply