Search code examples
pythonpyqtpyqt4

PyQT table update crash easyly


i first use PyQT4 .

i'm create a QTableWidget to show runing message...

when my program run, it ill crash Within ten minutes.

i try diable my TableUpdate function , and it's don't crash again.

there is my code please help me

class table_work(QThread):
    TableDataSignal = pyqtSignal()
    def __init__(self,main_self):
        # QThread.__init__(self)
        super(table_work, self).__init__(main_self)
        self.main_self = main_self
        self.table_update_list = list()

    @pyqtSlot(dict)
    def update_table_thread_o(self,work):

        try:
            row_pos = work['row_position']
            data = work['data']
            table_key_sort = work['key_sort']
            this_table = work['table']
            k = 0
            for table_key in table_key_sort:
                this_table.setItem(row_pos, k, QTableWidgetItem(unicode(data[table_key])))
                k += 1
            del work

        except:
           pass

    def update_table_thread(self):
        main_self = self.main_self

        table_work_list = self.table_update_list
        while 1:

            for work in self.table_update_list:
                row_pos = work['row_position']
                data = work['data']
                table_key_sort = work['key_sort']
                this_table = work['table']
                k = 0
                for table_key in table_key_sort:
                    this_table.setItem(row_pos, k, QTableWidgetItem(unicode(data[table_key])))
                    k += 1

            time.sleep(0.5)

    def run(self):
        self.update_table_thread()

this's update table message

 def update_table(self,address,change_obj=None,tabe_name='auto_card'):

        sample_dict = dict()
        table_key_sort = list()
        now_table_sort = 0

        if tabe_name == "auto_bot":
            this_table = self.auto_bot_procc_table
            table_data_list = self.auto_bot_procc_table_list
            now_table_sort = self.auto_bot_now_table_sort
            sample_dict['address'] = address
            sample_dict['money'] = 0
            sample_dict['run_time'] = 0
            sample_dict['item_cd'] = u"60分鐘後"
            sample_dict['stat'] = "Ready..."
            sample_dict['sort'] = now_table_sort

            table_key_sort.append('address')
            table_key_sort.append('money')
            table_key_sort.append('run_time')
            table_key_sort.append('item_cd')
            table_key_sort.append('stat')

        if tabe_name == "auto_card":
            this_table = self.process_table
            table_data_list =  self.now_procc_table_list
            now_table_sort = self.now_table_sort

            sample_dict['address'] = address
            sample_dict['done_num'] = 0
            sample_dict['pre_item'] = ""
            sample_dict['procc'] = "Ready"
            sample_dict['mission_procc'] = u"待命.."
            sample_dict['mission_num'] = 0
            sample_dict['mission_line'] = 0
            sample_dict['update_time'] = db.get_time()
            sample_dict['sort'] = now_table_sort
            sample_dict['option'] = ""


            table_key_sort.append('address')
            table_key_sort.append('done_num')
            table_key_sort.append('pre_item')
            table_key_sort.append('mission_procc')
            table_key_sort.append('procc')
            table_key_sort.append('mission_num')
            table_key_sort.append('mission_line')
            table_key_sort.append('update_time')

        if address not in table_data_list:
            this_table.insertRow(sample_dict['sort'])
            table_data_list[address] = sample_dict
            sample_dict['sort'] = self.auto_bot_now_table_sort
            self.auto_bot_now_table_sort += 1


        acc_data = table_data_list[address]
        if change_obj != None:
            key = change_obj['key']
            val = change_obj['val']
            if key in acc_data:
                acc_data[key] = val
                acc_data['update_time'] = db.get_time()

        rowPosition = acc_data['sort']


        temp = dict()
        temp['row_position'] = rowPosition
        temp['data'] = acc_data
        temp['key_sort'] = table_key_sort
        temp['table'] = this_table

        self.TableDataSignal.emit(temp) 
        del temp

Solution

  • Some time i get a ANS.

    i'm a PYQT newbie , After this period of various projects experience.

    I understand if you don't use Main Thread to Change UI, Always use sign/emit

    even your code is worked,but always use sign/emit, Otherwise there will be a series of disasters.

    you just like

    class sample(QtCore.QThread):
         table_data_change = QtCore.pyqtSignal(dict)
         def __init__(self,main_win):
            self.main = main_win
            self.table_data_change.connect(self.main.change_fn)           
         def test(self):
            data = dict()
            data['btn'] = .....
            data['val'] = .....        
            self.table_data_change.emit(data)   
    

    Save your time !