Results 1 to 12 of 12

Thread: Querying within Threads

  1. #1
    Join Date
    Mar 2006
    Posts
    13
    Thanks
    3
    Qt products
    Qt3
    Platforms
    Unix/X11

    Default Querying within Threads

    Hi,
    I am using Qt3.3 and i am geting segmentation fault when i try to query within my thread!! I am trying to implement threads as i want the GUI to be responsive when querying is being done. The function which does the query is defined within a class which has a macro Q_OBJECT cause other functions in that class need a timer for database insertion. I have created a separate thread class by sub classing QThread. I have then over ridden the run() function to call this query method.

    Please help!!
    Thanking you,
    Regards,
    Bera

  2. #2
    Join Date
    Jan 2006
    Location
    La Spezia,Italy
    Posts
    77
    Thanks
    9
    Thanked 5 Times in 5 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Querying within Threads

    Please,post the code.

  3. #3
    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: Querying within Threads


  4. #4
    Join Date
    Mar 2006
    Posts
    13
    Thanks
    3
    Qt products
    Qt3
    Platforms
    Unix/X11

    Default Re: Querying within Threads

    Sorry dude,
    I can't post the code!!
    Its illegal as i am working for a firm..
    It would be very helpful if u could provide me with some suggestions..
    Thanks.

  5. #5
    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: Querying within Threads

    Quote Originally Posted by bera82
    Sorry dude,
    I can't post the code!!
    Its illegal as i am working for a firm..
    It would be very helpful if u could provide me with some suggestions..
    Thanks.
    I'm sure posting a few lines of code won't hurt. If you don't want to provide a sample of your code, provide a minimal compilable example which reproduces the problem.

  6. #6
    Join Date
    Mar 2006
    Posts
    13
    Thanks
    3
    Qt products
    Qt3
    Platforms
    Unix/X11

    Default Re: Querying within Threads

    This is the function that gets called within the run() function of the thread class.
    The moment it reaches "resA.exec()" it gives segmentation fault.

    resA/resB/resC.... is of type QSqlQuery.
    resPrevQueryA/.... is also of type QSqlQuery

    This same function works beautifully fine without threads but hangs the GUI!!

    Thanks,
    bera

    Qt Code:
    1. void CHistData::queryDBase()
    2. {
    3.  
    4. QSqlDatabase *dbase = QSqlDatabase::addDatabase("QMYSQL3" );
    5.  
    6. dbase->setDatabaseName( "HMIDATABASE" );
    7. dbase->setUserName( "" );
    8. dbase->setPassword( "");
    9. dbase->setHostName( "");
    10. if( !dbase->open() )
    11. {
    12. dbase->lastError().showMessage( "An error occured. Please read the README file in the sqltable"
    13. "dir for more information.\n\n" );
    14. printf("Unable to open DB");
    15. }
    16. else
    17. printf(" opened DB");
    18.  
    19.  
    20. QString strFromTime=qplot->fromTimeStr;
    21. QString strToTime=qplot->toTimeStr;
    22.  
    23. int plotScreenXPixelCounter = 0;
    24.  
    25. QString strFromDateTime=qplot->fromDateStr;
    26. QString strToDateTime=qplot->toDateStr;
    27.  
    28.  
    29. strFromDateTime.append(" ");
    30. strToDateTime.append(" ");
    31.  
    32. // Store from & to - datetime specified by user in QString
    33. strFromDateTime.append(strFromTime);
    34. strToDateTime.append(strToTime);
    35.  
    36. // Store from & to datetime specified by user in string datatype of sql
    37. QString fromDateTime = strFromDateTime;
    38. QString toDateTime = strToDateTime;
    39.  
    40. // //cout<<"FROM DateTime before query: "<<fromDateTime<<endl;
    41. // //cout<<"TO DateTime before query: "<<toDateTime<<endl;
    42. //qplot->databaseEnabled = FALSE;
    43.  
    44. // create QString object to store FGU-FSM ID selected by user
    45. QString fguFsmID;
    46. //fguFsmID ="";
    47. fguFsmID.append(qplot->options.FGUSel);
    48. fguFsmID.append(qplot->options.FSMSel);
    49.  
    50. // //cout<<" FGUId check"<<fguFsmID<<endl;
    51. QString cName;
    52. // cName.append(' ');
    53. cName.append(fguFsmID);
    54.  
    55.  
    56. // Query to retrieve records for given From and To date
    57. cName.append("A");
    58. QString retrieveA = "select * from FreqTab where ChName = '"+cName+"' and ChDateTime between '"+fromDateTime+"' and '"+toDateTime+"' order by ChDateTime ";
    59. //////////////////////////////////////////////
    60. // qApp->processEvents();
    61.  
    62. printf("before resA.exec()\n");
    63.  
    64. resA=retrieveA;
    65. resA.exec();
    66.  
    67. printf("after resA.exec()\n");
    68.  
    69. // //cout<<"querySize: checking resA"<<resA.size()<<endl;
    70. ////////////////////////////
    71. // Query to retrieve records when given From Date does not have a record+
    72. QString prevA = "select * from FreqTab where ChName = '"+cName+"' and ChDateTime between '"+fromDateTime+"' and '"+toDateTime+"' order by ChDateTime desc limit 1";
    73.  
    74. //qApp->processEvents();
    75.  
    76. resPrevQueryA=prevA;
    77. resPrevQueryA.exec();
    78. // //cout<<"querySize: checking resPrevQueryA"<<resPrevQueryA.size()<<endl;
    79.  
    80. cName.truncate(5);
    81. cName.append("B");
    82. // //cout<<"----------------------------"<<cName<<endl;
    83.  
    84. // Query to retrieve records for given From and To date
    85. QString retrieveB = "select * from FreqTab where ChName = '"+cName+"' and ChDateTime between '"+fromDateTime+"' and '"+toDateTime+"' order by ChDateTime";
    86.  
    87. //qApp->processEvents();
    88.  
    89. resB=retrieveB;
    90. resB.exec();
    91. // //cout<<"querySize: checking resB"<<resB.size()<<endl;
    92.  
    93. // Query to retrieve records when given From Date does not have a record+
    94. QString prevB = "select * from FreqTab where ChName = '"+cName+"' and ChDateTime between '"+fromDateTime+"' and '"+toDateTime+"' order by ChDateTime desc limit 1";
    95.  
    96. //qApp->processEvents();
    97.  
    98. resPrevQueryB=prevB;
    99. resPrevQueryB.exec();
    100.  
    101. //cout<<"querySize: checking resPrevQueryB"<<resPrevQueryB.size()<<endl;
    102.  
    103. cName.truncate(5);
    104. cName.append("C");
    105.  
    106. // Query to retrieve records for given From and To date
    107. QString retrieveC = "select * from FreqTab where ChName = '"+cName+"' and ChDateTime between '"+fromDateTime+"' and '"+toDateTime+"' order by ChDateTime";
    108.  
    109. //qApp->processEvents();
    110.  
    111. resC=retrieveC;
    112. resC.exec();
    113.  
    114. //cout<<"querySize: checking resC"<<resC.size()<<endl;
    115.  
    116. // Query to retrieve records when given From Date does not have a record+
    117. QString prevC = "select * from FreqTab where ChName = '"+cName+"' and ChDateTime between '"+fromDateTime+"' and '"+toDateTime+"' order by ChDateTime desc limit 1";
    118.  
    119. //qApp->processEvents();
    120.  
    121. resPrevQueryC=prevC;
    122. resPrevQueryC.exec();
    123.  
    124. //cout<<"querySize: checking resPrevQueryC"<<resPrevQueryC.size()<<endl;
    125.  
    126. cName.truncate(5);
    127. cName.append("D");
    128.  
    129. // Query to retrieve records for given From and To date
    130. QString retrieveD = "select * from FreqTab where ChName = '"+cName+"' and ChDateTime between '"+fromDateTime+"' and '"+toDateTime+"' order by ChDateTime ";
    131.  
    132. //qApp->processEvents();
    133.  
    134. resD=retrieveD;
    135. resD.exec();
    136.  
    137. //cout<<"querySize: checking resD"<<resD.size()<<endl;
    138.  
    139. // Query to retrieve records when given From Date does not have a record+
    140. QString prevD = "select * from FreqTab where ChName = '"+cName+"' and ChDateTime between '"+fromDateTime+"' and '"+toDateTime+"' order by ChDateTime desc limit 1";
    141.  
    142.  
    143. //qApp->processEvents();
    144.  
    145. resPrevQueryD=prevD;
    146. resPrevQueryD.exec();
    147.  
    148. //cout<<"querySize: checking resPrevQueryD"<<resPrevQueryD.size()<<endl;
    149.  
    150. cName.truncate(5);
    151. cName.append("E");
    152.  
    153. // Query to retrieve records for given From and To date
    154. QString retrieveE = "select * from FreqTab where ChName = '"+cName+"' and ChDateTime between '"+fromDateTime+"' and '"+toDateTime+"' order by ChDateTime ";
    155.  
    156. //qApp->processEvents();
    157.  
    158. resE=retrieveE;
    159. resE.exec();
    160.  
    161. //cout<<"querySize: checking resE"<<resE.size()<<endl;
    162.  
    163. // Query to retrieve records when given From Date does not have a record+
    164. QString prevE = "select * from FreqTab where ChName = '"+cName+"' and ChDateTime between '"+fromDateTime+"' and '"+toDateTime+"' order by ChDateTime desc limit 1";
    165. //cout<<fguFsmID<<endl;
    166.  
    167. //qApp->processEvents();
    168.  
    169. resPrevQueryE=prevE;
    170. resPrevQueryE.exec();
    171.  
    172. //cout<<"querySize: checking resPrevQueryE"<<resPrevQueryE.size()<<endl;
    173.  
    174. cName.truncate(5);
    175. cName.append("F");
    176.  
    177. // Query to retrieve records for given From and To date
    178. QString retrieveF = "select * from FreqTab where ChName = '"+cName+"' and ChDateTime between '"+fromDateTime+"' and '"+toDateTime+"' order by ChDateTime ";
    179.  
    180. // qApp->processEvents();
    181.  
    182. resF=retrieveF;
    183. resF.exec();
    184.  
    185. //cout<<"querySize : checking resF"<<resF.size()<<endl;
    186.  
    187. // Query to retrieve records when given From Date does not have a record+
    188. QString prevF = "select * from FreqTab where ChName = '"+cName+"' and ChDateTime between '"+fromDateTime+"' and '"+toDateTime+"' order by ChDateTime desc limit 1";
    189.  
    190. // qApp->processEvents();
    191.  
    192. resPrevQueryF=prevF;
    193. resPrevQueryF.exec();
    194.  
    195. //cout<<"querySize: checking "<<resPrevQueryF.size()<<endl;
    196. dbase->close();
    197. }
    To copy to clipboard, switch view to plain text mode 
    Last edited by jpn; 12th March 2008 at 19:25. Reason: missing [code] tags

  7. #7
    Join Date
    Mar 2006
    Posts
    13
    Thanks
    3
    Qt products
    Qt3
    Platforms
    Unix/X11

    Default Re: Querying within Threads

    Hey hangs the GUI as in, GUI freezes untill query is over.. then everything is fine..
    Help me out pals..
    Thanks

  8. #8
    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: Querying within Threads

    In what thread does the qplot object live?

    PS. Please, at least remove the unnecessary comments.

  9. #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: Querying within Threads

    Quote Originally Posted by bera82
    Hey hangs the GUI as in, GUI freezes untill query is over.. then everything is fine..
    How do you start that thread? Do you invoke start()?

  10. #10
    Join Date
    Mar 2006
    Posts
    13
    Thanks
    3
    Qt products
    Qt3
    Platforms
    Unix/X11

    Default Re: Querying within Threads

    Hey sorry for not removing the comments!!

    qplot is an object of the ui file which is a plotting application.
    In the above code there are lineEdits for from and to dateTime which are entered by the user and accessed here.

    The thread is invoked by the start() method from the UI file itself when the user enters the From & To datetime and presses submit. In the submit function the thread is started and the above posted code runs.

    Thank u for ur valuable time
    Bera.

  11. #11
    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: Querying within Threads

    Quote Originally Posted by bera82
    qplot is an object of the ui file which is a plotting application.
    In the above code there are lineEdits for from and to dateTime which are entered by the user and accessed here.
    Then read this carefully: http://doc.trolltech.com/3.3/threads.html
    Especially the part about classes that use implicit sharing (like QString) and about classes derived from QObject.

  12. #12
    Join Date
    Jan 2008
    Posts
    107
    Thanks
    36
    Thanked 2 Times in 2 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11

    Post Re: Querying within Threads

    Hey bera82

    Remember that each connection to the DB must be uniquely named ...

    I use the following when working with dbconns inside threads:
    Qt Code:
    1. // create a unique database connection to serve this thread
    2. // two calls to rand() are a bit expensive but *effective*...
    3. // note: this is for a PostgreSQL connection ..
    4. sprintf(dbUnique, "myDB%x%x", rand(), rand() );
    5. myDB = QSqlDatabase::addDatabase ( "QPSQL", dbUnique );
    6. // set needed conn parameters here...
    7.  
    8. // whatever code...
    9. QSqlQuery query(myDB); // this is the important part: access it by its unique name.
    10.  
    11. // remember to close after you're done... (or upon the thread's destruction)
    12. myDB.close();
    To copy to clipboard, switch view to plain text mode 

    HTH,
    Pedro Doria Meunier

Similar Threads

  1. Replies: 8
    Last Post: 27th March 2013, 11:51
  2. Why do some QWidgets create own threads?
    By donglebob in forum Qt Programming
    Replies: 5
    Last Post: 6th July 2010, 17:01
  3. No context switch between Threads and parent ?
    By donglebob in forum Qt Programming
    Replies: 15
    Last Post: 29th October 2008, 22:49
  4. Once more: Threads in Qt4
    By high_flyer in forum Qt Programming
    Replies: 5
    Last Post: 9th August 2006, 18:35

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.