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 inNewLog()
, you can't access it fromsaveSettings()
.you can keep the path of the file (a string) as
MainWindow
's member, then create aQFile
object using the path in your both slots. -
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 insaveSettings()
.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)