How to populate a dataframe in a QDialog using PyQT
-
@jsulm Hi,
I have used the same line of code. But still I am getting the error: "TypeError: setItem(self, int, int, QTableWidgetItem): first argument of unbound method must have type 'QTableWidget'". Here is the snippet:def Dialog(self): table = self.MyTable(data,5,3) table.show() self.table = table def MyTable(self,data,*args): self.data = data horHeaders = [] for n, key in enumerate(sorted(self.data.keys())): horHeaders.append(key) for m, item in enumerate(self.data[key]): newitem = QTableWidgetItem(item) QTableWidget.setItem(m, n, newitem) QTableWidget.setHorizontalHeaderLabels(horHeaders) print horHeaders,newitem
-
@jsulm Hi,
I have used the same line of code. But still I am getting the error: "TypeError: setItem(self, int, int, QTableWidgetItem): first argument of unbound method must have type 'QTableWidget'". Here is the snippet:def Dialog(self): table = self.MyTable(data,5,3) table.show() self.table = table def MyTable(self,data,*args): self.data = data horHeaders = [] for n, key in enumerate(sorted(self.data.keys())): horHeaders.append(key) for m, item in enumerate(self.data[key]): newitem = QTableWidgetItem(item) QTableWidget.setItem(m, n, newitem) QTableWidget.setHorizontalHeaderLabels(horHeaders) print horHeaders,newitem
@Piyush said in How to populate a dataframe in a QDialog using PyQT:
QTableWidget.setItem(m, n, newitem) QTableWidget.setHorizontalHeaderLabels(horHeaders)
The left hand side for
setItem()
/setHorizontalHeaderLabels()
must be your actual table (widget) variable. You are passing the class/typeQTableWidget
. Hence the error message.Also while I'm here, your
MyTable()
does not return anything, and does not inherit from anything either. -
@Piyush said in How to populate a dataframe in a QDialog using PyQT:
QTableWidget.setItem(m, n, newitem) QTableWidget.setHorizontalHeaderLabels(horHeaders)
The left hand side for
setItem()
/setHorizontalHeaderLabels()
must be your actual table (widget) variable. You are passing the class/typeQTableWidget
. Hence the error message.Also while I'm here, your
MyTable()
does not return anything, and does not inherit from anything either. -
@Piyush said in How to populate a dataframe in a QDialog using PyQT:
QTableWidget.setItem(m, n, newitem) QTableWidget.setHorizontalHeaderLabels(horHeaders)
The left hand side for
setItem()
/setHorizontalHeaderLabels()
must be your actual table (widget) variable. You are passing the class/typeQTableWidget
. Hence the error message.Also while I'm here, your
MyTable()
does not return anything, and does not inherit from anything either. -
-
@Piyush said in How to populate a dataframe in a QDialog using PyQT:
Could you give me the code for that? Since I am not able to get it.
In your dialog you should have an instance of QTableWidget, right?
Something like:self.tableWidget = QTableWidget()
Then it would be
self.tableWidget.setItem(m, n, newitem)
-
@Piyush
Well, (as usual for me) I don't really understand what it is you are trying to do or ask about.Are you simply wishing to create a
QDialog
and then put aQTableWidget
on it, so it's a dialog which has a table (among possibly other widgets) on it? -
@Piyush said in How to populate a dataframe in a QDialog using PyQT:
Could you give me the code for that? Since I am not able to get it.
In your dialog you should have an instance of QTableWidget, right?
Something like:self.tableWidget = QTableWidget()
Then it would be
self.tableWidget.setItem(m, n, newitem)
@jsulm I am getting empty/none table. Here is the code snippet:
def Dialog(self): table = self.MyTable(data,5,3) table.show() self.table = table def MyTable(self,data,*args): self.data = data self.tableWidget = QTableWidget() horHeaders = [] for n, key in enumerate(sorted(self.data.keys())): horHeaders.append(key) for m, item in enumerate(self.data[key]): newitem = QTableWidgetItem(item) self.tableWidget.setItem(m, n, newitem) self.tableWidget.setHorizontalHeaderLabels(horHeaders)
-
@Piyush said in How to populate a dataframe in a QDialog using PyQT:
Exactly, I want a QDialog and then put a table on it.
So, did you create a table in your dialog? It is really hard to follow you as you do not provide much information.
-
@jsulm I am getting empty/none table. Here is the code snippet:
def Dialog(self): table = self.MyTable(data,5,3) table.show() self.table = table def MyTable(self,data,*args): self.data = data self.tableWidget = QTableWidget() horHeaders = [] for n, key in enumerate(sorted(self.data.keys())): horHeaders.append(key) for m, item in enumerate(self.data[key]): newitem = QTableWidgetItem(item) self.tableWidget.setItem(m, n, newitem) self.tableWidget.setHorizontalHeaderLabels(horHeaders)
@Piyush You're not creating any rows/columns.
See http://doc.qt.io/qt-5/qtablewidget.htmltableWidget = new QTableWidget(this); tableWidget->setRowCount(10); tableWidget->setColumnCount(5);
-
OK, so I don't know what all the earlier discussion was about, but a
QTableWidget
is simply aQWidget
like any other widget, and I presume you know how to add widgets to dialogs.Your code will look like:
dlg = QDialog() tbl = QTableWidget() dlg.addWidget(tbl) tbl.setHorizontalHeaderLabels(...) tbl.setRowCount(...) tbl.setColumnCount(...) ... item = QTableWidgetItem(...) tbl.setItem(m, n, item)
If you want to create a dedicated dialog for this, you might go:
class MyDialog(QDialog): def __init__(self, parent=None): super().__init__(parent) self.tbl = QTableWidget() self.addWidget(self.tbl) self.tbl.setHorizontalHeaderLabels(...) self.tbl.setRowCount(...) self.tbl.setColumnCount(...) ... item = QTableWidgetItem(...) self.tbl.setItem(m, n, item)
That's the skeleton outline. As @jsulm says, you need to create the actual rows/columns in the table (
tbl.setRow/ColumnCount()
) to hold theQTableWidgetItem
s you create.