Results 1 to 13 of 13

Thread: cannot write to QListWidget or QLineEdit

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Join Date
    Aug 2007
    Posts
    166
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows
    Thanks
    16
    Thanked 14 Times in 14 Posts

    Default Re: cannot write to QListWidget or QLineEdit

    http://doc.trolltech.com/4.4/qthread.html
    Read more about the threads, you must put your code in to the run() overloaded function and to start the thread via the start() method, then you code in run() will begin to execute.

  2. The following user says thank you to The Storm for this useful post:

    landonmkelsey (16th August 2008)

  3. #2
    Join Date
    Aug 2008
    Posts
    38
    Qt products
    Qt4
    Platforms
    Unix/X11
    Thanks
    19

    Default



    Thanks...I will be working on this today and tonight!

    The Qt4 docs say that start is a slot and the QThread example describes run() in the little example..

    I *will* switch as you have directed!!!! Thanks!

    The TCP/IP server mechanism works in spite of this problem.

    What mystifies me is that the problem persists even if I put a sleep(10) ahead of the tcpip_server code!

    Thanks again!



    I got the program to work using some well placed:
    Qt Code:
    1. QApplication::processEvents(); (see tcpip.cpp):D
    To copy to clipboard, switch view to plain text mode 

    inside the server code...who knows which one made it work!

    The code hangs up in accept() as it should in TCP/IP server code.

    waiting for a client. Accept hangs up Qt 4 tight!

    I did the run/start thread code as suggested!

    I think it is working!

    Thanks!


    tcpip.h
    Qt Code:
    1. #ifndef TCPIP_H
    2. #define TCPIP_H
    3. #include <QThread>
    4. #include <QtGui/QWidget>
    5. #include "ui_tcpip.h"
    6. class tcpip_client;
    7. class tcpipThread;
    8. class tcpip : public QWidget
    9. {
    10. Q_OBJECT
    11.  
    12. public:
    13. tcpip(QWidget *parent = 0);
    14. ~tcpip();
    15.  
    16.  
    17. private:
    18. Ui::tcpipClass ui;
    19. ushort port;
    20. tcpipThread* ptcpipThreadStart;
    21. private slots:
    22. void StartTCPServer();
    23. void StartTCPClient();
    24. void str_client(tcpip_client* tcpip_x, Ui::tcpipClass ui);
    25. void currChanged(QWidget*);
    26. };
    27.  
    28. class tcpipThread : public QThread
    29. {
    30. Ui::tcpipClass ui;
    31. ushort port;
    32. public:
    33. tcpipThread( Ui::tcpipClass uiparm, unsigned portParam):ui(uiparm),port(portParam)
    34. {}
    35. void run();
    36.  
    37. };
    38.  
    39. #endif // TCPIP_H
    To copy to clipboard, switch view to plain text mode 
    tcpip.cpp
    Qt Code:
    1. #include "tcpip.h"
    2. #include "tcpip_server.cpp"
    3. #include "tcpip_client.cpp"
    4. #include <iostream>
    5. //#include <QtDebug>
    6. tcpip::tcpip(QWidget *parent)
    7. : QWidget(parent)
    8. {
    9. ui.setupUi(this);
    10. connect( ui.pushButtonStartTCPServer, SIGNAL(clicked()), this, SLOT(StartTCPServer()) );
    11. connect( ui.pushButtonStartTCPClient, SIGNAL(clicked()), this, SLOT(StartTCPClient()) );
    12. connect( ui.pushButtonClose, SIGNAL(clicked()), this, SLOT(localClose()) );
    13.  
    14. ui.listWidgetServer->addItem(QString("server constructor"));
    15.  
    16. }
    17. //
    18. tcpip::~tcpip()
    19. {
    20. delete ptcpipThreadStart;
    21. }
    22. void tcpip::StartTCPServer()
    23. {
    24. ui.listWidgetServer->addItem(QString("server1 StartTCPServer"));
    25.  
    26. QApplication::processEvents();
    27. bool ok = true;
    28. QString qstr = ui.lineEditTCPIPServerPortServer->text();
    29. port = qstr.toInt(&ok);
    30. //qDebug()<<" server port "<<port;
    31. ui.lineEditServerStatus->setText("server started");
    32.  
    33. QApplication::processEvents();
    34.  
    35. tcpipThread* ptcpipThreadStart = new tcpipThread(ui, port);
    36. ptcpipThreadStart->start();
    37.  
    38.  
    39. }
    40. // ignore the following
    41. void tcpip::StartTCPClient() // not part of the server...ignore
    42. {
    43. ui.listWidgetClient->addItem(QString("client StartTCPClient"));
    44. bool ok = true;
    45. QString qstr = ui.lineEditTCPIPServerPortClient->text();
    46.  
    47. //QApplication::processEvents();
    48. ushort port = qstr.toInt(&ok);
    49. //qDebug()<<"client port = "<<port;
    50. tcpip_client* p_client = new tcpip_client((int)19999,port,FALSE, ui);
    51.  
    52. str_client(p_client, ui); /* process the request */
    53. close();
    54. }
    55. //
    56. #define MAXLINE 512
    57.  
    58. // str_client is the client processing program
    59. // particular to the application and depends on
    60. // what the server is doing
    61. void tcpip::str_client(tcpip_client* tcpip_x, Ui::tcpipClass ui)
    62. {
    63. int n;
    64. char sendline[MAXLINE], recvline[MAXLINE + 1];
    65. bool done=FALSE;
    66. while (!done)
    67. {
    68. cout<<"enter keyboard string"<<endl;
    69. cin.getline(sendline,256);
    70. n = strlen(sendline);
    71. if(n==0) break;
    72. sendline[n] = '\n'; /* newline terminate */
    73. sendline[n+1] = 0; /* newline terminate */
    74. n++;
    75. // use our client object to write to the server
    76. ui.listWidgetClient->addItem(QString("client constructorstr_client"));
    77. tcpip_x->tcp_write(sendline,n);
    78. cout<<"sendline:"<<sendline<<endl;
    79. /*
    80.   * Now read a line from the socket and write it to
    81.   * our standard output.
    82.   */
    83.  
    84. n = tcpip_x->tcp_read(recvline);
    85. recvline[n] = '\n'; /* null terminate */
    86. recvline[n+1] = 0; /* null terminate */
    87. n++;
    88. cout<<"bytes received "<<n;
    89. cout<<"client received:echo = "<<recvline<<endl;
    90. }
    91.  
    92. }
    93. void tcpip::currChanged(QWidget*){};
    94.  
    95. void tcpipThread::run()
    96. {
    97. tcpip_server* p_serv = new tcpip_server((int)19999,port,tcpip_server::str_echo, ui);
    98. qDebug()<<"should not happen before client calls in...back from CTOR";
    99. delete p_serv;
    100. }
    To copy to clipboard, switch view to plain text mode 
    tcpip_server.cpp
    Qt Code:
    1. #include "inet.h"
    2. #include <iostream>
    3. #include <sstream>
    4. #include <exception>
    5. using namespace std;
    6. #include "thread.cpp"
    7. #include "sock_io.cpp"
    8. #include "tstash.h"
    9. #include <QtDebug>
    10.  
    11. typedef void* (*p_VOID_FUNCT)(void*);
    12. class tcpip_server {
    13. static int socketfd, newsocketfd;
    14. socklen_t clilen;
    15. struct sockaddr_in cli_addr, serv_addr;
    16. ushort port;
    17. // struct sock_addr_in accept_ip_struct;
    18. int accept_ip_address;
    19. int client_ip_address;
    20. Ui::tcpipClass ui;
    21. public:
    22. static bool running;
    23. void localClose();
    24. static void* str_echo(void* p_void);
    25. // struct sock_addr_in client_ip_struct;
    26. public:
    27.  
    28. tcpip_server(int accept_ip_addr,
    29. ushort port_port,
    30. p_VOID_FUNCT p_void_funct, Ui::tcpipClass uip):
    31. accept_ip_address(accept_ip_addr),
    32. port(port_port),
    33. ui(uip)
    34.  
    35. {
    36.  
    37. int setreuse=1;
    38. int istatus;
    39.  
    40. //
    41. // server constructor section
    42. //
    43.  
    44. /*
    45.   * Open a TCP socket (an Internet stream socket).
    46.   */
    47. if ( (socketfd = socket(AF_INET, SOCK_STREAM, 0)) < 0)
    48. {
    49. qDebug()<<"socketfd = socket(AF_INET, SOCK_STREAM, 0)) < 0";
    50. return;
    51. }
    52. qDebug()<<"socketfd = socket(AF_INET, SOCK_STREAM, 0))"<<socketfd;
    53. int errorDup = dup2(socketfd,20);
    54. qDebug()<<"errorDup"<<errorDup;
    55. qDebug()<<"socketfd = socket(AF_INET, SOCK_STREAM, 0))"<<socketfd;
    56. /*
    57.   * Bind our local address so that the client can send to us.
    58.   */
    59.  
    60. memset((char *) &serv_addr,0, sizeof(serv_addr));
    61. serv_addr.sin_family = AF_INET;
    62. serv_addr.sin_addr.s_addr = htonl(INADDR_ANY);
    63. serv_addr.sin_port = htons(SERV_TCP_PORT);
    64.  
    65. if(istatus = setsockopt(socketfd, SOL_SOCKET, SO_REUSEADDR,
    66. (char *) &setreuse, sizeof(setreuse)))
    67. {
    68. qDebug()<<"setsockopt";
    69. return;
    70. }
    71.  
    72.  
    73. if (bind(socketfd, (struct sockaddr *) &serv_addr, sizeof(serv_addr)) < 0)
    74. {
    75. qDebug()<<"bind";
    76. return;
    77. }
    78.  
    79. listen(socketfd, 5);
    80.  
    81. tstash<sock_io> vector_psio;
    82. void* p_void = NULL;
    83. running = true;
    84. while(1)
    85. {
    86. QApplication::processEvents();
    87.  
    88. clilen = sizeof(cli_addr);
    89.  
    90. newsocketfd = accept(socketfd, (struct sockaddr *) &cli_addr,
    91. &clilen);
    92.  
    93. if (newsocketfd < 0)
    94. {
    95.  
    96. return;
    97. }
    98.  
    99.  
    100. sock_io* p_sio = new sock_io( newsocketfd);
    101. p_void = (void*)p_sio;
    102.  
    103. tthread* p_thread = new tthread( p_void_funct, p_void);
    104.  
    105. p_thread->run();
    106.  
    107. }
    108.  
    109.  
    110. }
    111. // destructor
    112. ~tcpip_server()
    113. {
    114. close(socketfd);
    115.  
    116. }
    117. };// end of class declaration
    118. #define MAXLINE 512
    119. bool tcpip_server::running = true;
    120. int tcpip_server::socketfd = 0;
    121. int tcpip_server::newsocketfd = 0;
    122. void* tcpip_server::str_echo(void* p_void)
    123. {
    124.  
    125. sock_io* p_sio = (sock_io*)p_void;
    126. int n;
    127. char line[MAXLINE];
    128. const char* pline = line;
    129. bool exitNow = false;
    130. do
    131. {
    132.  
    133.  
    134. n = p_sio->tcp_read(line);
    135.  
    136. if(!n)
    137. {
    138. running = false;
    139. close(socketfd);
    140. close(newsocketfd);
    141.  
    142. exit(0);
    143. }
    144. line[n] = '\n';
    145. line[n+1] = 0;
    146. n++;
    147. QString qstr(pline);
    148. qstr.trimmed();
    149. qDebug()<<"_____"<<qstr<<"_________";
    150.  
    151. if (qstr == QString("close"))
    152. {
    153. qDebug()<<"close detected";
    154. running = false;
    155. close(socketfd);
    156. close(newsocketfd);
    157. exitNow = true;
    158. break;
    159. }
    160.  
    161. p_sio->tcp_write(line, n);
    162. } while(1);
    163.  
    164. void* p_void_exit=0;
    165. delete p_sio;
    166.  
    167. pthread_exit(p_void_exit);
    168. if (exitNow) exit(0);
    169. return p_void_exit;
    170.  
    171. }
    172. void tcpip_server::localClose()
    173. {
    174.  
    175. running = false;
    176. close(socketfd);
    177. close(newsocketfd);
    178. }
    To copy to clipboard, switch view to plain text mode 
    Last edited by wysota; 3rd December 2009 at 12:03.

  4. #3
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,376
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Thanks
    4
    Thanked 5,019 Times in 4,795 Posts
    Wiki edits
    10

    Default Re: cannot write to QListWidget or QLineEdit

    A general rule is that if you use Qt's networking mechanism in a thread (QThread), you need to start its event loop by calling QThread::exec() with all its consequences.

  5. The following user says thank you to wysota for this useful post:

    landonmkelsey (18th August 2008)

  6. #4
    Join Date
    Aug 2008
    Posts
    38
    Qt products
    Qt4
    Platforms
    Unix/X11
    Thanks
    19

    Default Re: cannot write to QListWidget or QLineEdit

    program is now working perfectly (for the desired current state)

    thanks!

    I'll try the exec()

Similar Threads

  1. Pointer Question related to QLineEdit
    By ChrisReath in forum Qt Programming
    Replies: 1
    Last Post: 23rd May 2008, 16:13
  2. QValidator, regular expressions and QLineEdit
    By hvengel in forum Qt Programming
    Replies: 1
    Last Post: 8th August 2007, 02:25
  3. Replies: 13
    Last Post: 15th December 2006, 12:52

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.