当前位置: 代码迷 >> QT开发 >> 怎么C语言编程触发QFileSystemWatcher类发出fileChanged()信号
  详细解决方案

怎么C语言编程触发QFileSystemWatcher类发出fileChanged()信号

热度:24   发布时间:2016-04-25 04:03:39.0
怎样C语言编程触发QFileSystemWatcher类发出fileChanged()信号
实现功能:当/home/ma/ma.txt文件被修改后,实时显示在TextEdit文本框里,用C语言对文件ma.txt修改(open()/write()等函数)则触发不了fileChanged(),但是用VIM打开时可以触发,我该怎么C编程来实现fileChanged()信号触发呢?

------解决方案--------------------
另外,所有对文件的操作一定会发射信号的。也许你的代码有问题,我简单写了一个,测试没问题的。


CMainForm::CMainForm(QWidget *parent)
    : QWidget(parent),
      ui(new Ui::CMainForm)
{
    ui->setupUi(this);

    watcher.addPath("/home/xiachm/123");
    connect(&watcher, SIGNAL(fileChanged(const QString &)), this, SLOT(fileChanged(const QString &)));
}

void CMainForm::fileChanged(const QString &path)
{
    qDebug() << path;
}

void CMainForm::on_button_clicked()
{
    QFile file("/home/xiachm/123");
    file.open(QIODevice::WriteOnly);
    file.write("xiachm");
    file.close();
}