Results 1 to 16 of 16

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

  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.

  2. #2
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,729
    Thanks
    13
    Thanked 1,610 Times in 1,537 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

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

    Are you specifically trying to use the low level code for a reason?
    AFAICT QTcpSocket will do what you are after without using low level Windows sockets code. When data arrives it will emit readyRead() and that can trigger doSomething(). There is a couple of examples of QTcpSocket use in the docs.

  3. #3
    Join Date
    May 2014
    Posts
    16
    Qt products
    Qt4
    Platforms
    Windows

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

    I tried to use QTcpSocket, and I managed to get it working with Visual Studio (http://www.bogotobogo.com/Qt/Qt5_QTcpSocket.php), but there was not mention of "readyRead()".

    I saw "readyRead()" in the docs (http://qt-project.org/doc/qt-5/qiodevice.html#readyRead), but I got the impression that I would have to continuously check it in a loop that wasn't blocked by QApplication::exec().

    If you know how to use QTcpSocket asynchronously with QApplication, I'm all ears. I haven't been able to find a single example of such yet.

    Also, do you have a link to these docs that you're talking about? The ones with examples? This one (http://qt-project.org/doc/qt-5/QTcpSocket.html) doesn't have any examples. It is just the tip of a very large documentation iceberg.
    Last edited by amdreallyfast; 24th May 2014 at 22:21. Reason: stupid "reply" feature got crossed with "edit" and deleted my previous message

  4. #4
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

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

    The document you link to has a number of links to examples, one of them being http://qt-project.org/doc/qt-5/qtnet...t-example.html

    You don't call readyRead(), it is a signal just like QSocketNotifier::activated(), so it is connected to a slot that then handles the incoming data.
    QTcpSocket just makes the whole setup way easier.

    SIGNAL and SLOT are just C-preprocessor macros, the compiler used by QtCreator and the one used by Visual Studio are both equally capable of handling those.

    Sure, QtCreator can do some introspection for code completion during editing, but the connection will work regardless of the editor that was used to write it.

    Quote Originally Posted by amdreallyfast View Post
    Also, for the record, I did find a tutorial on QTcpSocket and managed to get it to work in Visual Studio (http://www.bogotobogo.com/Qt/Qt5_QTcpSocket.php), but there was not mention of "readyRead()".
    This is because the example uses blocking I/O, so it calls waitForReadyRead() instead of connecting to readyRead()

    Cheers,
    _

  5. #5
    Join Date
    May 2014
    Posts
    16
    Qt products
    Qt4
    Platforms
    Windows

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

    ...it is a signal just like QSocketNotifier::activated(), so it is connected to a slot...
    I'm not following. What is a "signal"? And what is a "slot"? They are preprocessor macros for "qFlagLocation("1"#a QLOCATION)" and "qFlagLocation("2"#a QLOCATION)", respectively.

    This is because the example uses blocking I/O, so it calls waitForReadyRead instead of connecting to readRead()
    What do you mean, "connecting to"? How would you "connect" to a preprocessor macro? What does that even mean?


    Added after 42 minutes:


    Also, I've been looking at the example you linked to, trying to tear it apart and get it working. I'm running into an error in my "connect(...)" call again, in which it cannot find the function specified in the fourth argument (the one with SLOT(...) surrounding it). This may be a compiler-specific constructor order error.

    This is my basic code:
    Qt Code:
    1. #include <QtCore\QObject>
    2. #include <QtNetwork\QTcpSocket>
    3. #include <QtCore\QDebug>
    4.  
    5. class my_TCP_socket : public QObject
    6. {
    7. public:
    8. explicit my_TCP_socket(QObject *parent = 0);
    9.  
    10. ~my_TCP_socket();
    11.  
    12. private:
    13. QTcpSocket *m_socket_ptr;
    14.  
    15. void read_it();
    16. };
    17.  
    18.  
    19.  
    20. my_TCP_socket::my_TCP_socket(QObject *parent) :
    21. QObject(parent)
    22. {
    23. m_socket_ptr = new QTcpSocket(this);
    24.  
    25. m_socket_ptr->connectToHost("10.10.10.126", 5);
    26. if (!(m_socket_ptr->waitForConnected(5000)))
    27. {
    28. qDebug() << "Not connected!";
    29. m_socket_ptr = NULL;
    30. }
    31. else
    32. {
    33. qDebug() << "Connected!";
    34. }
    35.  
    36. this->connect(m_socket_ptr, SIGNAL(readyRead()), this, SLOT(this->read_it(void)));
    37. }
    38.  
    39. void my_TCP_socket::read_it()
    40. {
    41. QDataStream in(m_socket_ptr);
    42.  
    43. qint64 bytes_available = m_socket_ptr->bytesAvailable();
    44. if (bytes_available > 0)
    45. {
    46. QString in_data;
    47. in >> in_data;
    48.  
    49. qDebug() << in_data;
    50. }
    51. else
    52. {
    53. qDebug() << "no data";
    54. }
    55. }
    To copy to clipboard, switch view to plain text mode 

    Those functions may not exist yet when "connect(...)" is called in the constructor, although I may just be doing it wrong because this

    Qt Code:
    1. int method_index = this->metaObject()->indexOfMethod("init()");
    To copy to clipboard, switch view to plain text mode 

    just returns -1, and that's after initialization.

    Again, any feedback would be helpful.


    Added after 20 minutes:


    Followup: Should have noted that the attempt connect the "read_it()" function resulted in this error at runtime:
    Qt Code:
    1. Object::connect: No such slot QObject::this->read_it(void) in *path redacted*
    To copy to clipboard, switch view to plain text mode 

    I have tentatively discovered that the Q_OBJECT macro needs to be declared at the top of the "private" section of the class, but that is producing a linker error. I put that into a new thread. The question seemed different enough from this thread.

    Please advise still. The examples are not working outside QtCreator.
    Last edited by amdreallyfast; 24th May 2014 at 23:29.

  6. #6
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,729
    Thanks
    13
    Thanked 1,610 Times in 1,537 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

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

    I'm not following. What is a "signal"? And what is a "slot"?
    The concepts are documented: Signals & Slots. Assistant, which ships with Qt, is your friend. Searching the index for "examples" is also instructive.

    The general principals of Network Programming specifically Using TCP with QTcpSocket and QTcpServer. Fortune Client Example is a receiver for a binary payload but it demonstrates the asynchronous use of QTcpSocket.

  7. #7
    Join Date
    May 2014
    Posts
    16
    Qt products
    Qt4
    Platforms
    Windows

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

    I don't seem to be getting across that I am not using QtCreator. The explanations and examples in QtAssistant and every piece of documentation that I have found so far assume that I am using the QtCreator IDE. I am not.

    I already got the code working in QtCreator. I didn't want to use that because the rest of my program was build in Visual Studio. The documentation that I have seen for signals and slots completely falls apart when you try to run it in Visual Studio because the Meta-Object Compiler is not a built-in thing.

    Do you know how I could get MOC working in Visual Studio? I have found no such thing yet. This documentation (http://qt.developpez.com/doc/4.7/moc/), which is about using MOC, again assume QtCreator.
    Last edited by amdreallyfast; 25th May 2014 at 02:15. Reason: spelling corrections

  8. #8
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,729
    Thanks
    13
    Thanked 1,610 Times in 1,537 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

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

    I don't seem to be getting across that I am not using QtCreator.
    We do not seem to be communicating that C++ is C++ (and Qt idiom is Qt idiom) regardless of which editor you use to write it. Visual Studio and Qt Creator are glorified editors that can run a build process on the source they edit.
    The explanations and examples in QtAssistant and every piece of documentation that I have found so far assume that I am using the QtCreator IDE. I am not.
    The examples in Qt Assistant, which is not part of Qt Creator, are IDE agnostic (unless you happen to be reading the manual for the Qt Creator IDE itself of course). The documents I specifically linked you to make absolutely no mention of Qt Creator and do not rely on it in any way. You can equally well use Notepad and the command line.


    If you are using VS Express then, no, there is no point and click method of getting moc support (It must be possible to make it work but you need to understand how applications are built). If you are using a full VS version then the Qt VS Add-in should look after this for you. I do not use VS with Qt so I cannot help with the specifics.
    What is the Qt Visual Studio add-in?
    Qt Downloads, Click Show Downloads button, and scroll to "Other Downloads" group.
    Last edited by ChrisW67; 25th May 2014 at 03:37. Reason: Added VS info

  9. #9
    Join Date
    May 2014
    Posts
    16
    Qt products
    Qt4
    Platforms
    Windows

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

    ...C++ is C++ (and Qt idiom is Qt idiom) regardless of which editor you use to write it.
    Alright. I never picked that up. Everything that I've been taught and everything that I have learned through school in computer science tells me that it's all implementation dependent. If everyone follows the standard, then I'm ok and I can use it, but if anything fails, then nearly any and every explanation is an option, and that leaves me overwhelmed. I am intentionally asking absurd questions to try to figure out what is happening, and I'm sorry if that is irritating, but I don't know any other way to ping other people for little-used information.

    In this case, the problem is in hunting down the build commands to moc.exe (http://qt-project.org/doc/qt-4.8/moc...d-line-options), and figuring out how to include them in the right order to get the rest of the compilation steps to jive.

    Thanks for the feedback so far.

    I'll post my findings when I solve them. It may have been easier to just learn Win32 threading.

  10. #10
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,729
    Thanks
    13
    Thanked 1,610 Times in 1,537 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

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

    Can I suggest you start by ignoring Visual Studio, open a Qt command prompt (usually a shortcut for this in the Start menu), establish your Microsoft command line environment, something like:
    Qt Code:
    1. C:\Program Files\Microsoft SDKs\Windows\v7.1\bin\SetEnv.cmd /x86
    To copy to clipboard, switch view to plain text mode 
    and follow:
    Getting Started Programming with Qt
    Where you see "make" as a command run "nmake" (the Microsoft make utility)

    By the time you get down to "Using a QMainWindow" you will have a PRO file that generates a Makefile containing moc invocations. You will see these in the output when you run nmake.

  11. #11
    Join Date
    May 2014
    Posts
    16
    Qt products
    Qt4
    Platforms
    Windows

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

    Issue is resolved. I explained it to myself in the reply below. Maybe it will help someone else too.


    Thanks for the feedback. I still had to figure it out for myself, but a few of the supplied links provided new ideas when I looked at them the second (or third) time.

    I am running Visual Studio 2013 Express, so to get MOC to work, I had to do manually adjust the command line that built the VS project. In case any other confused person discovers this, I'll just post all my demo code and give myself a thorough explanation.

    Misc Note: The following two additional include directories were specified in project properties->Configuration Properties->C/C++->Addition Include Directories:
    Qt Code:
    1. C:\Qt\4.8.5\include
    2. $(ProjectDir)
    To copy to clipboard, switch view to plain text mode 

    Explanation:
    In order to use SLOT or SIGNAL in Visual Studio without the Qt plugin, such as if you are using VS Express as I am, you need to invoke the MOC system yourself. Examine the header file below for details on how SLOT is used, but SIGNAL, Q_INVOKABLE, and other meta-system tags are used in the same way.

    The MOC system will generate .cpp files (they appear to be C-based files, so they may actually be C files that can work without a fuss in a C++ compiler (??is this correct??)), not binary files, so they cannot be linked in later and must be run through the compiler from the beginning. Therefore, the files in need of the meta system tags need to be run through MOC before the rest of the compilation begins.

    (1) Right-click your project (NOT the solution) that contains the file that needs the MOC tags (in my case, the project that contained my_TCP_socket.h, shown below), select properties.

    (2) Configuration Properties->Custom Build Step->General

    (3) Leave "Execute After" blank.

    (4) Under "Execute Before", select "PreBuildEvent" from the drop down menu. This tells Visual Studio to attempt to execute the text in "Command Line" before the pre-build event, which is the earliest build phase in the Visual Studio program builder (http://msdn.microsoft.com/en-us/library/e85wte0k.aspx).

    (5) In "Command Line", follow this outline: *location of moc executable* *path of file to run through MOC* -o *path of where you want the output file to go (hence the "-o" option)
    In my case, this was as follows:
    Qt Code:
    1. C:\Qt\4.8.5\bin\moc.exe $(ProjectDir)\my_TCP_socket.h -o $(ProjectDir)\tmp\moc_my_TCP_socket.cpp
    To copy to clipboard, switch view to plain text mode 


    Note: It is necessary to include the MOC-generated .cpp files from a source file. It is generally shenaniganry to #include a source file (.c or .cpp) at all because it will cause "X is already defined" errors if it happens to get included more than once. It is perfectly fine to declare things more than once, but it is not ok to define them more than once. Class method definitions in a header file can get away with it because they are special. Each compilation unit (that is, source file) is compiled once on its own into an object file, along with all the stuff that was dumped into it via #includes, and then all the resulting object files are linked together. If there is a function definition that appears more than once during this linking stage, such as non-class functions generated by MOC that were included in more than one compilation unit, then there will be "X is already defined" errors during linking.

    Here is my custom TCP socket class declared in "my_TCP_socket.h":
    Qt Code:
    1. #pragma once
    2.  
    3. #include <QtCore\QObject>
    4. #include <QtNetwork\QTcpSocket>
    5. #include <QtCore\QDebug>
    6.  
    7. // if this header file gets included by more than one source file, then there will be an "X is already defined" error, so it is a very bad plan to include the MOC-generated code here
    8. // Note: The commented out #include was kept in expressly for this comment.
    9. //#include "tmp\moc_my_TCP_socket.cpp"
    10.  
    11. class my_TCP_socket : public QObject
    12. {
    13. public:
    14. explicit my_TCP_socket(QObject *parent = 0);
    15.  
    16. ~my_TCP_socket();
    17.  
    18. private:
    19. // http://qt-project.org/doc/qt-4.8/qobject.htm#Q_OBJECT
    20. // "The Q_OBJECT macro must appear in the private section of a class definition
    21. // that declares its own signals and slots or that uses other services provided
    22. // by Qt's meta-object system."
    23. Q_OBJECT
    24.  
    25. QTcpSocket *m_socket_ptr;
    26.  
    27. // http://qt-project.org/doc/qt-4.8/qobject.html#Q_INVOKABLE
    28. // "Apply this macro to definitions of member functions to allow them to be
    29. // invoked via the meta-object system. The macro is written before the return
    30. // type..."
    31. // Note: This applies to Q_SLOT and Q_SIGNAL as well, even though the
    32. // documentation at the link does not expressly say so (as far as I saw).
    33. Q_SLOT void read_it();
    34. };
    To copy to clipboard, switch view to plain text mode 

    Here is my_TCP_socket.cpp:
    Qt Code:
    1. #include "my_TCP_socket.h"
    2.  
    3. #include <iostream>
    4. using std::cout;
    5. using std::endl;
    6.  
    7. #include <QtCore\QMetaMethod>
    8.  
    9. #include "tmp\moc_my_TCP_socket.cpp"
    10.  
    11. my_TCP_socket::my_TCP_socket(QObject *parent) :
    12. QObject(parent)
    13. {
    14. m_socket_ptr = new QTcpSocket(this);
    15.  
    16. m_socket_ptr->connectToHost("10.10.10.126", 5);
    17. if (!(m_socket_ptr->waitForConnected(5000)))
    18. {
    19. qDebug() << "Not connected!";
    20. m_socket_ptr = NULL;
    21. }
    22. else
    23. {
    24. qDebug() << "Connected!";
    25. }
    26.  
    27. // just showing that the file exists
    28. int method_index = this->metaObject()->indexOfMethod("read_it()");
    29. cout << "index of method read_it() is '" << method_index << "'" << endl;
    30. QObject::connect(m_socket_ptr, SIGNAL(readyRead()), this, SLOT(read_it()));
    31. }
    32.  
    33. my_TCP_socket::~my_TCP_socket()
    34. {
    35. if (NULL != m_socket_ptr)
    36. {
    37. m_socket_ptr->close();
    38. }
    39. }
    40.  
    41. void my_TCP_socket::read_it()
    42. {
    43. //QDataStream in(m_socket_ptr);
    44.  
    45. if (m_socket_ptr->waitForReadyRead(3000))
    46. {
    47. qint64 bytes_available = m_socket_ptr->bytesAvailable();
    48. if (bytes_available > 0)
    49. {
    50. qDebug() << "Reading '" << bytes_available << "' bytes";
    51. qDebug() << m_socket_ptr->readAll();
    52. //QString in_data;
    53. //in >> in_data;
    54.  
    55. //qDebug() << in_data;
    56. }
    57. else
    58. {
    59. qDebug() << "no data";
    60. }
    61. }
    62. }
    To copy to clipboard, switch view to plain text mode 

    Lastly, here is main.cpp:
    Qt Code:
    1. #include "my_TCP_socket.h"
    2.  
    3. #include <QtCore/QCoreApplication>
    4. #pragma comment (lib, "C:/Qt/4.8.5/lib/QtCored4.lib")
    5. #pragma comment (lib, "C:/Qt/4.8.5/lib/QtNetworkd4.lib")
    6.  
    7.  
    8. int main(int argc, char **argv)
    9. {
    10. int app_ret_val = 0;
    11. QCoreApplication app(argc, argv);
    12. my_TCP_socket S;
    13.  
    14. app_ret_val = app.exec();
    15.  
    16. return app_ret_val;
    17. }
    To copy to clipboard, switch view to plain text mode 
    Last edited by amdreallyfast; 25th May 2014 at 06:09. Reason: updated contents; explicitly declared as "resolved"

  12. #12
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

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

    There is really no point in calling waitForReadyRead() in the slot connected to the socket's readyRead() signal.

    In the worst case this makes your program wait again instead of reading the data that is already there.

    Cheers,
    _

  13. #13
    Join Date
    May 2014
    Posts
    16
    Qt products
    Qt4
    Platforms
    Windows

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

    There is really no point in calling waitForReadyRead() in the slot connected to the socket's readyRead() signal.
    O rly? I was wondering about that. I had an issue in which two transmissions of data would somehow, in the same time frame, be read instead of one. Only 25 bytes is coming from my microcontroller, but when I put in the waitForReadyRead(...), it read 50 bytes.

    I should note that the waitForReadyRead(...) call was necessary to retrieve data at all when I ran the program in Visual Studio recently. I must have inadvertently "crossed some wires", so to speak. I'll try to do without, although I'd like to know how that function works and what it is linked such that it read twice.

  14. #14
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

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

    Well, "wait for read read" does what its name suggests

    It waits until readyRead() is emitted.

    In your case you are getting the slot called on a readyRead() emission and then wait for a second one.

    Cheers,
    _

  15. #15
    Join Date
    May 2014
    Posts
    16
    Qt products
    Qt4
    Platforms
    Windows

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

    I suspected that might be the case. Thanks for the confirmation.

  16. #16
    Join Date
    May 2014
    Posts
    16
    Qt products
    Qt4
    Platforms
    Windows

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

    I made a mistake when describing the setup for moc. As it is, it will not run in a fresh project.

    I had to manually create the "/tmp/" folder that I described in the command line for the custom build step because moc.exe does not seem to create folders that do not exist in the specified path (I had to run moc.exe manually to figure that one out). After I manually create the folder, I had to run moc.exe manually on each file that contained a meta-object keyword, such as Q_OBJECT, SLOT, and SIGNAL, to create the moc-generated source file.

    THEN the Visual Studio build would run as described. This isn't as pretty and easy as doing it in QtCreator. I have to manually help it along with the initial setup, and then it works.

    EDIT: I know that my custom build step is running because I can see the created/modified time in Windows Explorer, but it is apparently not finishing before the preprocessor comes along. This may be a symptom of the same problem that requires me to run moc.exe manually before I can build my Visual Studio project. Apparently, something is happening in the preprocessor before the PreBuildEvent, even before moc.exe finishes. Or maybe Visual Studio launches moc.exe in its own thread, then continues on in the build step before moc.exe finishes. I don't know. Whatever is going on, if I add a function labeled with Q_SIGNAL, then it must not have a definition because the moc-generated source file will define the function labeled as a signal. The linker will flip out at this unless it is already present in the moc-generated source file, so I had to run moc.exe manually once I added this function before the build would complete.
    Last edited by amdreallyfast; 26th May 2014 at 20:42. Reason: updated contents

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
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.