Results 1 to 16 of 16

Thread: QMetaObject use in visual studio (or "asyncronous QSocketNotifier use")

Threaded View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Join Date
    May 2014
    Posts
    16
    Qt products
    Qt4
    Platforms
    Windows

    Default QMetaObject use in visual studio (or "asyncronous QSocketNotifier use")

    I am using QtCreator 3.1.1, Qt library 4.8.5, and visual Studio 2013 Express.

    For the last several hours, Qt has been kicking my butt. I am trying to perform asyncronous socket communication with a microcontroller, but QCoreApplication::exec() is blocking.

    I could do something dirty like the following, but I'd rather learn to do it right:
    Qt Code:
    1. #include <QtCore/QCoreApplication>
    2. #include "my_socket_handler.h"
    3.  
    4. int main(int argc, char **argv)
    5. {
    6. QCoreApplication app(argc, argv);
    7.  
    8. while(1)
    9. {
    10. check_for_and_receive_incoming_data_over_sockets();
    11. app.processEvents();
    12. }
    13.  
    14.  
    15. return 0;
    16. }
    To copy to clipboard, switch view to plain text mode 


    But that smells like foul code. I'd rather do it right with QSocketNotifier (or some other idea if you have one). Since I'm using visual studio, I'll use winsock. I got the following winsock code to work with QSocketNotifier in QtCreator:


    Qt Code:
    1. #include <QCoreApplication>
    2. #include <QSocketNotifier>
    3. #include <QtDebug>
    4.  
    5. #include <WinSock2.h>
    6. #include <WS2tcpip.h>
    7. #pragma comment (lib, "Ws2_32.lib")
    8.  
    9. #include <iostream>
    10. using std::cout;
    11. using std::endl;
    12.  
    13. #define DEFAULT_PORT "5"
    14. #define DEFAULT_HOST_ADDR "10.10.10.126"
    15. #define DEFAULT_BUF_LEN 512
    16.  
    17.  
    18. class RawSocketReader : public QObject
    19. {
    20. Q_OBJECT
    21.  
    22. public slots:
    23. void doSomething(int socket)
    24. {
    25. char buff[DEFAULT_BUF_LEN];
    26. int length;
    27. while ((length = recv(socket, buff, DEFAULT_BUF_LEN, 0)) >= 0) {
    28. qDebug() << "Received" << length << "bytes";
    29. }
    30. }
    31. };
    32.  
    33. // ??also, what the heck does this do and where does it live??
    34. #include "main.moc"
    35.  
    36. int main(int argc, char *argv[]) {
    37. int app_ret_val = 0;
    38. QCoreApplication app(argc, argv);
    39.  
    40. WSADATA wsa_data;
    41. struct addrinfo *result_ptr = 0;
    42. struct addrinfo hints;
    43. SOCKET my_socket = INVALID_SOCKET;
    44.  
    45. int rx_byte_count = 0;
    46. char rx_buf[DEFAULT_BUF_LEN];
    47.  
    48. // initialize WSA stuff
    49. WSAStartup(MAKEWORD(2, 2), &wsa_data);
    50.  
    51. // set up the "hints" struct that will guide the search for
    52. // the host
    53. ZeroMemory(&hints, sizeof(hints));
    54. hints.ai_family = AF_UNSPEC;
    55. hints.ai_socktype = SOCK_STREAM;
    56. hints.ai_protocol = IPPROTO_TCP;
    57.  
    58. getaddrinfo("10.10.10.126", "5", &hints, &result_ptr);
    59. my_socket = socket(
    60. result_ptr->ai_family,
    61. result_ptr->ai_socktype,
    62. result_ptr->ai_protocol);
    63.  
    64. connect(
    65. my_socket,
    66. result_ptr->ai_addr,
    67. (int)(result_ptr->ai_addrlen));
    68.  
    69.  
    70. QSocketNotifier notifier(my_socket, QSocketNotifier::Read);
    71. RawSocketReader reader;
    72. QObject::connect(
    73. & notifier,
    74. SIGNAL(activated(int)),
    75. &reader,
    76. SLOT(doSomething(int)));
    77.  
    78. app_ret_val = app.exec();
    79.  
    80. closesocket(my_socket);
    81. my_socket = INVALID_SOCKET;
    82. freeaddrinfo(result_ptr);
    83. WSACleanup();
    84.  
    85.  
    86. return app_ret_val;
    87. }
    To copy to clipboard, switch view to plain text mode 


    But I can't get that "QObject::connect(...)" call to work in Visual Studio. The problem seems to stem from the SIGNAL and SLOT calls, which QtCreator can process, but Visual Studio can't (or at least, I haven't figured it out yet). They are supposed to supply QMetaMethod references to the "QObject::connect(...)" function, but they aren't. According to the compiler error, they are providing "const char *" arguments (the "SIGNAL" and "SLOT" #defines seem to merely return pointers to the method).

    I think my solution lies in QMetaMethod, but I am having difficulty piecing together documentation from different Qt things just to figure out how to construct the thing, and I haven't yet figured out what .lib or .dll it lives in. Just about all the documentation about meta stuff and SIGNAL and SLOT seems to be written in the assumption that QtCreator is being used.


    So I'm stuck. Help? I want to get asynchronous socket communication up and running within a Qt application.
    Last edited by amdreallyfast; 24th May 2014 at 21:36.

Similar Threads

  1. Replies: 1
    Last Post: 15th September 2013, 08:49
  2. error "cmd.exe" exited with code 1 in visual studio 2010
    By cactus0830 in forum Installation and Deployment
    Replies: 1
    Last Post: 22nd June 2013, 04:25
  3. Replies: 1
    Last Post: 14th May 2011, 08:02
  4. Replies: 1
    Last Post: 10th March 2011, 14:40
  5. Qmake DLLDESTDIR, Visual Studio "clean solution"
    By SiLiZiUMM in forum Installation and Deployment
    Replies: 4
    Last Post: 18th March 2008, 12:14

Tags for this Thread

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
  •  
Qt is a trademark of The Qt Company.