Stumbled on a giant cow artificial intelligence course, could not help but share to everyone. Tutorial is not only a zero-based, user-friendly, and very humorous, like watching a fiction! I think too much bad, so to others. Tutorial links: https://www.cbedai.net/qtlyx
Sometimes we want to choose the drop-down list in the cell QTableWidget's like this excel inside. So how to set it? How to achieve read and write it? Especially in pyqt, in a c ++, the Internet has been a lot of examples.
1, write
for row in range(len(index_list)):
for col in range(len(col_list)):
if row in [2, 3, 4]:
table.setItem(row, col, QTableWidgetItem(
str(df.loc[df.index[row], df.columns[col]])))
elif row == 0:
comBox_direction = QComboBox()
comBox_direction.addItems(["买", "卖"])
comBox_direction.setStyleSheet("QComboBox{margin:3px};")
if df.loc[df.index[row], df.columns[col]] == "买":
comBox_direction.setCurrentIndex(0)
elif df.loc[df.index[row], df.columns[col]] == "卖":
comBox_direction.setCurrentIndex(1)
table.setCellWidget(row, col, comBox_direction)
elif row == 1:
comBox_call_put = QComboBox()
comBox_call_put.addItems(["看涨", "看跌"])
comBox_call_put.setStyleSheet("QComboBox{margin:3px};")
if df.loc[df.index[row], df.columns[col]] == "看涨":
comBox_call_put.setCurrentIndex(0)
elif df.loc[df.index[row], df.columns[col]] == "看跌":
comBox_call_put.setCurrentIndex(1)
table.setCellWidget(row, col, comBox_call_put)
Let's look at the code above. This code is used to refresh the QtableWidget, and the value of the table consistent dataframe. General assignment is simple:
table.setItem(row, col, QTableWidgetItem(
str(df.loc[df.index[row], df.columns[col]])))
So if you want to assign to a QComboBox it? It is also very simple, first instantiate a QComboBox objects:
comBox_direction = QComboBox()
comBox_direction.addItems(["买", "卖"])
comBox_direction.setStyleSheet("QComboBox{margin:3px};")
Then, bound to the table:
table.setCellWidget(row, col, comBox_direction)
Let's look at the code above. This code is used to refresh the QtableWidget, and the value of the table consistent dataframe. General assignment is simple:
2, Reading
So, when we want to read the table in QComBox, how to do it?
combox_context_llist = [table.cellWidget(i, col).currentText() for i in range(0, 1)]
When we get content combox time, and that is the time to write, in turn, use cellWidget, rather than item.
Well, ending with a python students can also achieve a combination of a table and combox.