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:
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;
void modelRefresh();
...
}
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();
...
}
To copy to clipboard, switch view to plain text mode
Here Code:
ThreadNotify
::ThreadNotify(QObject *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
) {
...
}
void MainWindowForm::modelRefresh()
{
model
->setQuery
(QString("SELECT ....");
if (model->lastError().isValid())
{
qDebug() << model->lastError();
}
}
...
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::WaitCursor));
model->setQuery(QString("SELECT ....");
if (model->lastError().isValid())
{
qDebug() << model->lastError();
}
QApplication::restoreOverrideCursor();
}
...
To copy to clipboard, switch view to plain text mode
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.
Bookmarks