PDA

View Full Version : declare an array of QSemaphore and array of slot functions



radeberger
27th April 2010, 15:27
Hi everybody!

how can I declare an array of QSemaphore and one array of slot functions?

The point is, that depending on configuration parameters, one thread need to pass other threads one or more semaphores, and we need to use one or more slots for some signals. I wanted to do it with arrays, and pass the other threads only a pointer to the first semaphore and a pointer to the first slot function but as of now I didn't managed.

Has anyone any ideas?

Thanks in advance

borisbn
27th April 2010, 16:42
array of QSemaphore


QSemaphore semArr[ N ];
//or
QSemaphore * semArr = new QSemaphore[ N ];
//or (I think the best)
std::vector< QSemaphore > semArr;
semArr.resize( N );

...
// passing this array to another function:
foo( &semArr[ 0 ], N );

...
void foo( QSemaphore * semArr, int N )
{
...
}


slots are just a const pointer to char. That's why an array of slots is an array of const char *:


typedef const char * ConstCharPtr;
ConstCharPtr slotsArr[ N ];

radeberger
28th April 2010, 12:42
thanks borisbn!

I'll try it and I'll give feedback!

thank you very much!!!

squidge
28th April 2010, 12:50
Remember to store the result of the SLOT macro and not just the function name, else you might get strange results.

(Prefixing the function name with '1' will probably work, but it's always handy to use the proper macro in case this changes or you wish to have the debug info)

radeberger
30th April 2010, 07:28
Hi everybody,

I've tried some of your proposals and it was fine, I used vector of semaphores and slot functions, but now I'm think to try something else. How can I differentiate between three newConnection signals from three QTcpServer? I mean If I can do this, then I could have only one slot function for the three connections and give it as a input param this identificator, so do you know how can I do this? can I give or get params from newConnection() signal?

Thank you very much!

squidge
30th April 2010, 07:32
You want to connect multiple signals to a single slot? If so, use QSignalMapper

tbscope
30th April 2010, 07:35
Create a simple subclass of the QTcpServer class so you have a custom ID property.
Then use the sender() function in your slot: http://doc.qt.nokia.com/4.6/qstatemachine-signalevent.html#sender
Use the sender object to retrieve the ID and act accordingly.

Or if you just need the adres of the QTcpServer, you do not need to subclass, just use the sender() object.

tbscope
30th April 2010, 07:38
Even another way is to handle the new connections in QTcpServer itself.
Create a subclass where you define a slot for new connections and in the constructor add a connection from the newconnection signal to you custom slot inside your subclass.

radeberger
1st May 2010, 11:52
Hi guys!

thank you very much for the proposals. I think It's enough with the function sender(). I will try it this way.

Thanks!

radeberger
1st May 2010, 17:00
Hi!

I tried this with the sender() function but I had some trouble.

in the slot function I have:

myclass *tcpServerSender = qobject_cast<myclass*>(QObject::sender());

but this pointer is always NULL, do you have any ideas, how can I get the pointer to the object?

thanks!

squidge
1st May 2010, 19:34
sender() should only be used as a last resort. Instead, use QSignalMapper. You can then add your senders to the signal mapper when you create them. The signal mapper will then call your slot with an integer id or string (your choice) to identify the sender.

radeberger
2nd May 2010, 13:24
thanks fatjuicymole!

you are right, with QSignalMapper runs fine!