How to populate a dataframe in a QDialog using PyQT
-
Hi,
I need to display a table in QDialog. How can I do it in Python. Here is the code I am using:
def onClick(self):
d = QDialog()
b1 = QPushButton("ok",d)
b1.move(50,50)
d.setWindowTitle("Dialog")
d.setWindowModality(Qt.ApplicationModal)
d.setWindowFlags()
d.exec_()@Piyush You asked same question here: https://forum.qt.io/topic/87363/pop-up-window-displaying-a-dataframe and got answers. Did you try to do what already was suggested?
-
@Piyush You asked same question here: https://forum.qt.io/topic/87363/pop-up-window-displaying-a-dataframe and got answers. Did you try to do what already was suggested?
-
Hi
To put a table on/in a Dialog.
You must have your own dialog. So subclass is highly relevant :)
There is no other way. Except table it self can be window. but then its not dialog like.listview example
https://www.pythoncentral.io/pyside-pyqt-tutorial-qlistview-and-qstandarditemmodel/dialog example
https://gist.github.com/justinfx/1951709 -
Hi
To put a table on/in a Dialog.
You must have your own dialog. So subclass is highly relevant :)
There is no other way. Except table it self can be window. but then its not dialog like.listview example
https://www.pythoncentral.io/pyside-pyqt-tutorial-qlistview-and-qstandarditemmodel/dialog example
https://gist.github.com/justinfx/1951709@mrjj Hi,
So this is what I am trying to do. But getting an error. "My app object has no attribute set item"self.ui.btn1.clicked.connect(self.Dialog)
def Dialog(self):
table = self.MyTable(data,5,3)
#table.setWindowFlags(table.windowFlags() | Qt.Window)
table.show()
self.table = tabledef MyTable(self,data,*args): QTableWidget.__init__(self) 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) self.setItem(m, n, newitem) self.setHorizontalHeaderLabels(horHeaders) print horHeaders,newitem
-
Hi
you must call setItem(m, n, newitem) on the QTableWidget not via Self since in this case Self seems to be app. -
Hi
you must call setItem(m, n, newitem) on the QTableWidget not via Self since in this case Self seems to be app. -
@mrjj Hi,
I tried that too but getting an error: "TypeError: setItem(self, int, int, QTableWidgetItem): first argument of unbound method must have type 'QTableWidget'" -
@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
-
@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.