0

I have two classes: class A and class B.

In class A, I have a private slot Refresh which is called using QTimer every two seconds and helps in updating values in QTableView.

Class B is defined by QThread and in run function I am taking data from the client with the help of sockets and all.

Now the issue is that when run takes data from client then QTimer updates the table and thus updates in between without updating all the data. Sometimes it updates less and vice versa. This can be done if we sync in a way that as the data is taken the Refresh function does it work. But how can I do this? Because Refresh is of another class so I thought of a way to sync QTimer with sleep or a way by which I can call that function in class B only.

Cœur
  • 37,241
  • 25
  • 195
  • 267
Mcolorz
  • 145
  • 1
  • 5
  • 20
  • use shared data, use critical section for reading and updating it in QTableView and improve your timer interval. – ScarCode Jul 12 '12 at 11:37
  • @spyke even i now i should improve my timer interval.thats one obvious thing you are telling.m asking can i sync that because i have defined these two classes in different files so i cant even use global parameters. – Mcolorz Jul 12 '12 at 11:49
  • Obviously, you can start reading of data from other class and use mutex in the other file[the data reading class]. – ScarCode Jul 12 '12 at 11:54
  • can you please give me an example of mutex that how i have to declare it and how can i use it if the two functions are in two different file.please help me out,i am stuck over this from a long time @spyke – Mcolorz Jul 12 '12 at 12:16

2 Answers2

2

Using mutexes (QMutex) in Qt as @spyke suggested. Add a mutex in the class containing your data.

in header file of class:

class MyDataClass : public QObject
{
    Q_OBJECT
    ...

signals: 
    void dataChanged();
private:
    QMutex mutex;
    ....

and in the method accessing the data:

MyDataClass::accessFromAnyThread(QString newNode) {
    mutex.lock();
    ...
    //access critical data e.g.
    this->data.append(newNode);
    ...
    mutex.unlock();
    emit dataChanged();
}

If you are doing both reading and writing you should look into QSermaphore if you have performance issues.

Hope this gets you somewhere...

hg.
  • 324
  • 1
  • 13
  • in one function i am taking data and in the other one ie __Refresh__ function i am updating view using QStandardItem ino QTableView. my data is filled into a map which is defined static and after filling i want refrsh function to start.this is what i want but now as this Refresh function is in another class its working independantly and starting in between of feeling dat – Mcolorz Jul 12 '12 at 12:45
  • your example seems good and i guess something like this will work but in which class i have to declare mutex ,then how to access it and then what is thsi QString and all.please help – Mcolorz Jul 12 '12 at 12:46
  • The QString is just an example parameter. You could emit a signal after the mutex.unlock statement, and have it connected to your view. How about that? – hg. Jul 12 '12 at 12:53
  • ok i saw an example.but how can i use same mutex in both the class.i mean wont it give error on using variable of one class in another – Mcolorz Jul 12 '12 at 13:14
  • My answer is more generic than that. MyDataClass does not directly represent either of your classes. I believe you might have an architectural problem as well, but difficult to say without seeing the whole thing. The class (A) that manipulates the QTableView could have a method invoked from the thread acquiring data from client. Every time you access the QTableView you should lock the mutex and then unlock when you are done. In that way the timer/client will block while the other one is accessing the QTableView. Please show a little more of your code and how the objects interact. – hg. Jul 12 '12 at 13:40
  • You should not try to access the mutex from outside the class, but instead access the method using the mutex. This mutex belongs to the intance but is thread safe, which means that any number of threads can try to access the method without being within the, so called, critical section at once. – hg. Jul 12 '12 at 13:42
  • ya i have put mutex.lock() at the start of both Refresh and run and mutex.unlock() at the end of both.i have defined this QQMutex mutex inside the class that contains Refresh function. this is what i have done so far but still i guess both function are working simultaneously. i m getting no error in what i am doing but god knows why these two functions are still working together. – Mcolorz Jul 12 '12 at 14:44
  • Implement lock data and unlock data as public functions in data class..So that start the collection thread in your class where the timer is and you can use these functions to unlock/lock the shared data in timer class and always lock/unlock data in data class while reading..so that read in data class and update in timer class is synched. I thought this was basic OOPs concept.. – ScarCode Jul 12 '12 at 14:56
  • why i have to implement lock and unlock as public function,i mean they are member of QMutex class. @spyke – Mcolorz Jul 12 '12 at 15:22
  • @hg. no my QTableView is not acquiring data from the client with the help of run.it is taking data directly from a map map which is defined static in class A so that other class B can also access it and insert value in it from client. – Mcolorz Jul 12 '12 at 16:07
0

I am not completely sure what you are trying to do but i think you could use an QAbstractTableModel.

Fill the data you receive in an implementation of this model and add it to the table view via

yourtableview->setmodel(yourtablemodel)

Then you do not need a Refresh() function or something like that. The tableview will always show the content of the model.

TWE
  • 431
  • 2
  • 11