简体   繁体   中英

error in removing item in qlayout pyqt4 python

The self.ui.verticalLayout.addWidget(MainWindow(self)) is working, But i receive an error when trying to remove the widget.

TypeError: QLayout.removeWidget(QWidget): argument 1 has unexpected type 'PyQt4.QtCore.pyqtWrapperType'

Here are the sample codes:

I have this separate .py file to create widget with qtableview

class MyWindow(QWidget):
    pcobject =[]
    def __init__(self, *args):
        QWidget.__init__(self, *args)
        layout = QVBoxLayout(self)
        self.tableview = QTableView()
        layout.addWidget(self.tableview)
........

And separate .py with vertical layout to add MyWindow Class.

-Edited

from tableview import MyWindow

class QTEST(QtGui.QMainWindow):
    def __init__(self, parent=None):
        QtGui.QWidget.__init__(self, parent)
        self.table = MyWindow
        self.ui = Ui_MainWindow()
        self.ui.setupUi(self)

        self.ui.verticalLayout.addWidget(self.table))
        self.ui.gridLayout.addLayout(self.ui.verticalLayout, 1, 0, 1, 1)
        self.connect(self.ui.pushButton_15, QtCore.SIGNAL("clicked()"), self.table_view )

    def table_view(self):

        #import sip

        self.ui.verticalLayout.removeItem(self.table)

        #self.table.setParent(None)
        #sip.delete(self.table)
        #self.table = None

if __name__ == "__main__":
    app = QtGui.QApplication(sys.argv)
    myapp = QTEST() 
    myapp.show()
    sys.exit(app.exec_())

QTEST.table is set to MyWindow class, not instance. You need to add parenthesis: self.table = MyWindow()

For reference, PyQt4.QtCore.pyqtWrapperType is the base type of all PyQt4 classes, so if you see this error it usually means you're doing something with a class instead of an instance.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM