From 10:00 CET Friday 22nd November we will adjust how the server works to deal with some recently reported problems. Therefore there may be a load problem, if you experience more problems than usual trying to access the forum then please PM AndyS or any of the moderators so they can inform me.


How do i acces an other slot?



  • Hello,

    I like to know how i can acces something i created in an other slot?

    void MainWindow::NewLog()
    {   QFile file (QCoreApplication::applicationDirPath() +"/logs/"+ui->lineEdit->text() +".txt");
        file.open(QIODevice::WriteOnly | QIODevice::Text);
        
    qDebug()<<file.fileName();
    }
    
    
    void MainWindow::saveSettings()
    {   QString c_time = QTime::currentTime().toString();
        QString sText = c_time + " " 
                + ui->label_4->text()+ " "
                + ui->label_4->text()+ " "
                + ui->label_4->text();
        
    qDebug()<< sText;
    
        QTextStream out(&file);
           out << sText;
    }
    

    This is my code, now i want to acces "file" from NewLog in the SaveSettings slot, is this possible?
    i dont like to put it in the same slot, because now i can call SaveSettings with a timer and it will write the data to the log, without creating a a new log/destroing the last data.
    Thnx



  • slots are normal functions with some additional abilities added by qt. and since file is a local variable in NewLog(), you can't access it from saveSettings().

    you can keep the path of the file (a string) as MainWindow's member, then create a QFile object using the path in your both slots.


  • Qt Champions 2018

    @GhostWolf

    and to add to @user4592357,

    you can even have QFile *file as member variable, you just have to make sure it is completely set up correctly before using it in saveSettings().

    Regards



  • I tried some different options, and found a solution:

    
    void MainWindow::saveSettings()
    {
        name = (QCoreApplication::applicationDirPath() +"/logs/"+ui->lineEdit->text() +".txt");
           QFile file (name) ;
           if (newfile == true){
           if (file.exists()){
               qDebug ()<<"already exists";
               NewLog();
               timer->stop();
    
           } else {
               qDebug()<< "Does not exist";
    
               file.open(QIODevice::WriteOnly | QIODevice::Text | QIODevice::Append);
                  QString c_time = QTime::currentTime().toString();
                  QString sText =QString::number(update) + ". "+ c_time + ": rpm = "+ rpm + ", load = "+ QString::number(load) + ", "
                            + name1+ " = " + result1 + ", "+ name2+ " = " + result2 + ", "
                            + name3+ " = " + result3 + ", "+ name4+ " = " + result4 + ", "
                            + name5+ " = " + result5 + ", "+ name6+ " = " + result6 + ", "
                            + name7+ " = " + result7 + ", "+ name8+ " = " + result8 + ", "
                            + name9+ " = " + result9 + ", "+ name10+ " = " + result10;
    
                   ui->label_log->setText(sText);
    
                   update++;
    
                   QTextStream out(&file);
                   out << sText + "\n";
                   file.close();
    
                   newfile = false;
    
           }}else{
               file.open(QIODevice::WriteOnly | QIODevice::Text | QIODevice::Append);
                  QString c_time = QTime::currentTime().toString();
                  QString sText =QString::number(update) + ". "+ c_time + ": rpm = "+ rpm + ", load = "+ QString::number(load) + ", "
                            + name1+ " = " + result1 + ", "+ name2+ " = " + result2 + ", "
                            + name3+ " = " + result3 + ", "+ name4+ " = " + result4 + ", "
                            + name5+ " = " + result5 + ", "+ name6+ " = " + result6 + ", "
                            + name7+ " = " + result7 + ", "+ name8+ " = " + result8 + ", "
                            + name9+ " = " + result9 + ", "+ name10+ " = " + result10;
    
                   ui->label_log->setText(sText);
    
                   update++;
    
                   QTextStream out(&file);
                   out << sText + "\n";
                   file.close();
    
    
    }}
    

    Maybe it can be easier, but i am still learning. Thanks for your input.
    If you have comments on how to change my code, please tell me.
    (newlog was only to test something else now)