Domanda

I have a problem with the creation of multiple tabs in table and a delete button. This button should delete the rows in the current table. My problem is that it only deletes rows in the last created table if I create more than one new tab. And I can't name the tables due to the fact that I don't know how many tabs I need.

import sys
from PyQt4 import QtGui, QtCore

class Fenster(QtGui.QMainWindow):

    def __init__(self, parent=None):

        QtGui.QMainWindow.__init__(self, parent)

        self.resize(300, 300)

        addButton = QtGui.QPushButton(u"Add Tab")
        self.connect(addButton, QtCore.SIGNAL("clicked()"), self.addTab)

        layout = QtGui.QVBoxLayout()
        layout.addWidget(addButton)

        self.tab_widget = QtGui.QTabWidget()
        self.tab_widget.updatesEnabled()
        widget = QtGui.QWidget()
        self.tab_widget.addTab(widget, "Tab 1")
        widget.setLayout(layout)

        self.setCentralWidget(self.tab_widget)
        self.show()


    def addTab(self):

        contents = QtGui.QWidget()

        delButton = QtGui.QPushButton(u"Del Row")
        self.connect(delButton, QtCore.SIGNAL("clicked()"), self.delRow)

        self.table = QtGui.QTableWidget(5, 2)

        layout = QtGui.QVBoxLayout()
        layout.addWidget(self.table)
        layout.addWidget(delButton)

        self.tab_widget.addTab(contents, "New Tab")

        contents.setLayout(layout)

    def delRow(self):

        self.table.setRowCount(0)        

if __name__ == '__main__':
    app = QtGui.QApplication(sys.argv)
    window = Fenster()
    window.show()
    sys.exit(app.exec_())        
È stato utile?

Soluzione

The problem is that self.table always points to the last table widget you created. The delRow method needs to know which table to delete from, so it needs a reference to that table. I would suggest the following code. Here, your slot takes an argument that points to the table you want to delete from.

def addTab(self):

    contents = QtGui.QWidget()

    table = QtGui.QTableWidget(5, 2)
    delButton = QtGui.QPushButton(u"Del Row")
    delButton.clicked.connect(lambda: self.delRow(table))


    layout = QtGui.QVBoxLayout()
    layout.addWidget(table)
    layout.addWidget(delButton)

    self.tab_widget.addTab(contents, "New Tab")

    contents.setLayout(layout)

def delRow(self, table):

    table.setRowCount(0) 

A couple of things about this code:

  • I've used the new style signal/slot method to connect the clicked signal to a slot (it is more pythonic)

  • Because the signal expects to connect to a slot that takes no arguments, I've wrapped your delRow(table) method using lambda. If you haven't come across lambda before, it is basically short hand for writing a one line function. You can read up about it in the Python docs.

Altri suggerimenti

You could track which tab is active via currentChanged() signal. Then in signal handler you set self.table.

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top