Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. How to populate a dataframe in a QDialog using PyQT
Forum Updated to NodeBB v4.3 + New Features

How to populate a dataframe in a QDialog using PyQT

Scheduled Pinned Locked Moved Unsolved General and Desktop
24 Posts 5 Posters 9.7k Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • J jsulm
    5 Feb 2018, 09:46

    @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)
    
    P Offline
    P Offline
    Piyush
    wrote on 5 Feb 2018, 09:54 last edited by
    #21

    @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)
    
    J 1 Reply Last reply 5 Feb 2018, 09:58
    0
    • P Piyush
      5 Feb 2018, 09:49

      @JonB Exactly, I want a QDialog and then put a table on it.

      J Offline
      J Offline
      jsulm
      Lifetime Qt Champion
      wrote on 5 Feb 2018, 09:54 last edited by
      #22

      @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.

      https://forum.qt.io/topic/113070/qt-code-of-conduct

      1 Reply Last reply
      1
      • P Piyush
        5 Feb 2018, 09:54

        @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)
        
        J Offline
        J Offline
        jsulm
        Lifetime Qt Champion
        wrote on 5 Feb 2018, 09:58 last edited by
        #23

        @Piyush You're not creating any rows/columns.
        See http://doc.qt.io/qt-5/qtablewidget.html

        tableWidget = new QTableWidget(this);
        tableWidget->setRowCount(10);
        tableWidget->setColumnCount(5);
        

        https://forum.qt.io/topic/113070/qt-code-of-conduct

        1 Reply Last reply
        2
        • P Piyush
          5 Feb 2018, 09:49

          @JonB Exactly, I want a QDialog and then put a table on it.

          J Online
          J Online
          JonB
          wrote on 5 Feb 2018, 10:03 last edited by JonB 2 May 2018, 10:10
          #24

          @Piyush

          OK, so I don't know what all the earlier discussion was about, but a QTableWidget is simply a QWidget 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 the QTableWidgetItems you create.

          1 Reply Last reply
          2

          21/24

          5 Feb 2018, 09:54

          • Login

          • Login or register to search.
          21 out of 24
          • First post
            21/24
            Last post
          0
          • Categories
          • Recent
          • Tags
          • Popular
          • Users
          • Groups
          • Search
          • Get Qt Extensions
          • Unsolved