blog about qt and qxt related stuff

Montag, 27. August 2007

About locks and validations

Hi there,

last time i added two new classes to Qxt:
  • QxtStringValidator
  • QxtFileLock.

QxtStringValidator was initialliy a class to validate over a QStringList, but this was not really worth to put it into Qxt. So i decided to make it more powerfull. Now it provides a String based validation over a QAbstractItemModel. :


QxtStringValidator validator = new QxtStringValidator();
validator->setLookupModel(myCoolCustomModel);

/*now we have to choose the right column
this will lookup in column 3, starting from row 0
*/
validator->setStartModelIndex(myCoolCustomModel->index(0,3));
//case insensitive lookup
validator->setCaseSensitivity(Qt::CaseInsensitive);
//and now set the validator to a lineEdit
lineEdit->setValidator(validator);

There are some other features like :
  • wrapping lookup
  • case sensitive/insensitive lookup
  • recursive lookups
  • looking up different model rows
And it is also possible to use a QStringList as data source (never forget your roots ;) ).

QxtFileLock is something i had in my mind for a long time since i thought about writing a isam filemanager based on QT. The first thing i realized was that there is no file lock available in Qt. Well it is possible but not using a nice crossplattform class like Qt offers them. So here is my solution for that:


void doSomeOpOnFile()
{
off_t lockStart = 0x10;
off_t lockLength = 20;
QxtFileLock readLock(&file,
lockStart,lockLength,QxtFileLock::ReadLock);
if(readLock.lock())
{
/*do some read operations*/
/*the lock gets cleaned up after it is deleted it is not needed to call unlock()*/
QxtFileLock writeLock (
&file,lockStart,lockLength,QxtFileLock::WriteLock);
if(writeLock.lock())
{
/*do some write operations*/
}
}
/*in this case we don't have to care about releasing the lock. QxtFileLock will do it when the instance is destroyed*/
}

I had to do some extra work on *nix based systems. On *nix a lock does not behave like a normal unix lock. In fact on *nix locks are process wide, means a process can get a lock on a file as often as it wants to. With QxtFileLock this is not possible, locks are threads and handles bound. That means: A Thread can writelock the same region of a file twice only if it uses the SAME handle. If a different handle holds the lock a call to lock() will fail.
On *nix we use fcntl and on windows LockFileEx to implement the locking stuff.

Attention: don't mix QxtFileLock and lockf or flock on linux they are NOT compatible.
Also use only fcntl to lock the files in other apps.

Well i hope these two classes can go into the 0.2.4 release.
Due to vacation and other things i had to do the QxtScheduleView did not make it into the trunk.

zbenjamin
"your friendly developer from the neighbourhood"