Results 1 to 13 of 13

Thread: cannot write to QListWidget or QLineEdit

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

    Default cannot write to QListWidget or QLineEdit

    I have isolated the problem as follows:

    I have some old old code for a TCP/IP server.

    I cannot use QTCPIPServer or any Qt network programs!

    I must use Qt 4 designer to make a gui to communicate with the TCP/IP server!

    I press a button and a slot program calls the TCP/IP constructor.

    The TCP/IP constructor calls the usual socket, bing, listen accept.

    The program continues to work as a server but the code to write to a QListWidget or QLineEdit object does not work.

    I've even tried putting the constructor in its own thread.

    I've tried various placements of QApplication::processEvents();

    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.  
    13. ui.listWidgetServer->addItem(QString("server constructor")); ///works here
    14.  
    15. }
    16.  
    17. tcpip::~tcpip()
    18. {
    19. }
    20. void tcpip::StartTCPServer()
    21. {
    22. ui.listWidgetServer->addItem(QString("server StartTCPServer")); // not here
    23.  
    24. // QApplication::processEvents();
    25. bool ok = true;
    26. QString qstr = ui.lineEditTCPIPServerPortServer->text();
    27. port = qstr.toInt(&ok);
    28. //qDebug()<<" server port "<<port;
    29. ui.lineEditServerStatus->setText("server started");
    30.  
    31. QApplication::processEvents();
    32.  
    33. tcpipThread tcpipThreadStart(ui, port);
    34. tcpipThreadStart.run();
    35. tcpipThreadStart.wait(ULONG_MAX);
    36.  
    37. }
    38. void tcpip::StartTCPClient()
    39. {
    40. ui.listWidgetClient->addItem(QString("client StartTCPClient"));
    41. bool ok = true;
    42. QString qstr = ui.lineEditTCPIPServerPortClient->text();
    43.  
    44. //QApplication::processEvents();
    45. ushort port = qstr.toInt(&ok);
    46. //qDebug()<<"client port = "<<port;
    47. tcpip_client* p_client = new tcpip_client((int)19999,port,FALSE, ui);
    48.  
    49. str_client(p_client, ui); /* process the request */
    50. close();
    51. }
    52.  
    53.  
    54. }
    55. void tcpip::currChanged(QWidget*){};
    56.  
    57. void tcpipThread::run()
    58. {
    59. tcpip_server* p_serv = new tcpip_server((int)19999,port,tcpip_server::str_echo, ui);
    60. qDebug()<<"oops...back from CTOR";
    61. delete p_serv;
    62. }
    To copy to clipboard, switch view to plain text mode 
    Last edited by jpn; 11th August 2008 at 21:25. Reason: missing [code] tags

  2. #2
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    5,372
    Thanks
    28
    Thanked 976 Times in 912 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: cannot write to QListWidget or QLineEdit

    Quote Originally Posted by landonmkelsey View Post
    tcpipThread tcpipThreadStart(ui, port);
    tcpipThreadStart.run();
    tcpipThreadStart.wait(ULONG_MAX);
    To start the thread you have to invoke QThread::start(), not run(). And don't wait for the thread here, because you will block the event loop and your GUI won't work.

  3. The following user says thank you to jacek for this useful post:

    landonmkelsey (16th August 2008)

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

    Default

    Thanks I'll try that !

    Strange that QApplication:rocessEvents(); didn't help!

    getting a message from qtcentre but it states that popup blocker is the problem.

    I think I have the code as you recommended. Still doesn't work!

    The tcp/ip section still works under the cover of the gui.

    BTW the gui features (close, etc) are also locked up!

    Can't be far from success!

    The Qt documentation example for QThread uses run().

    Qt Code:
    1. // tcpip.h
    2. #ifndef TCPIP_H
    3. #define TCPIP_H
    4. #include <QThread>
    5. #include <QtGui/QWidget>
    6. #include "ui_tcpip.h"
    7. class tcpip_client;
    8. class tcpip : public QWidget
    9. {
    10. Q_OBJECT
    11.  
    12. public:
    13. tcpip(QWidget *parent = 0);
    14. ~tcpip();
    15.  
    16. private:
    17. Ui::tcpipClass ui;
    18. ushort port;
    19. private slots:
    20. void StartTCPServer();
    21. void StartTCPClient();
    22. void str_client(tcpip_client* tcpip_x, Ui::tcpipClass ui);
    23. void currChanged(QWidget*);
    24. };
    25. class tcpipThread : public QThread
    26. {
    27. Ui::tcpipClass ui;
    28. ushort port;
    29. public:
    30. tcpipThread( Ui::tcpipClass uiparm, unsigned portParam):ui(uiparm),port(portParam)
    31. {}
    32. void run(){}
    33. void start();
    34. };
    35.  
    36. #endif // TCPIP_H
    37.  
    38. // tcpip.cpp
    39.  
    40. #include "tcpip.h"
    41. #include "tcpip_server.cpp"
    42. #include "tcpip_client.cpp"
    43. #include <iostream>
    44. //#include <QtDebug>
    45. tcpip::tcpip(QWidget *parent)
    46. : QWidget(parent)
    47. {
    48. ui.setupUi(this);
    49. connect( ui.pushButtonStartTCPServer, SIGNAL(clicked()), this, SLOT(StartTCPServer()) );
    50. connect( ui.pushButtonStartTCPClient, SIGNAL(clicked()), this, SLOT(StartTCPClient()) );
    51.  
    52. ui.listWidgetServer->addItem(QString("server constructor"));
    53.  
    54. }
    55.  
    56. tcpip::~tcpip()
    57. {
    58. }
    59. void tcpip::StartTCPServer()
    60. {
    61. ui.listWidgetServer->addItem(QString("server StartTCPServer"));
    62.  
    63. // QApplication::processEvents();
    64. bool ok = true;
    65. QString qstr = ui.lineEditTCPIPServerPortServer->text();
    66. port = qstr.toInt(&ok);
    67. //qDebug()<<" server port "<<port;
    68. ui.lineEditServerStatus->setText("server started");
    69.  
    70. QApplication::processEvents();
    71.  
    72. tcpipThread tcpipThreadStart(ui, port);
    73. tcpipThreadStart.start();
    74. // tcpipThreadStart.wait(ULONG_MAX);
    75.  
    76. }
    77. void tcpip::StartTCPClient()
    78. {
    79. ui.listWidgetClient->addItem(QString("client StartTCPClient"));
    80. bool ok = true;
    81. QString qstr = ui.lineEditTCPIPServerPortClient->text();
    82.  
    83. //QApplication::processEvents();
    84. ushort port = qstr.toInt(&ok);
    85. //qDebug()<<"client port = "<<port;
    86. tcpip_client* p_client = new tcpip_client((int)19999,port,FALSE, ui);
    87.  
    88. str_client(p_client, ui); /* process the request */
    89. close();
    90. }
    91. //
    92. #define MAXLINE 512
    93.  
    94. // str_client is the client processing program
    95. // particular to the application and depends on
    96. // what the server is doing
    97. void tcpip::str_client(tcpip_client* tcpip_x, Ui::tcpipClass ui)
    98. {
    99. int n;
    100. char sendline[MAXLINE], recvline[MAXLINE + 1];
    101. bool done=FALSE;
    102. while (!done)
    103. {
    104. cout<<"enter keyboard string"<<endl;
    105. cin.getline(sendline,256);
    106. n = strlen(sendline);
    107. if(n==0) break;
    108. sendline[n] = '\n'; /* newline terminate */
    109. sendline[n+1] = 0; /* newline terminate */
    110. n++;
    111. // use our client object to write to the server
    112. ui.listWidgetClient->addItem(QString("client constructorstr_client"));
    113. tcpip_x->tcp_write(sendline,n);
    114. cout<<"sendline:"<<sendline<<endl;
    115. /*
    116.   * Now read a line from the socket and write it to
    117.   * our standard output.
    118.   */
    119.  
    120. n = tcpip_x->tcp_read(recvline);
    121. recvline[n] = '\n'; /* null terminate */
    122. recvline[n+1] = 0; /* null terminate */
    123. n++;
    124. cout<<"bytes received "<<n;
    125. cout<<"client received:echo = "<<recvline<<endl;
    126. }
    127.  
    128. }
    129. void tcpip::currChanged(QWidget*){};
    130.  
    131. void tcpipThread::start()
    132. {
    133. tcpip_server* p_serv = new tcpip_server((int)19999,port,tcpip_server::str_echo, ui);
    134. qDebug()<<"oops...back from CTOR";
    135. delete p_serv;
    136. }
    To copy to clipboard, switch view to plain text mode 
    Last edited by wysota; 3rd December 2009 at 11:02.

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

    Default Re: cannot write to QListWidget or QLineEdit

    You are creating your thread on stack, so it goes out of scope immediately. I doubt it is what you want. Then your str_client performs an endless loop, blocking Qt's event loop from processing events hence the GUI freeze. So currently you should observe your GUI freezing and your server not working.

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

    landonmkelsey (16th August 2008)

  7. #5
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    5,372
    Thanks
    28
    Thanked 976 Times in 912 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: cannot write to QListWidget or QLineEdit

    Quote Originally Posted by landonmkelsey View Post
    getting a message from qtcentre but it states that popup blocker is the problem.
    You can access your private messages by clicking "User CP" link in the top-left or "Private Messages" in the top-right corner.

  8. The following user says thank you to jacek for this useful post:

    landonmkelsey (12th August 2008)

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

    Wink

    Quote Originally Posted by wysota View Post
    You are creating your thread on stack, so it goes out of scope immediately. I doubt it is what you want. Then your str_client performs an endless loop, blocking Qt's event loop from processing events hence the GUI freeze. So currently you should observe your GUI freezing and your server not working.
    The server continues to work but maybe you've given me a critical clue!

    Thanks!

    The gui is truly locked up!

    It is actually str_echo that works in the server.

    Later I must straighten out all this code!

    ah legacy code!

    problem str_echo is not called until a client "calls" in.

    The gui fails/locks up before then!

    I did fix this...thanks!

    tcpipThread* ptcpipThreadStart = new tcpipThread(ui, port);
    ptcpipThreadStart->start();

    the listen(), accept() blocks are now in their own thread

    Qt Code:
    1. void tcpipThread::start()
    2. {
    3. tcpip_server* p_serv = new tcpip_server((int)19999,port,tcpip_server::str_echo, ui);
    4. qDebug()<<"oops...back from CTOR";
    5. delete p_serv;
    6. }
    To copy to clipboard, switch view to plain text mode 

    The problem still exists!

    It is unusual that I cannot write to QListWidget or QLineEdit even if I put a sleep(10) before the server is instantiated in its own thread!
    Last edited by wysota; 3rd December 2009 at 11:02.

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

    Default Re: cannot write to QListWidget or QLineEdit

    note the use of dup2 to try another file descriptor
    note that even putting a sleep(10) ahead of all this does not allow write to
    QLineEdit and QListWidget

    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. public:
    21. static bool running;
    22.  
    23. static void* str_echo(void* p_void);
    24. // struct sock_addr_in client_ip_struct;
    25. public:
    26.  
    27. tcpip_server(int accept_ip_addr,
    28. ushort port_port,
    29. p_VOID_FUNCT p_void_funct, Ui::tcpipClass ui):
    30. accept_ip_address(accept_ip_addr),
    31. port(port_port)
    32.  
    33. {
    34.  
    35. int setreuse=1;
    36. int istatus;
    37.  
    38. //
    39. // server constructor section
    40. //
    41.  
    42. /*
    43.   * Open a TCP socket (an Internet stream socket).
    44.   */
    45. if ( (socketfd = socket(AF_INET, SOCK_STREAM, 0)) < 0)
    46. {
    47. qDebug()<<"socketfd = socket(AF_INET, SOCK_STREAM, 0)) < 0";
    48. return;
    49. }
    50. qDebug()<<"socketfd = socket(AF_INET, SOCK_STREAM, 0))"<<socketfd;
    51. int errorDup = dup2(socketfd,20);
    52. qDebug()<<"errorDup"<<errorDup;
    53. qDebug()<<"socketfd = socket(AF_INET, SOCK_STREAM, 0))"<<socketfd;
    54. /*
    55.   * Bind our local address so that the client can send to us.
    56.   */
    57.  
    58. memset((char *) &serv_addr,0, sizeof(serv_addr));
    59. serv_addr.sin_family = AF_INET;
    60. serv_addr.sin_addr.s_addr = htonl(INADDR_ANY);
    61. serv_addr.sin_port = htons(SERV_TCP_PORT);
    62.  
    63. /********************* set up so several server processes **************/
    64. /***** can use the same port ******************************************/
    65.  
    66. if(istatus = setsockopt(socketfd, SOL_SOCKET, SO_REUSEADDR,
    67. (char *) &setreuse, sizeof(setreuse)))
    68. {
    69. qDebug()<<"setsockopt";
    70. return;
    71. }
    72.  
    73.  
    74. if (bind(socketfd, (struct sockaddr *) &serv_addr, sizeof(serv_addr)) < 0)
    75. {
    76. qDebug()<<"bind";
    77. return;
    78. }
    79.  
    80. listen(socketfd, 5);
    81.  
    82. tstash<sock_io> vector_psio;
    83. void* p_void = NULL;
    84. running = true;
    85. while(1)
    86. {
    87.  
    88. clilen = sizeof(cli_addr);
    89. qDebug()<<"enter accept()";
    90. newsocketfd = accept(socketfd, (struct sockaddr *) &cli_addr,
    91. &clilen);
    92. // (unsigned int*)&clilen);
    93. qDebug()<<"newsocketfd"<<newsocketfd;
    94. qDebug()<<"exit accept()";
    95. if (newsocketfd < 0)
    96. {
    97.  
    98. return;
    99. }
    100.  
    101.  
    102. sock_io* p_sio = new sock_io( newsocketfd);
    103. p_void = (void*)p_sio;
    104.  
    105. tthread* p_thread = new tthread( p_void_funct, p_void);
    106.  
    107. p_thread->run();
    108.  
    109. }
    110.  
    111.  
    112. }
    113. // destructor
    114. ~tcpip_server()
    115. {
    116. close(socketfd); /* close original socket */
    117.  
    118. }
    119. };// end of class declaration
    120.  
    121. /*
    122.  * Read a stream socket one line at a time, and write each line back
    123.  * to the sender.
    124.  */
    125.  
    126. #define MAXLINE 512
    127. bool tcpip_server::running = true;
    128. int tcpip_server::socketfd = 0;
    129. int tcpip_server::newsocketfd = 0;
    130. void* tcpip_server::str_echo(void* p_void)
    131. {
    132.  
    133. sock_io* p_sio = (sock_io*)p_void;
    134. int n;
    135. char line[MAXLINE];
    136. const char* pline = line;
    137. bool exitNow = false;
    138. do
    139. {
    140.  
    141.  
    142. n = p_sio->tcp_read(line);
    143. // cout<<n<<"characters received"<<endl;
    144. if(!n)
    145. {
    146. running = false;
    147. close(socketfd);
    148. close(newsocketfd);
    149. qDebug()<<"str_echo tried to close";
    150. exit(0);
    151. }
    152. line[n] = '\n';
    153. line[n+1] = 0;
    154. n++;
    155. QString qstr(pline);
    156. qstr.trimmed();
    157. qDebug()<<"_____"<<qstr<<"_________";
    158.  
    159. if (qstr == QString("close"))
    160. {
    161. qDebug()<<"close detected";
    162. running = false;
    163. close(socketfd);
    164. close(newsocketfd);
    165. exitNow = true;
    166. break;
    167. }
    168.  
    169. p_sio->tcp_write(line, n);
    170. } while(1);
    171.  
    172. void* p_void_exit=0;
    173. // destruct thread object, work done
    174.  
    175. delete p_sio;
    176.  
    177. pthread_exit(p_void_exit);
    178. if (exitNow) exit(0);
    179. return p_void_exit;
    180.  
    181. }
    To copy to clipboard, switch view to plain text mode 

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

    Default Re: cannot write to QListWidget or QLineEdit

    Ok, could you pinpoint where in your code exactly the GUI gets frozen? You might do it using a debugger or by placing qDebug() statements in your code to see where the execution stops. It's hard for us to trace your code as there are many function calls to 3rd party code, so you have to do it yourself.

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

    landonmkelsey (16th August 2008)

  13. #9
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    5,372
    Thanks
    28
    Thanked 976 Times in 912 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: cannot write to QListWidget or QLineEdit

    Quote Originally Posted by landonmkelsey View Post
    Qt Code:
    1. void tcpipThread::start()
    2. {
    3. tcpip_server* p_serv = new tcpip_server((int)19999,port,tcpip_server::str_echo, ui);
    4. qDebug()<<"oops...back from CTOR";
    5. delete p_serv;
    6. }
    To copy to clipboard, switch view to plain text mode 
    If tcpipThread is a QThread subclass, this should be in run().
    Last edited by jacek; 14th August 2008 at 22:47. Reason: typo

  14. The following user says thank you to jacek for this useful post:

    landonmkelsey (16th August 2008)

  15. #10
    Join Date
    Aug 2007
    Posts
    166
    Thanks
    16
    Thanked 14 Times in 14 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    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.

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

    landonmkelsey (16th August 2008)

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

    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 11:03.

  18. #12
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    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.

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

    landonmkelsey (18th August 2008)

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

    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, 15:13
  2. QValidator, regular expressions and QLineEdit
    By hvengel in forum Qt Programming
    Replies: 1
    Last Post: 8th August 2007, 01:25
  3. Replies: 13
    Last Post: 15th December 2006, 11: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
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.