Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. How do i acces an other slot?
Forum Update on Monday, May 27th 2025

How do i acces an other slot?

Scheduled Pinned Locked Moved Unsolved General and Desktop
slotslogqfiletimerqtextstream
4 Posts 3 Posters 1.1k Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • G Offline
    G Offline
    GhostWolf
    wrote on last edited by
    #1

    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

    aha_1980A 1 Reply Last reply
    0
    • U Offline
      U Offline
      user4592357
      wrote on last edited by
      #2

      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.

      1 Reply Last reply
      2
      • G GhostWolf

        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

        aha_1980A Offline
        aha_1980A Offline
        aha_1980
        Lifetime Qt Champion
        wrote on last edited by
        #3

        @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

        Qt has to stay free or it will die.

        1 Reply Last reply
        1
        • G Offline
          G Offline
          GhostWolf
          wrote on last edited by GhostWolf
          #4

          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)

          1 Reply Last reply
          0

          • Login

          • Login or register to search.
          • First post
            Last post
          0
          • Categories
          • Recent
          • Tags
          • Popular
          • Users
          • Groups
          • Search
          • Get Qt Extensions
          • Unsolved