June 29, 2011

yannifan yannifan
Lab Rat
10 posts

Mixing pthreads and main GUI thread - Can pthread function emit a signal

 

Hello

My QT Qt desktop application is a Server which is listening to connections from clients.
The main GUI runs in the main thread. When a client starts, it is accepted on a new thread which is a pthread (say t1)

The need is to intimate the GUI thread of data received. I get a QPixMap error if i try to draw anything on the GUI using t1.
Hence I need to intimate the main thread through a slot and signal. The functionality after accepting the client is running in a new global function according to pthread design. Do I need to move this to a new class or is there any other way

Thanks in advance

10 replies

June 29, 2011

Lukas Geyer Lukas Geyer
Gene Splicer
2074 posts

Any GUI related operations can only be done in the GUI thread (with some minor exceptions).

The easiest solution might be migrating your pthreads code to QThreads, in which case you can use ordinary signals and slots to communicate among threads.

June 29, 2011

yannifan yannifan
Lab Rat
10 posts

Hi

I have made the code changes it looks like this :

  1. class SANServer : public QThread
  2. {
  3.  
  4.     Q_OBJECT
  5. .........
  6. signals:
  7.     void EmitServerConnected();
  8. };

In the constructor of the mainwindow,

  1.     connect(iSANServer, SIGNAL(EmitServerConnected()), this, SLOT(NotifyServerConnected()));

in the class of mainwindow

  1. private slots:
  2.     void NotifyServerConnected();

In the mainwindow.cpp file

  1. void MainWindow::NotifyServerConnected()
  2. {
  3. }

In one of the functions of the mainwindow

  1.     iSANServer = new SANServer(port_1, num_1);
  2.     iSANServer->start();

Constructor of iSANserver

  1. SANServer::SANServer(int aPort, int aMaxClients)//, ServerObserver &aObserver) : iObserver(aObserver)
  2. {
  3.     iPort = aPort;
  4.     iMaxClients = aMaxClients;

However Im getting the following errors :

1)

  1. /home/sudhix/QtProjects/SANServer-build-desktop/../SANServer/SANServer.cpp:3: error: undefined reference to `vtable for SANServer'

Im not sure why the vtable error is comming for the SANServer.

2)

  1. /home/sudhix/QtProjects/SANServer-build-desktop/../SANServer/SANServer.cpp:95: error: undefined reference to `SANServer::EmitServerConnected()'

I havent defined the function for emiting the signal. How may I do this?? or is there a standard signal i can use?

Thanks

June 29, 2011

Lukas Geyer Lukas Geyer
Gene Splicer
2074 posts

Re-run qmake, this will usually solve your problem (and most of the v-table like errors you may encounter).

However, the NotifyServerConnected signal slot should be public, not private.

June 29, 2011

Franzk Franzk
Lab Rat
830 posts

Lukas Geyer wrote:
However, the NotifyServerConnected signal should be public, not private.
NotifyServerConnected is a slot. It is irrelevant whether the access is private, protected or public, because the meta object can always invoke it. The signal cannot have access specifiers (it is always protected).

To emit signals, place

  1. emit EmitServerConnected();
somewhere in the code, where you think it makes sense to emit the signal.

Incidentally, I think EmitServerConnected() and NotifyServerConnected() are poorly chosen names. EmitServerConnected() should probably be called serverConnected() or connected(). NotifyServerConnected() should be named after what it does, not what you make it react to. It is in essence a normal function, so it is best to choose a name that fits with its behavior.

 Signature 

“Horse sense is the thing a horse has which keeps it from betting on people.”—W.C. Fields

http://www.catb.org/~esr/faqs/smart-questions.html

June 29, 2011

Lukas Geyer Lukas Geyer
Gene Splicer
2074 posts
Franzk wrote:
It is irrelevant whether the access is private, protected or public, because the meta object can always invoke it. The signal cannot have access specifiers (it is always protected).

That’s true. But this is a question of interface design. Public slots can and should be invoked from other classes, whereas private slots should not – even if the system supports it lacks support for encapsulation.

Franzk wrote:
NotifyServerConnected is a slot.
I meant slot, didn’t I write it? ;-)

June 29, 2011

yannifan yannifan
Lab Rat
10 posts

Hello All

Thanks for your help. The code has started working :)

Changes made

1)

  1. public:
  2. void run(void);
  3.  
  4. changed to
  5.  
  6. protected:
  7. virtual run(void);

2)
Slots are made public

3) Most importantly, deleted all object files, did a clean, restarted QTcreator and it compiled.
Maybe a combination of all these helped it to work :)

June 29, 2011

kkrzewniak kkrzewniak
Lab Rat
218 posts
yannifan wrote:
1)
  1. /home/sudhix/QtProjects/SANServer-build-desktop/../SANServer/SANServer.cpp:3: error: undefined reference to `vtable for SANServer'
Im not sure why the vtable error is comming for the SANServer.

Running qmake would solve this, it usually occurs because of moc skipping the class.

 Signature 

Me, Grimlock, not “nice dino”. ME BASH BRAINS!

June 29, 2011

kkrzewniak kkrzewniak
Lab Rat
218 posts
Lukas Geyer wrote:
However, the NotifyServerConnected signal slot should be public, not private.

You can connect signals to private slots.

 Signature 

Me, Grimlock, not “nice dino”. ME BASH BRAINS!

June 29, 2011

Lukas Geyer Lukas Geyer
Gene Splicer
2074 posts
kkrzewniak wrote:
You can connect signals to private slots.
Lukas Geyer wrote:
That’s true. But this is a question of interface design. Public slots can and should be invoked from other classes, whereas private slots should not – even if the system supports it lacks support for encapsulation.

June 29, 2011

kkrzewniak kkrzewniak
Lab Rat
218 posts

Yes I’m aware of your view that one should not connect foreign signals to private slots, but you have not mentioned it in your original post concerning making the slot public. This may lead to a false assumptions that such connections are impossible.

 Signature 

Me, Grimlock, not “nice dino”. ME BASH BRAINS!

 
  ‹‹ Transfering Qt from one machine to other      QFileSystemModel filter problem!! ››

You must log in to post a reply. Not a member yet? Register here!