Results 1 to 12 of 12

Thread: declare an array of QSemaphore and array of slot functions

  1. #1
    Join Date
    Feb 2010
    Posts
    28
    Qt products
    Qt4
    Platforms
    Windows

    Default declare an array of QSemaphore and array of slot functions

    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

  2. #2
    Join Date
    Apr 2010
    Location
    Rostov-na-Donu, Russia
    Posts
    153
    Thanks
    2
    Thanked 26 Times in 23 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: declare an array of QSemaphore and array of slot functions

    array of QSemaphore
    Qt Code:
    1. QSemaphore semArr[ N ];
    2. //or
    3. QSemaphore * semArr = new QSemaphore[ N ];
    4. //or (I think the best)
    5. std::vector< QSemaphore > semArr;
    6. semArr.resize( N );
    7.  
    8. ...
    9. // passing this array to another function:
    10. foo( &semArr[ 0 ], N );
    11.  
    12. ...
    13. void foo( QSemaphore * semArr, int N )
    14. {
    15. ...
    16. }
    To copy to clipboard, switch view to plain text mode 

    slots are just a const pointer to char. That's why an array of slots is an array of const char *:
    Qt Code:
    1. typedef const char * ConstCharPtr;
    2. ConstCharPtr slotsArr[ N ];
    To copy to clipboard, switch view to plain text mode 

  3. #3
    Join Date
    Feb 2010
    Posts
    28
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: declare an array of QSemaphore and array of slot functions

    thanks borisbn!

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

    thank you very much!!!

  4. #4
    Join Date
    Sep 2009
    Location
    UK
    Posts
    2,447
    Thanks
    6
    Thanked 348 Times in 333 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: declare an array of QSemaphore and array of slot functions

    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)

  5. #5
    Join Date
    Feb 2010
    Posts
    28
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: declare an array of QSemaphore and array of slot functions

    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!

  6. #6
    Join Date
    Sep 2009
    Location
    UK
    Posts
    2,447
    Thanks
    6
    Thanked 348 Times in 333 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: declare an array of QSemaphore and array of slot functions

    You want to connect multiple signals to a single slot? If so, use QSignalMapper

  7. #7
    Join Date
    Jan 2006
    Location
    Belgium
    Posts
    1,938
    Thanked 268 Times in 268 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows
    Wiki edits
    20

    Default Re: declare an array of QSemaphore and array of slot functions

    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/qstatema...nt.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.

  8. #8
    Join Date
    Jan 2006
    Location
    Belgium
    Posts
    1,938
    Thanked 268 Times in 268 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows
    Wiki edits
    20

    Default Re: declare an array of QSemaphore and array of slot functions

    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.

  9. #9
    Join Date
    Feb 2010
    Posts
    28
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: declare an array of QSemaphore and array of slot functions

    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!

  10. #10
    Join Date
    Feb 2010
    Posts
    28
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: declare an array of QSemaphore and array of slot functions

    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!

  11. #11
    Join Date
    Sep 2009
    Location
    UK
    Posts
    2,447
    Thanks
    6
    Thanked 348 Times in 333 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: declare an array of QSemaphore and array of slot functions

    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.

  12. #12
    Join Date
    Feb 2010
    Posts
    28
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: declare an array of QSemaphore and array of slot functions

    thanks fatjuicymole!

    you are right, with QSignalMapper runs fine!

Similar Threads

  1. 2D Array
    By ctote in forum Qt Programming
    Replies: 5
    Last Post: 27th January 2010, 10:57
  2. Min and Max value in array
    By Benjamin in forum Qt Programming
    Replies: 13
    Last Post: 27th February 2009, 14:19
  3. Why do i need to declare a function as a slot?
    By cbarmpar in forum Qt Programming
    Replies: 2
    Last Post: 31st August 2008, 20:38
  4. array of pointers to functions
    By moowy in forum General Programming
    Replies: 3
    Last Post: 19th October 2006, 10:48
  5. declare array in a class
    By mickey in forum General Programming
    Replies: 5
    Last Post: 17th April 2006, 20:25

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.