i tried to send data from another class and add new row each time they sent the data. my current setup using qt designer for my ui and create all the logic in python
here's my code
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()
Now the problem is in class Main where Add_ROW_TO_NOTA doesn't add new row to the table. but if i move the code to ADD_NEW_ROW and clicked directly from main ui, it will works.
and some notes, when i press 'Tambah' button, it did send data to add_row_to_nota but it did not execute the setRowCount method. can somebody tell me what's wrong with my code? thanks
from Adding new row to tablewidget doesn't work
No comments:
Post a Comment