[Interest] QFileSystemWatcher malfunctioning

Sujan Dasmahapatra sdh at lmwindpower.com
Thu Feb 2 14:19:25 CET 2012


Dear Thorsen
I need your help. I have implemented the SignalBlocker class in a new
header file;
////////////////////////////////////////////////////////////////////////
/////////////////////////////////////////////
#ifndef SIGNALBLOCKER_H
#define SIGNALBLOCKER_H

/**
 * Small helper class that blocks all signals from an object for the
lifetime of this object.
 * it is safe against deletion of the object before deletion of this.
 *
 * This class was written by Bo Thorsen of Fionia Software
<bo at fioniasoftware.dk>.
 * The code is in the public domain.
 */
class SignalBlocker {
public:
    explicit SignalBlocker(QObject* object) : mObject(object) {
        mWasBlocked = object->signalsBlocked();
        object->blockSignals(true);
    }

    ~SignalBlocker() {
        if (mObject && !mWasBlocked)
            mObject->blockSignals(false);
    }

private:
    // Disabled
    SignalBlocker(const SignalBlocker&);
    SignalBlocker& operator=(const SignalBlocker&);

    QPointer<QObject> mObject;
    bool mWasBlocked;
};


#endif // SIGNALBLOCKER_H
////////////////////////////////////////////////////////////////////////
///////////////////////////////////////

Now I have 2 .cpp files. One is plotgraphs.cpp and other is
mainwindow.cpp
So I want to block the signal at some function in mainwindow.cpp
And my QFileSystemWatcher object is initialized and connected  in
plotgraphs.cpp file. Like below.

//plotgraphs.cpp
QFileSystemWatcher *watcher;
/////////////////////////////////////
PlotGraphsDlg::PlotGraphsDlg(MainWindow *parent)
{
    watcher = new QFileSystemWatcher(this);
    connect(watcher,SIGNAL(fileChanged(const QString &)),this,
SLOT(cfgFileModified(const QString &)));
}
/////////////////////////////////////

And in mainwindow at some function I am modifying the file. So in that
function in the beginning I am writing this
/////////////////////////////////////////
void MainWindow::saveCFG()
{
    if(!plotgraphsdlg)
    return;
    SignalBlocker blocker(plotgraphsdlg->watcher); //blocking the signal
    QString update;
    QFile file(filename);
    if(!file.exists())
    return;
    if(!file.open(QIODevice::WriteOnly))
    qWarning("Cannot Open file % s", filename);
    QTextStream out(&file);
   out << update;  //This is updating the file.....
}
/////////////////////////////////////////


But after updating I am still getting the message that the file is
updated. The signal is not blocked. Please tell me whats going wrong in
this. Thanks in advance for your help.


Kind regards,


Sujan Dasmahapatra
Project Leader, Aero Group
CE - Aero Group

Tel  +91 80 66470248
Mob  
sdh at lmwindpower.com


LM Wind Power Blades
lmwindpower.com

Together we capture the wind to power a cleaner world


This e-mail and any attachments are confidential. If you are not the
named or intended recipient, please notify the sender immediately and do
not disclose the contents to any other person, use it for any purpose,
or store or copy the information in any medium. Any unauthorized
disclosure, use or storage is prohibited and might be unlawful.
-----Original Message-----
From: interest-bounces+sdh=lmwindpower.com at qt-project.org
[mailto:interest-bounces+sdh=lmwindpower.com at qt-project.org] On Behalf
Of Bo Thorsen
Sent: Thursday, January 12, 2012 1:45 PM
To: interest at qt-project.org
Subject: Re: [Interest] QFileSystemWatcher malfunctioning

Den 12-01-2012 08:35, Andreas Pakulat skrev:
> On 12.01.12 08:01:17, Sujan Dasmahapatra wrote:
>> Dear Friends
>>
>> I am using QFileSystemWatcher for knowing when the file is modified
from
>> outside my application. I am seeing even if the file is modified from
>> the application this is giving message that the file is modified. Can
>> anyone tell whats going wrong in this..see the snippet below
>
> There is nothing going wrong here, thats how QFileSystemWatcher is
> intended to work. If you don't want to be notified for changes you're
> doing yourself you need to add code for that in your application.

And this is of course the task for a SignalBlocker. I've done exactly 
this (block QFileSystemWatcher) for a customer a couple of months ago, 
and it really is the only solution.

I just put my SignalBlocker implementation on my blog:

http://www.fioniasoftware.dk/blog/?p=158

(Sorry for the plug but it was a bit long for a mail, and there might be

other interested in it.)

With this class you just do this:

{
   SignalBlocker blocker(watcher);
   modifyTheFile();
}

I hope this helps.

Bo Thorsen,
Fionia Software.

-- 

Expert Qt and C++ developer for hire
Contact me if you need expert Qt help
http://www.fioniasoftware.dk
_______________________________________________
Interest mailing list
Interest at qt-project.org
http://lists.qt-project.org/mailman/listinfo/interest
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.qt-project.org/pipermail/interest/attachments/20120202/7f2b922f/attachment.html>


More information about the Interest mailing list