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 Add column names to QTableWidget present in QDialog?
Forum Updated to NodeBB v4.3 + New Features

How to Add column names to QTableWidget present in QDialog?

Scheduled Pinned Locked Moved Solved General and Desktop
5 Posts 3 Posters 17.3k 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.
  • P Offline
    P Offline
    Piyush
    wrote on last edited by
    #1

    Hi,

    I am unable to add new column names to QTableWidget. I am using setHorizontalHeaderLabels function still unable to add names. Here is the snippet:

    def Dialog(self):
         table = self.MyTable(Big21,3,1)
         table.show()
         # this prevents the garbage collector
                     # from deleting the new table
         self.table = table
             
    def MyTable(self,data,*args):
         QTableWidget.__init__(self)
         self.data = data
         columns = ['A','B']
         tableWidget = QTableWidget()
         tableWidget.setHorizontalHeaderLabels(columns)
         tableWidget.setRowCount(len(data))
         tableWidget.setColumnCount(len(data.columns))
         for i in range (0,len(data)):
             for j in range (0,len(data.columns)):
                 item1 = str(data.iloc[i,j])
                 tableWidget.setItem(i, j,  QTableWidgetItem(item1))
         return tableWidget
    
    1 Reply Last reply
    0
    • mrjjM Offline
      mrjjM Offline
      mrjj
      Lifetime Qt Champion
      wrote on last edited by mrjj
      #2

      HI
      you have to increase col count
      tableWidget.setColumnCount
      to allow for one more :)

      https://wiki.qt.io/How_to_Use_QTableWidget

      P 1 Reply Last reply
      3
      • mrjjM mrjj

        HI
        you have to increase col count
        tableWidget.setColumnCount
        to allow for one more :)

        https://wiki.qt.io/How_to_Use_QTableWidget

        P Offline
        P Offline
        Piyush
        wrote on last edited by
        #3

        @mrjj Hi,
        Actually the code works if I move the line tableWidget.setHorizontalHeaderLabels(columns) after assigning columncount.

        mrjjM 1 Reply Last reply
        0
        • P Piyush

          @mrjj Hi,
          Actually the code works if I move the line tableWidget.setHorizontalHeaderLabels(columns) after assigning columncount.

          mrjjM Offline
          mrjjM Offline
          mrjj
          Lifetime Qt Champion
          wrote on last edited by mrjj
          #4

          @Piyush
          Hi :)
          Well setHorizontalHeaderLabels also uses the col count and it must be correct.

          1 Reply Last reply
          2
          • qwasder85Q Offline
            qwasder85Q Offline
            qwasder85
            wrote on last edited by
            #5

            You can set header contents for existing columns via the "setHeaderData()" of your table's model, eg.:

            p_table->model()->setHeaderData( column_index, Qt::Horizontal, "Hello", Qt::DisplayRole );
            
            1 Reply Last reply
            0

            • Login

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