PDA

View Full Version : Problem: QThead and QTableview



ederbs
6th November 2007, 22:08
Hello Friends,

I am having problems of communication between Thread and my MainWindowForm.

Inside the Thread I run a method for upgrading the QTableView, I am getting more errors.

Here Class Header:



class ThreadNotify : public QThread
{
Q_OBJECT

public:
ThreadNotify(QObject *parent = 0);
virtual ~ThreadNotify();

void setCapsule(MainWindowForm *pointMainCapsule);

MainWindowForm *accessMainCapsule;

void setMessage(const QString &message);
void stop();

signals:

void transactionStarted(const QString &message);

....

protected:

void run();
....
};

class MainWindowForm : public QMainWindow, public Ui::MainWindowForm
{
Q_OBJECT

public:

MainWindowForm(QWidget *parent = 0);
virtual ~MainWindowForm();

ThreadNotify instanceThreadNotify;
QSqlQueryModel *model;

void modelRefresh();
...
}


Here Code:




ThreadNotify::ThreadNotify(QObject *parent)
: QThread (parent)
{
stopped = false;
}

ThreadNotify::~ThreadNotify()
{
/** Destroys the object and frees any allocated resources */
}

void ThreadNotify::setCapsule(MainWindowForm *pointMainCapsule)
{
accessMainCapsule = pointMainCapsule;
}

....

void ThreadNotify::run()
{
while (!stopped == nnotifies < N_NOTIFICATIONS)
{
int sock;
fd_set input_mask;

sock = PQsocket(conn);

if (sock < 0)
break;

FD_ZERO(&input_mask);
FD_SET(sock, &input_mask);

if (select(sock + 1, &input_mask, NULL, NULL, NULL) < 0)
{
(void)exit_nicely(conn);
}

PQconsumeInput(conn);

while ((notify = PQnotifies(conn)) != NULL)
{
qApp->lock();

accessMainCapsule->modelRefresh();

qApp->unlock();
PQfreemem(notify);
nnotifies++;
}
}

PQfinish(conn);
stopped = false;
}

...

MainWindowForm::MainWindowForm(QWidget *parent)
: QMainWindow(parent)
{
...
}

void MainWindowForm::modelRefresh()
{
QApplication::setOverrideCursor(QCursor(Qt::WaitCu rsor));

model->setQuery(QString("SELECT ....");

if (model->lastError().isValid())
{
qDebug() << model->lastError();
}

QApplication::restoreOverrideCursor();
}

...


When I receive the notification of PostgreSQL running on the Thread, the method of class MainWindowForm is triggered.

More get the following error:

=======ERROR====================================
QObject::connect: Cannot queue arguments of type 'QModelIndex'
(Make sure 'QModelIndex' is registered using qRegisterMetaType().)
QObject::connect: Cannot queue arguments of type 'QModelIndex'
(Make sure 'QModelIndex' is registered using qRegisterMetaType().)
QObject::connect: Cannot queue arguments of type 'QModelIndex'
(Make sure 'QModelIndex' is registered using qRegisterMetaType().)
QObject::connect: Cannot queue arguments of type 'QModelIndex'
(Make sure 'QModelIndex' is registered using qRegisterMetaType().)

Notify ==> ASYNC NOTIFY trigger 'nalertupdatetab' backend pid 21611
=======ERROR====================================

I used something similar in Qt3 with QDataTable, and not had that problem, someone could help me with that?

Thanks, edm.

wysota
6th November 2007, 22:12
You can type in

qRegisterMetaType<QModelIndex>("QModelIndex");
in main(). But I wouldn't do that, the index may become invalid before the slot is triggered. Why don't you use the PostgreSQL driver that comes with Qt? You could avoid the worker thread and have a nice object oriented interface to your database.

This might be interesting for you as well:
http://labs.trolltech.com/blogs/2007/11/02/asynchronous-database-event-notifications/

ederbs
7th November 2007, 16:24
Hummm... ok,

Interestingly the new functionality of Qt 4.4, more is still in the snapshot.

One question, with subscribeToNotification() I will not need to use the
most Thread?

Thanks, edm

jpn
8th November 2007, 13:46
edm, I find this thread confusing because you have marked Qt3 alone as used products. Furthermore, QApplication::lock() / QApplication::unlock() only exist in Qt3, whereas QModelIndex only exists in Qt4.

Eldritch
9th November 2007, 03:32
The gotcha here (and it's subtle) is that down in the bowels of all this, there's a signal / slot connection that fails because, as you saw, QModelIndex can't be part of an async connection. Since a model index is not 'safe' to post across threads, this limitation exists with good reason. Trying to register QModelIndex as a meta type is doomed to fail.

So, what can you do? Well, first, a question: Must the update be done synchronously? If not, I suggest posting a custom event over to your main window, and have the handler for the custom event execute the refresh.

If it needs to be synchronous, you can create a custom event, post it, and wait for it to finish before posting again. If you're worried about flooding the queue with posted events, you can always employ the various mechanisms Qt has to prevent this kind of thing.

In our apps, we've had to deal with this problem. What we found was that finding out / understanding what was happening and why was way harder than 'fixing' the problem. (Surprising how often that's the case. ;D ).

Good luck!

wysota
9th November 2007, 11:17
One question, with subscribeToNotification() I will not need to use the
most Thread?
Right, no extra threads needed.