Results 1 to 2 of 2

Thread: Reload function doesn't catch updates on database , closes when no data

  1. #1

    Exclamation Reload function doesn't catch updates on database , closes when no data

    My code is working great but it should reload the Qwidget to get the new data from DB every 1 min.

    Also, when I click the button to move to the 2nd UI then I submit back . if there is no data to show on the Qtable widget it simply ends the program.

    Here is my code :


    Qt Code:
    1. # -*- coding: utf-8 -*-
    2.  
    3. from PyQt4 import QtGui, QtCore
    4. import sys
    5. import main3, report
    6. import MySQLdb
    7.  
    8. db = MySQLdb.connect(host="localhost", # your host, usually localhost
    9. user="root", # your username
    10. passwd="Buii3tf0ky001@yahoo000", # your password
    11. db="health", # name of the database
    12. port=3306) # number of the data base port
    13.  
    14. cur = db.cursor()
    15.  
    16.  
    17. class report(QtGui.QMainWindow, report.Ui_Report1):
    18. def __init__(self, p_id, parent=None):
    19. super(report, self).__init__(parent)
    20. self.setupUi(self)
    21.  
    22. self.lineEdit.setText('ID: ' + str(p_id))
    23. cur.execute("SELECT * FROM patient WHERE id = '%s'" % (p_id))
    24. pt_row_report = cur.fetchall()
    25. for row in pt_row_report:
    26. pt_name_report = row[1]
    27. pt_age_report = row[2]
    28. cur.execute("SELECT * FROM clinic WHERE p_id = '%s'" % (p_id))
    29. clinic_row = cur.fetchone()
    30. d_id = clinic_row[2]
    31. ray_type1 = clinic_row[6]
    32. ray_type = ray_type1[17:]
    33. cur.execute("SELECT * FROM staff WHERE id = '%s'" % (d_id))
    34. staff_row = cur.fetchall()[0]
    35. doctor_name = staff_row[1]
    36. doctor_id = staff_row[0]
    37. self.lineEdit_2.setText('Name: ' + str(pt_name_report))
    38. self.lineEdit_3.setText('Age: ' + str(pt_age_report))
    39. self.lineEdit_6.setText(str(ray_type))
    40. self.lineEdit_7.setText('Dr. ' + str(doctor_name))
    41.  
    42. cur.execute("SELECT Name FROM staff WHERE id = 48588")
    43. ray_dr_name = cur.fetchone()[0]
    44. self.lineEdit_4.setText('Dr. ' + str(ray_dr_name))
    45. self.pushButton.clicked.connect(lambda: self.update_report(p_id, ray_type, doctor_id))
    46.  
    47. def closeEvent(self, event):
    48. First().show()
    49. event.accept()
    50.  
    51. def update_report(self, name, ray, d_id):
    52.  
    53. x = self.textEdit.toPlainText()
    54. sql = "UPDATE clinic SET Extra_Notice = (%s) WHERE p_id=(%s)"
    55. cur.execute(sql, (x, name))
    56. db.commit()
    57. cur.execute("INSERT INTO rays (p_ID, Ray_Type, Report, D_ID, RS_ID) VALUES (%s,%s,%s,%s,%s)",
    58. (int(name), ray, x, d_id, 48588))
    59. db.commit()
    60. self.hide()
    61. First().reload_data()
    62.  
    63.  
    64.  
    65.  
    66. def get_text(self, x):
    67. x = self.textEdit.text()
    68. return x
    69.  
    70.  
    71. class First(QtGui.QMainWindow, main3.Ui_MainWindow):
    72. def __init__(self, parent=None):
    73. super(First, self).__init__(parent)
    74. self.setupUi(self)
    75. timer = QtCore.QTimer(self)
    76. timer.timeout.connect(self.reload_data)
    77. timer.start(60 * 1000)
    78. self.reload_data()
    79.  
    80. def reload_data(self):
    81. self.show()
    82. self.tableWidget.clear()
    83. self.tableWidget.setRowCount(0)
    84. self.tableWidget.setColumnCount(7)
    85. self.tableWidget.setHorizontalHeaderLabels(
    86. ("Queue number;ID;Name;Age;Address;Phone;Make Operation;").split(";"))
    87. header = self.tableWidget.horizontalHeader()
    88. header.setResizeMode(0, QtGui.QHeaderView.Stretch)
    89. header.setResizeMode(1, QtGui.QHeaderView.ResizeToContents)
    90. header.setResizeMode(2, QtGui.QHeaderView.ResizeToContents)
    91.  
    92. cur.execute("SELECT * FROM clinic WHERE Extra_Notice LIKE %s", ("%{}%".format('x-ray is required'),))
    93. test_list = cur.fetchall()
    94. queue = 1
    95. for row in test_list:
    96. c_id = row[0]
    97. p_id = row[1]
    98. cur.execute("SELECT * FROM patient WHERE id = '%s'" % (p_id))
    99. pt = cur.fetchone()
    100. print(pt)
    101. if not pt:
    102. continue
    103. pt_name = pt[1]
    104. pt_age = pt[2]
    105. pt_address = pt[4]
    106. pt_phone = pt[3]
    107.  
    108. button = QtGui.QPushButton('Make Operation')
    109. button.setStyleSheet("background-color: #4f81bc; color: white;")
    110. button.clicked.connect(lambda checked, p_id=p_id: self.open_report(p_id))
    111.  
    112. rowposition = self.tableWidget.rowCount()
    113.  
    114. self.tableWidget.insertRow(rowposition)
    115.  
    116. for i, val in enumerate([queue, c_id, pt_name, pt_age, pt_address, pt_phone]):
    117. self.tableWidget.setItem(rowposition, i, QtGui.QTableWidgetItem(str(val)))
    118. self.tableWidget.setCellWidget(rowposition, 6, button)
    119. queue += 1
    120.  
    121. def open_report(self, p_id):
    122. self.child_win = report(p_id)
    123. self.child_win.show()
    124. self.hide()
    125.  
    126.  
    127. def main():
    128. app = QtGui.QApplication(sys.argv)
    129. app.setStyle('Plastique')
    130. main = First()
    131. main.update()
    132. main.show()
    133. sys.exit(app.exec_())
    134.  
    135.  
    136. if __name__ == '__main__':
    137. main()
    To copy to clipboard, switch view to plain text mode 

    Pleae keep in my mind that am using pyqt4 with python 2.7

  2. #2
    Join Date
    Jan 2008
    Location
    Alameda, CA, USA
    Posts
    3,975
    Thanks
    232
    Thanked 627 Times in 617 Posts
    Qt products
    Qt5
    Platforms
    Unix/X11 Windows Android

    Default Re: Reload function doesn't catch updates on database , closes when no data

    I am not a python expert, but aren't lines 48 and 61 creating -new- instances of First (your main window) when you should be using the single instance you create in main()?
    <=== The Great Pumpkin says ===>
    Please use CODE tags when posting source code so it is more readable. Click "Go Advanced" and then the "#" icon to insert the tags. Paste your code between them.

Similar Threads

  1. Not Able to put breakpoint inside catch() of try,catch block
    By vinothrajendran in forum Qt Programming
    Replies: 0
    Last Post: 15th July 2015, 08:22
  2. Replies: 2
    Last Post: 6th April 2014, 11:07
  3. Replies: 11
    Last Post: 5th September 2012, 20:47
  4. QTableView massive model data updates issue
    By cyberbob in forum Newbie
    Replies: 3
    Last Post: 7th December 2010, 23:09
  5. Call to database function.
    By retto in forum Qt Programming
    Replies: 1
    Last Post: 14th September 2009, 12:29

Tags for this Thread

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.