Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. Qt for Python
  4. how to save specific columns from tablewidget?
Forum Updated to NodeBB v4.3 + New Features

how to save specific columns from tablewidget?

Scheduled Pinned Locked Moved Unsolved Qt for Python
3 Posts 2 Posters 522 Views
  • 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.
  • B Offline
    B Offline
    badr
    wrote on last edited by
    #1

    I am using pyqt5...I have 5 columns in tablewidget with data . I want to save specific two column from tablewidget . how can do that?

    B 1 Reply Last reply
    0
    • B badr

      I am using pyqt5...I have 5 columns in tablewidget with data . I want to save specific two column from tablewidget . how can do that?

      B Offline
      B Offline
      badr
      wrote on last edited by
      #2

      @badr
      this my code:
      def save(self):

          filename = QFileDialog.getSaveFilename(self,'Save File',os.getenv('Home'))
          with open(filename[0],'w') as f:
              item = self.tableWidget.itemAt(self.tableWidget.currentRow(), 1)
              f.write(item)
      
      1 Reply Last reply
      0
      • DenniD Offline
        DenniD Offline
        Denni
        wrote on last edited by
        #3

        Okay using the snippet you have given I believe this might help you with what you are trying to do. The part that comes before the saveData function is assumed to have been done somewhere else within your program but is essential for having the saveData function work properly. Now I used a QTreeView as opposed to a QTreeWidget but they are similar, however, you might have to tweak things a little if you need to use that QTreeWidget

            tableWdgt = QTreeView()
            model = QStandardItemModel(0, cols)
            tableWdgt.setModel(self.model)
        
            def saveData(self):
                filename = QFileDialog.getSaveFilename(self,'Save File',os.getenv('Home'))
        
                rows = self.tableWdgt.model.rowCount()
                cols = self.tableWdgt.model.columnCount()
                with open(filename[0],'w') as filOut:
                    for row in range(rows):
                        for col in range(cols):
                            if col in (0, 3):   #grabs 1st and 4th columns
                                cellValue = self.tableWdgt.model.item(row, col).text()
                                filOut.write(cellValue)
        

        madness... is like gravity, all takes is a little... push -- like from an unsolvable bug

        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