update text edit at run time.
-
@mbatra please check the bool return value of addPath call, because I don't know of a file system that uses
:
as a valid beginning for a path@J-Hilk
Hi,
I have added this now, it returns false;
bool b = watcher->addPath("D:\Putty_Log\log.log");
Can we specify the full path like this. Does it accepts.
It also gives this error message:
QObject::connect: No such signal QFileSystemWatcher::fileChanged(QString &)
QObject::connect: (receiver name: 'MainWindow')Thanks.
-
@J-Hilk
Hi,
I have added this now, it returns false;
bool b = watcher->addPath("D:\Putty_Log\log.log");
Can we specify the full path like this. Does it accepts.
It also gives this error message:
QObject::connect: No such signal QFileSystemWatcher::fileChanged(QString &)
QObject::connect: (receiver name: 'MainWindow')Thanks.
@mbatra said in update text edit at run time.:
bool b = watcher->addPath("D:\Putty_Log\log.log");
@mbatra When using backslash in string you must escape using double
\\
, which gives youD:\\Putty_Log\\log.log
as a path.@mbatra said in update text edit at run time.:
void MainWindow::onstartButtonClicked()
{
watcher = new QFileSystemWatcher(this);
watcher->addPath(":/log.log");
connect(watcher, SIGNAL(fileChanged(QString)), this, SLOT(slotFileChanged(QString)));}
You don't need necessarily to use a pointer for your QFileSystemWatcher member object. As you have done here this will create a new instance every time
onstartButtonClicked
is called, which is probaly not what you want. But it will probably work anyway. -
Hi,
Thanks for your response. Now, I have added the below code:
.h file:
QFileSystemWatcher *watcher;
public slots:
void slotFileChanged(QString &text);.cpp file:
void MainWindow::slotFileChanged(QString &text)
{
qDebug()<<"slotFileChanged Called\n";
qDebug()<<text<<endl;
}void MainWindow::onstartButtonClicked()
{
watcher = new QFileSystemWatcher(this);
watcher->addPath(":/log.log");connect(watcher, SIGNAL(fileChanged(QString)), this, SLOT(slotFileChanged(QString)));
}
is it correct now.
Still I am not getting the messages displayed in the slotchanged function.
@mbatra said in update text edit at run time.:
watcher->addPath(":/log.log");
This is a Qt resource path. It won't be accepted for watching, and in any case Qt resource files are read-only so it will never change.
bool b = watcher->addPath("D:\Putty_Log\log.log");
Single backslash characters must be doubled in a C++ literal string.
connect(watcher, SIGNAL(fileChanged(QString)), this, SLOT(slotFileChanged(QString)));
QObject::connect: No such signal QFileSystemWatcher::fileChanged(QString &)
It is much easier if you use the "new"-style
connect()
syntax, which was introduced over a decade ago.The signature is
QFileSystemWatcher::fileChanged(const QString &)
, I don't know thatSLOT()
let's you get away without thatconst
.As @Gojir4 says, you should not create a new
QFileSystemWatcher
object each time a button is pressed. -
@mbatra said in update text edit at run time.:
watcher->addPath(":/log.log");
This is a Qt resource path. It won't be accepted for watching, and in any case Qt resource files are read-only so it will never change.
bool b = watcher->addPath("D:\Putty_Log\log.log");
Single backslash characters must be doubled in a C++ literal string.
connect(watcher, SIGNAL(fileChanged(QString)), this, SLOT(slotFileChanged(QString)));
QObject::connect: No such signal QFileSystemWatcher::fileChanged(QString &)
It is much easier if you use the "new"-style
connect()
syntax, which was introduced over a decade ago.The signature is
QFileSystemWatcher::fileChanged(const QString &)
, I don't know thatSLOT()
let's you get away without thatconst
.As @Gojir4 says, you should not create a new
QFileSystemWatcher
object each time a button is pressed.Hi Jon,
Thanks for your response. Now the code is working fine. I have created the QFileSystemWatcher object in the constructor only once...not in the button everytime.
Thanks for correcting me.
Now the file is getting checked and when its updated the text edit contents are also updated, but only when I click on the button, the contents are getting updated in the text edit.
I want the text edit contents should get updated automatically (dynamically) as and when the contents in the txt file is getting changed.Please see the code below:
void MainWindow::onstartButtonClicked()
{
textEdit->show();bool b = watcher->addPath("D:/Putty_Log/Putty.log"); qDebug()<<"\nAddPath Return Value = "<<b; emit slotFileChanged("D:/Putty_Log/Putty.log");
}
void MainWindow::slotFileChanged(QString text)
{
qDebug()<<"slotFileChanged Called\n";
qDebug()<<text<<endl;QFile inputFile(text); inputFile.open(QIODevice::ReadOnly); if (!inputFile.isOpen()) return; QTextStream stream(&inputFile); for (QString line = stream.readLine(); !line.isNull(); line = stream.readLine()) { textEdit->append(line); };
}
Any help would be appreciated.
-
Hi Jon,
Thanks for your response. Now the code is working fine. I have created the QFileSystemWatcher object in the constructor only once...not in the button everytime.
Thanks for correcting me.
Now the file is getting checked and when its updated the text edit contents are also updated, but only when I click on the button, the contents are getting updated in the text edit.
I want the text edit contents should get updated automatically (dynamically) as and when the contents in the txt file is getting changed.Please see the code below:
void MainWindow::onstartButtonClicked()
{
textEdit->show();bool b = watcher->addPath("D:/Putty_Log/Putty.log"); qDebug()<<"\nAddPath Return Value = "<<b; emit slotFileChanged("D:/Putty_Log/Putty.log");
}
void MainWindow::slotFileChanged(QString text)
{
qDebug()<<"slotFileChanged Called\n";
qDebug()<<text<<endl;QFile inputFile(text); inputFile.open(QIODevice::ReadOnly); if (!inputFile.isOpen()) return; QTextStream stream(&inputFile); for (QString line = stream.readLine(); !line.isNull(); line = stream.readLine()) { textEdit->append(line); };
}
Any help would be appreciated.
@mbatra said in update text edit at run time.:
emit slotFileChanged("D:/Putty_Log/Putty.log");
And where did you connect the signal from the file watcher to your slot? How is it supposed to work when you don't connect the slot?
-
Hi Jon,
Thanks for your response. Now the code is working fine. I have created the QFileSystemWatcher object in the constructor only once...not in the button everytime.
Thanks for correcting me.
Now the file is getting checked and when its updated the text edit contents are also updated, but only when I click on the button, the contents are getting updated in the text edit.
I want the text edit contents should get updated automatically (dynamically) as and when the contents in the txt file is getting changed.Please see the code below:
void MainWindow::onstartButtonClicked()
{
textEdit->show();bool b = watcher->addPath("D:/Putty_Log/Putty.log"); qDebug()<<"\nAddPath Return Value = "<<b; emit slotFileChanged("D:/Putty_Log/Putty.log");
}
void MainWindow::slotFileChanged(QString text)
{
qDebug()<<"slotFileChanged Called\n";
qDebug()<<text<<endl;QFile inputFile(text); inputFile.open(QIODevice::ReadOnly); if (!inputFile.isOpen()) return; QTextStream stream(&inputFile); for (QString line = stream.readLine(); !line.isNull(); line = stream.readLine()) { textEdit->append(line); };
}
Any help would be appreciated.
@mbatra said in update text edit at run time.:
emit slotFileChanged("
Why emit?
Simply call slotFileChanged as normal method.
If it does not work: did you do any debugging? Is slotFileChanged called? What happens inside slotFileChanged (can the file be successfully opened?). -
@mbatra said in update text edit at run time.:
emit slotFileChanged("
Why emit?
Simply call slotFileChanged as normal method.
If it does not work: did you do any debugging? Is slotFileChanged called? What happens inside slotFileChanged (can the file be successfully opened?). -
@mbatra said in update text edit at run time.:
emit slotFileChanged("
Why emit?
Simply call slotFileChanged as normal method.
If it does not work: did you do any debugging? Is slotFileChanged called? What happens inside slotFileChanged (can the file be successfully opened?). -
Hi,
Yes, when I call emit slotFileChanged("D:/Putty_Log/Putty.log");, its called and file is also opened. But when the file contents are getting updated, then this signal doesn't getting emitted. Its emitted only once.
@mbatra There is NO need for emit at all!
I just realised that your code makes no sense:void MainWindow::onstartButtonClicked() { textEdit->show(); bool b = watcher->addPath("D:/Putty_Log/Putty.log"); qDebug()<<"\nAddPath Return Value = "<<b; emit slotFileChanged("D:/Putty_Log/Putty.log"); }
You need to call slotFileChanged in the slot connected to the signal from the watcher. Currently you call it in onstartButtonClicked - so why should it be called everytime the file is edited? Think about the logic.
-
If I call
connect(watcher, SIGNAL(fileChanged(QString)), this, SLOT(slotFileChanged(QString)));Signal doesn't get emitted at all.
@mbatra said in update text edit at run time.:
connect(watcher, SIGNAL(fileChanged(QString)), this, SLOT(slotFileChanged(QString)));
This ought be all you need. When the watcher sees a file change it emits the
fileChanged()
signal and yourslotFileChanged()
slot should be called. -
@mbatra said in update text edit at run time.:
connect(watcher, SIGNAL(fileChanged(QString)), this, SLOT(slotFileChanged(QString)));
This ought be all you need. When the watcher sees a file change it emits the
fileChanged()
signal and yourslotFileChanged()
slot should be called. -
-
@mbatra
Now that you have it working, how about changing over to New Signal Slot Syntax:connect(watcher, &QFileSystemWatcher::fileChanged, this, &MainWindow::slotFileChanged);
Use this instead of
SIGNAL
/SLOT()
syntax going forward for all yourconnect()
s, you will get e.g. compile-time checking that the signal and slot methods and arguments match. -
@mbatra
Now that you have it working, how about changing over to New Signal Slot Syntax:connect(watcher, &QFileSystemWatcher::fileChanged, this, &MainWindow::slotFileChanged);
Use this instead of
SIGNAL
/SLOT()
syntax going forward for all yourconnect()
s, you will get e.g. compile-time checking that the signal and slot methods and arguments match. -
@mbatra
Now that you have it working, how about changing over to New Signal Slot Syntax:connect(watcher, &QFileSystemWatcher::fileChanged, this, &MainWindow::slotFileChanged);
Use this instead of
SIGNAL
/SLOT()
syntax going forward for all yourconnect()
s, you will get e.g. compile-time checking that the signal and slot methods and arguments match.Hi,
I am facing another issue. File contents are not getting updating dynamically. If I add anything from the keyboard into the txt file slotchanged signal gets emitted. But if I run a batch file and that batch file creates a log file which I am reading into my application, contents are getting updated in the log file but text edit not getting updated at the same time.
Any help would be appreciated.
-
Hi,
I am facing another issue. File contents are not getting updating dynamically. If I add anything from the keyboard into the txt file slotchanged signal gets emitted. But if I run a batch file and that batch file creates a log file which I am reading into my application, contents are getting updated in the log file but text edit not getting updated at the same time.
Any help would be appreciated.
@mbatra Do you mean the file does not exist at the beginning, but is created to some later point in time? If that is the case then I suggest to also add the folder where the file is created to QFileSystemWatcher, so that you get a notification when the file is created in that folder.
Because if you read the documentation:bool QFileSystemWatcher::addPath(const QString &path) Adds path to the file system watcher if path exists. The path is not added if it does not exist
"if path exists. The path is not added if it does not exist"
-
Hi,
I am facing another issue. File contents are not getting updating dynamically. If I add anything from the keyboard into the txt file slotchanged signal gets emitted. But if I run a batch file and that batch file creates a log file which I am reading into my application, contents are getting updated in the log file but text edit not getting updated at the same time.
Any help would be appreciated.
@mbatra
Exactly as @jsulm has written. If the file to be monitored does not yet exist you cannot place aQFileSystemWatcher
on it. You have to watch the directory first to see file creation viadirectoryChanged()
. Only once it has been created can you then start to monitor the file forfileChanged()
, so a two-step process for your case. -
@mbatra
Exactly as @jsulm has written. If the file to be monitored does not yet exist you cannot place aQFileSystemWatcher
on it. You have to watch the directory first to see file creation viadirectoryChanged()
. Only once it has been created can you then start to monitor the file forfileChanged()
, so a two-step process for your case.