Results 1 to 2 of 2

Thread: QThread and QDataTable

  1. #1
    Join Date
    Sep 2006
    Location
    Rio de Janeiro, Brazil
    Posts
    44
    Thanks
    2
    Thanked 3 Times in 3 Posts
    Qt products
    Qt3 Qt4
    Platforms
    MacOS X Unix/X11

    Default QThread and QDataTable

    I have one Form1 that it needs update immediate, when somebody I inserted some data in the DB automatically this data must appear in the Form1 of the user.

    For this I am using the events NOTIFY / LISTEN of PostgreSQL and QThreads.

    More I am having difficulties to give refresh in the screen.

    they observe the code.

    Qt Code:
    1. /////////////// thread.h ///////////////////////////////
    2. #ifndef THREAD_H
    3. #define THREAD_H
    4.  
    5. #include <qthread.h>
    6.  
    7. class Thread : public QThread
    8. {
    9. public:
    10. Thread();
    11.  
    12. virtual void backSqlThreadStart();
    13.  
    14. virtual void run();
    15. virtual void stop();
    16.  
    17. private:
    18. volatile bool stopped;
    19. };
    20. #endif
    To copy to clipboard, switch view to plain text mode 
    ------------------------------------------------------------------------------

    Qt Code:
    1. /////////////// thread.cpp ///////////////////////////////
    2. extern "C" {
    3. #include <stdio.h>
    4. #include <stdlib.h>
    5. #include <string.h>
    6. #include <errno.h>
    7. #include <sys/time.h>
    8. #include "libpq-fe.h"
    9. }
    10.  
    11. #include "thread.h"
    12.  
    13. Thread::Thread()
    14. {
    15. stopped = false;
    16. }
    17.  
    18. static void exit_nicely(PGconn *conn)
    19. {
    20. PQfinish(conn);
    21. }
    22.  
    23. void Thread::backSqlThreadStart()
    24. {
    25. const char *conninfo;
    26. PGconn *conn;
    27. PGresult *res;
    28. PGnotify *notify;
    29. int nnotifies;
    30.  
    31. conninfo = "dbname = developers";
    32.  
    33. /* Make a connection to the database */
    34. conn = PQconnectdb(conninfo);
    35.  
    36. /* Check to see that the backend connection was successfully made */
    37. if (PQstatus(conn) != CONNECTION_OK)
    38. {
    39. fprintf(stderr, "Connection to database '%s' failed.\n", PQdb(conn));
    40. fprintf(stderr, "%s", PQerrorMessage(conn));
    41. exit_nicely(conn);
    42. }
    43.  
    44. /*
    45.   * Issue LISTEN command to enable notifications from the rule's NOTIFY.
    46.   */
    47. res = PQexec(conn, "LISTEN TBL2");
    48. if (PQresultStatus(res) != PGRES_COMMAND_OK)
    49. {
    50. fprintf(stderr, "LISTEN command failed: %s", PQerrorMessage(conn));
    51. PQclear(res);
    52. exit_nicely(conn);
    53. }
    54.  
    55. /*
    56.   * should PQclear PGresult whenever it is no longer needed to avoid
    57.   * memory leaks
    58.   */
    59. PQclear(res);
    60.  
    61. /* Quit after four notifies are received. */
    62. nnotifies = 0;
    63. while (nnotifies < 200)
    64. {
    65. /*
    66.   * Sleep until something happens on the connection. We use select(2)
    67.   * to wait for input, but you could also use poll() or similar
    68.   * facilities.
    69.   */
    70. int sock;
    71. fd_set input_mask;
    72.  
    73. sock = PQsocket(conn);
    74.  
    75. if (sock < 0)
    76. break; /* shouldn't happen */
    77.  
    78. FD_ZERO(&input_mask);
    79. FD_SET(sock, &input_mask);
    80.  
    81. if (select(sock + 1, &input_mask, NULL, NULL, NULL) < 0)
    82. {
    83. fprintf(stderr, "select() failed: %s\n", strerror(errno));
    84. exit_nicely(conn);
    85. }
    86.  
    87. /* Now check for input */
    88. PQconsumeInput(conn);
    89. while ((notify = PQnotifies(conn)) != NULL)
    90. {
    91. fprintf(stderr,
    92. "ASYNC NOTIFY of server FNS2 '%s' received from backend pid %d\n",
    93. notify->relname, notify->be_pid);
    94. PQfreemem(notify);
    95. nnotifies++;
    96. }
    97. }
    98.  
    99. fprintf(stderr, "Done.\n");
    100.  
    101. /* close the connection to the database and cleanup */
    102. PQfinish(conn);
    103.  
    104. return true;
    105. }
    106.  
    107. void Thread::run()
    108. {
    109. while (!stopped)
    110. backSqlThreadStart();
    111. stopped = false;
    112.  
    113. }
    114.  
    115. void Thread::stop()
    116. {
    117. stopped = true;
    118. }
    To copy to clipboard, switch view to plain text mode 
    -------------------------------------------------------------------------------

    In form1 I invoke thread, and the notifications appear in the console, the problem are to bring update the QDataTable of the Form1.

    As I inside make of my QThread archive to bring up to date the QDataTable of the Form1 is accurately in this part of the code.

    Qt Code:
    1. ///////////////////// thread.cpp //////////////////////////////
    2. /* Now check for input */
    3. PQconsumeInput(conn);
    4. while ((notify = PQnotifies(conn)) != NULL)
    5. {
    6. /* some thing of the type */
    7. //Parent->dataTable->refresh();
    8.  
    9. fprintf(stderr,
    10. "ASYNC NOTIFY of server FNS2 '%s' received from backend pid %d\n",
    11. notify->relname, notify->be_pid);
    12. PQfreemem(notify);
    13. nnotifies++;
    14. }
    15. }
    To copy to clipboard, switch view to plain text mode 
    -----------------------------------------------------------------

    They forgive for the bad English,

    Ederson.

    Update! Added code tags.
    Last edited by e8johan; 11th October 2006 at 08:21.

  2. #2
    Join Date
    Jan 2006
    Location
    Alingsås, Sweden
    Posts
    437
    Thanks
    3
    Thanked 39 Times in 39 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QThread and QDataTable

    How come you are using PQ directly instead of through the QtSql module?

    The obvious way to do what you want to do is to emit a signal to the dialog in question as soon as a change takes place.

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.