Problem with TextEdit
-
I have created a software to receive packets from another pc, the software is to read the raw data of the packet, it works, it can receive packets. However, some of the text are not shown, as in, when then text is shown, a while later the text will disappear and to reveal the text, I have to highlight that area, I am trying to append the raw data received as string, is there any other widgets that are suitable for appending or shown huge number of string other than TextEdit, because I wish to see all the data instead of the white spaces and I have to highlight the white spaces to see the oncoming packets.
P.S: I am using the append function to append all the raw data on to the TextEdit, is it the problem lies with me using the wrong function? So should I use append?
-
Could you post some code snippet to highlight ( :D ) your problem's roots?
Maybe you coud use QTextView if you do not want to edit your data, and for this aplication QPlainTextEdit is also good, but the latter is almost the same as QTextEdit, it's just for plain text.
-
Just from the description, it sounds like a bussy loop receiving the updates, and then not letting the eventloop do it's job of making sure the widget gets repainted... However, I might be completely off, not having seen a single line of relevant code.
-
Ok I will post the codes here but it is a bit wordy, and i have tried using plain text edit, it didnt even display the packets, unlike the textedit, but due to some lines of data suddenly disappear or didnt even show, I have to show them by selecting the empty areas
-
@while (1) {
// Call recvfrom() to get it then display the received data...
ByteReceived = recvfrom(ReceivingSocket, ReceiveBuf, BufLength,
0, (SOCKADDR *)&SenderAddr, &SenderAddrSize);if (ByteReceived > 0)
m_pReceiver->PrintInfo("\n\nServer: Total Bytes received: "
+ QString(ByteReceived)
+ "\nServer: The data is: "
+ QString(ReceiveBuf));
else if (ByteReceived <= 0)
m_pReceiver->PrintInfo("Server: Connection closed with error code: "
+ WSAGetLastError());
else
m_pReceiver->PrintInfo("Server: recvfrom() failed with error code: "
+ WSAGetLastError());
@ -
[quote author="kay2e4" date="1340719017"]
@
while (1){
// Call recvfrom() to get it then display the received data...
ByteReceived = recvfrom(ReceivingSocket, ReceiveBuf, BufLength,
0, (SOCKADDR *)&SenderAddr, &SenderAddrSize);if (ByteReceived > 0)
m_pReceiver->PrintInfo("\n\nServer: Total Bytes received: "
+ QString(ByteReceived)
+ "\nServer: The data is: "
+ QString(ReceiveBuf));
else if (ByteReceived <= 0)
m_pReceiver->PrintInfo("Server: Connection closed with error code: "
+ WSAGetLastError());
else
m_pReceiver->PrintInfo("Server: recvfrom() failed with error code: "
+ WSAGetLastError());
@
[/quote]edit: Please use @ tags around code sections; Andre
-
done thanks for the advice, this is just a sample of the codes, so once i receive a packet from the sender it will display the raw data as string. I will try to post a picture of the software when running tomorrow in my school pc
-
It is exactly what I thought: you're not letting the event loop do it's work. Don't use a
@
while (1) { }
@
construct. Network communications in Qt are supposed to asynchronous. -
i think i show u the entire set of codes just in case....
@int ReceiverThread::StartReceiver()
{
WSADATA wsaData;
SOCKET ReceivingSocket;
SOCKADDR_IN ReceiverAddr, SenderAddr;
int SenderAddrSize = sizeof(SenderAddr), Port = 5150, BufLength = 6000,
ByteReceived = 5, SelectTiming, ErrorCode;
char ReceiveBuf[6000];// Initialize Winsock version 2.2 if( WSAStartup(MAKEWORD(2,2), &wsaData) != 0) { m_pReceiver->PrintInfo("Server: WSAStartup failed with error %ld\n" + WSAGetLastError()); return -1; } else m_pReceiver->PrintInfo("Server: The Winsock DLL status is " + QString(wsaData.szSystemStatus)); // Create a new socket to receive datagrams on. ReceivingSocket = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP); if (ReceivingSocket == INVALID_SOCKET) { m_pReceiver->PrintInfo("Server: Error at socket(): " + WSAGetLastError()); WSACleanup(); // Exit with error return -1; } else m_pReceiver->PrintInfo("Server: socket() is OK!"); // Set up a SOCKADDR_IN structure that will tell bind that we want to // receive datagrams from all interfaces using port 5150. // The IPv4 family ReceiverAddr.sin_family = AF_INET; // Port no. 5150 ReceiverAddr.sin_port = htons(Port); // From all interface (0.0.0.0) ReceiverAddr.sin_addr.s_addr = htonl(INADDR_ANY); // Associate the address information with the socket using bind. At this // point you can receive datagrams on your bound socket. if (bind(ReceivingSocket, (SOCKADDR *)&ReceiverAddr, sizeof(ReceiverAddr)) == SOCKET_ERROR) { m_pReceiver->PrintInfo("Server: bind() failed! Error: " + WSAGetLastError()); closesocket(ReceivingSocket); WSACleanup(); // and exit with error return -1; } else m_pReceiver->PrintInfo("Server: bind() is OK!"); // Some info on the receiver side... getsockname(ReceivingSocket, (SOCKADDR *)&ReceiverAddr, (int *)sizeof(ReceiverAddr)); m_pReceiver->PrintInfo("Server: Receiving IP(s) used: " + QString(inet_ntoa(ReceiverAddr.sin_addr)) + "\nServer: Receiving port used: " + QString(htons(ReceiverAddr.sin_port)) + "\nServer: I\'m ready to receive a datagram..."); SelectTiming = RecvFromTimeoutUDP(ReceivingSocket, RCV_TIMEOUT, 0); switch (SelectTiming) { case 0: // Timed out, do whatever you want to handle this situation m_pReceiver->PrintInfo("Server: Timeout while waiting for client!..."); break; case -1: // Error occurred, maybe we should display an error message? Need more // tweaking here and the recvfromTimeOutUDP()... m_pReceiver->PrintInfo("Server: Some error encountered with code number: " + WSAGetLastError()); break; default: while (1) { // Call recvfrom() to get it then display the received data... ByteReceived = recvfrom(ReceivingSocket, ReceiveBuf, BufLength, 0, (SOCKADDR *)&SenderAddr, &SenderAddrSize);
if (ByteReceived > 0)
m_pReceiver->PrintInfo("\n\nServer: Total Bytes received: "
+ QString(ByteReceived)
+ "\nServer: The data is: "
+ QString(ReceiveBuf));
else if (ByteReceived <= 0)
m_pReceiver->PrintInfo("Server: Connection closed with error code: "
+ WSAGetLastError());
else
m_pReceiver->PrintInfo("Server: recvfrom() failed with error code: "
+ WSAGetLastError());// Some info on the sender side getpeername(ReceivingSocket, (SOCKADDR *)&SenderAddr, &SenderAddrSize); m_pReceiver->PrintInfo("Server: Sending IP used: " + QString(inet_ntoa(SenderAddr.sin_addr)) + "Server: Sending port used: " + QString(htons(SenderAddr.sin_port))); } } // When your application is finished receiving datagrams close the socket. m_pReceiver->PrintInfo("Server: Finished receiving. Closing the listening socket..."); if (closesocket(ReceivingSocket) != 0) m_pReceiver->PrintInfo("Server: closesocket() failed! Error code: " + WSAGetLastError()); else m_pReceiver->PrintInfo("Server: closesocket() is OK..."); // When your application is finished call WSACleanup. m_pReceiver->PrintInfo("Server: Cleaning up..."); if(WSACleanup() != 0) m_pReceiver->PrintInfo("Server: WSACleanup() failed! Error code: " + WSAGetLastError()); else m_pReceiver->PrintInfo("Server: WSACleanup() is OK"); // system("PAUSE"); return 0;
}@
-
The program is running on TextEdit but due to the disappearance of the text, I post this tread to ask whether is it a bug, and can it be solved, for now to solve it, I have to select empty area then the text will show... Is there a solution? The problem lies with the TextEdit
-
Analysis stays exactly the same. Your while(1) loop is the main issue.
-
I am a novice in programming, so what do you suggest I use?
-
So I can assume once I solve the while(1) loop the TextEdit should have no problems right?
-
Moderators note: please edit previous comments instead of posting a new reply seconds after your last one.
On topic: yes, getting rid of the bussy waiting loop will solve the issue, but doing that will require a re-design of your application.
-
so i just stay as it is?
-
No. You do the needed re-design. I was just trying to warn you that it might be trickyer than it looks initially to get rid of the loop. Just because something is difficult or a lot of work, does not mean it shouldn't be done or isn't worth doing.
-
So I just redesign the loop change it to another function, then it will work? Is that what you mean?
-
You need to come up with an asyncronous design. A design that allows to return to the event loop after each time you add output to your text edit. If you just move the loop to another function, nothing has changed.
Basically, instead of polling continuiously if there is new data to display, you need to switch to a design where either only poll at a specific interval, or much better, don't poll at all but react to a signal, event or interupt or something like that as the data actually comes in.