向 tablewidget 添加新行不起作用

问题描述 投票:0回答:0

我尝试从另一个类发送数据,并在每次发送数据时添加新行。 我当前的设置使用 qt designer 作为我的 ui 并在 python 中创建所有逻辑

这是我的代码

MAIN_CLASS,_=loadUiType(Path.cwd()/"addNota.ui")
NOTA_CARI_BARANG_CLASS,_=loadUiType(Path.cwd()/"cariBarangNota.ui")

class ReadOnlyDelegate(QtWidgets.QStyledItemDelegate):
    def createEditor(self, parent, option, index):
        return

class Main(QDialog, MAIN_CLASS):
    def __init__(self, parent=None):
        super(Main,self).__init__(parent)
        self.setupUi(self)
        db=sqlite3.connect("rekankita.db")
        cursor=db.cursor()
        get_list_customer = ''' 
        SELECT nama FROM customer ORDER BY nama ASC
        '''
        get_list_expedisi = ''' 
        SELECT nama FROM expedisi ORDER BY nama ASC
        '''
        list_cari_customer = [item[0] for item in cursor.execute(get_list_customer)]
        self.addCustomerNota.addItems(list_cari_customer)
        list_cari_expedisi = [item[0] for item in cursor.execute(get_list_expedisi)]
        self.addExpedisiNota.addItems(list_cari_expedisi)
        
        self.handle_buttons()

    
    def handle_buttons(self):
        self.pushAddItemNota.clicked.connect(self.add_new_row)
    
    def add_new_row(self):
        dialog = AddCariNota(self)
        dialog.exec()
        
    def add_row_to_nota(self,item):
        print(item)
        row_count = self.tableTambahNota.rowCount()+1
        self.tableTambahNota.setRowCount(row_count)
        #delegate = ReadOnlyDelegate(self)
        self.tableTambahNota.setItem(row_count-1,0,QTableWidgetItem(str(item)))
        #self.tableTambahNota.setItemDelegateForColumn(0, delegate)
        self.btn_delete = QtWidgets.QPushButton('Delete')
        self.btn_delete.clicked.connect(self.add_delete_button)
        self.tableTambahNota.setCellWidget(row_count-1,3,self.btn_delete)
            
    def add_delete_button(self):
        button = self.sender()
        if button:
            row = self.tableTambahNota.indexAt(button.pos()).row()
            self.tableTambahNota.removeRow(row)
            print("success press here")
        

class AddCariNota(QDialog, NOTA_CARI_BARANG_CLASS):
    
    def __init__(self, parent=Main):
        super(AddCariNota,self).__init__(parent) 
        self.main_ui = Main(self)
        self.setupUi(self)
        db=sqlite3.connect("rekankita.db")
        cursor=db.cursor()
        command = '''SELECT nama FROM barang'''
        result = cursor.execute(command)
        self.tableCariBarangNota.setRowCount(0)
        for row_number, row_data in enumerate(result, start=0):
            self.tableCariBarangNota.insertRow(row_number)
            for column_number, data in enumerate(row_data):
                self.tableCariBarangNota.setItem(row_number, column_number, QTableWidgetItem(str(data)))
                self.btn_add = QtWidgets.QPushButton('Tambah')
                self.btn_add.clicked.connect(self.return_data)
                self.tableCariBarangNota.setCellWidget(row_number,1,self.btn_add)

        horizontalHeader = self.tableCariBarangNota.horizontalHeader()
        horizontalHeader.setSectionResizeMode(0, QtWidgets.QHeaderView.ResizeToContents)                  

        
        self.handle_buttons()
    
    def handle_buttons(self):
        self.buttonCariBarangNota.clicked.connect(self.get_master_barang)
        
    def get_master_barang(self):
        db=sqlite3.connect("rekankita.db")
        cursor=db.cursor()
        cari_barang = str.upper(self.textCariBarangNota.text())
        command = '''SELECT nama FROM barang WHERE nama like ? '''
        result = cursor.execute(command,  ('%'+cari_barang+'%',))
        #NEED TO GET ALL DATA TO TABLE THE
        self.tableCariBarangNota.setRowCount(0)
        for row_number, row_data in enumerate(result, start=0):
            self.tableCariBarangNota.insertRow(row_number)
            for column_number, data in enumerate(row_data):
                self.tableCariBarangNota.setItem(row_number, column_number, QTableWidgetItem(str(data)))
                self.btn_add = QtWidgets.QPushButton('Tambah')
                self.btn_add.clicked.connect(self.return_data)
                self.tableCariBarangNota.setCellWidget(row_number,1,self.btn_add)
        
        horizontalHeader = self.tableCariBarangNota.horizontalHeader()
        horizontalHeader.setSectionResizeMode(0, QtWidgets.QHeaderView.ResizeToContents)    

    def return_data(self, item):
        button = self.sender()
        index = self.tableCariBarangNota.indexAt(button.pos())
        row = self.tableCariBarangNota.indexAt(button.pos()).row()
        column =  self.tableCariBarangNota.indexAt(button.pos()).column()
        self.item = self.tableCariBarangNota.item(row,column-1).text()
        self.main_ui.add_row_to_nota(self.item)

def main():
    app=QApplication(sys.argv)
    window=Main()
    window.show()
    app.exec()
    
if __name__ == '__main__':
    main()

现在问题出在 Main 类中,其中 Add_ROW_TO_NOTA 没有向表中添加新行。但如果我将代码移至 ADD_NEW_ROW 并直接从主用户界面单击,它将起作用

有人能告诉我为什么我的代码不起作用吗?谢谢

python pyqt pyqt5
© www.soinside.com 2019 - 2024. All rights reserved.