Qt GUI-只想在值更改时读取.txt文件
Qt GUI - Want to read from .txt file only when the value is changed
我实现了一个QTimer对象,用信号通知一个函数每秒从.txt文件读取一次,并创建另一个类的新对象。我的问题是,我想限制我的函数只创建一个对象,并不断检查.txt文件是否有任何更改。如何做到这一点?
以下是每秒钟执行一次的代码
void PutMeDown::signalReceived()
{
char buffer;
char currentState;
char prevState = '0';
int fd = open("/home/stud/test", O_RDWR, 0666);
if(fd < 0)
cout << "can't open file" << endl;
else
read(fd, &buffer, 1);//read from file
currentState = buffer;
if(currentState == prevState)
{
drive = new Drive(this);
drive->setWindowFlags(Qt::Window | Qt::FramelessWindowHint);
drive->show();
this->hide();
}
close(fd);
}
在Qt
-QFileSystemWatcher
中有一个特殊的类。这个类可以告诉您文件已更改(fileChanged
信号)。
QFileSystemWatcher * watcher = new QFileSystemWatcher(this);
watcher->addPath(mFileName);
connect(watcher,SIGNAL(fileChanged(QString)),SLOT(slot(QString)));
在插槽中,您可以读取文件或做其他事情。有了这个类,您就不需要使用QTimer
,它比每秒检查一次文件要好。
回到你关于1个物体的问题。最简单的解决方案是提供额外的bool
变量,在slot中检查该变量并创建新对象。您还可以提供一些方法将此变量更改为"外部",以便在需要时创建对象。
我还看到您使用non-Qt
方法来读取文件。Qt
中也有一个特殊的类:QFile。检查一下,也许你的任务允许你使用QFile
。
相关文章:
- 一种在C++中读取TXT配置文件的简单方法
- 如何读取 TXT 文件并仅将 int 存储在 C 中
- 如何在C++中读取.txt文件
- 尝试读取.txt文件并根据用户输入的字母拉出特定行
- 读取.txt文件C++
- 如何在 c++ 中使用 fstream 读取.txt文件
- 如何用 c++ 读取 txt 文件,如果我们不知道其中有多少个数据字符串,以及每行有多长?
- 要读取txt文件并从第二列中找到其最小值的第一列值
- C 如何通过特定n字符读取TXT文件的值
- C :按行读取txt文件的读数,单词用单词读取,每行的单词不均匀
- 读取.txt文件并将每行放入向量
- 动态分配读取 txt 文件的内存
- 读取.txt文件并组织成二维数组
- 如何从现有目录中读取TXT文件
- 尝试创建一个读取.txt文件,显示它,计数唯一单词的程序,并在使用了多少次的情况下显示独特的单词.C
- 读取.txt文件中的多行并输出每行的答案
- 在生成器 6 中使用 ifstream 读取 txt 文件C++
- 内核模式:如何逐行读取 txt 文件
- c++读取txt文件并复制到两个类istream::peek()中
- C :使用ARGV [1]到一个函数中读取.txt文件的错误