从QWidget(PyQt4)调用QMainWindow中的方法/属性

时间:2014-02-22 16:48:35

标签: python pyqt4 qwidget qmainwindow

我正在尝试在QMainWindow中添加文件 - 保存对话框,但这需要从QWidget调用self.tablet [0]。 代码如下:

from PyQt4.QtGui import *
from PyQt4.QtCore import *
import csv

header1 = ['N', 'Type', 'V', 'Pg', 'Pn', 'Qn']
header2 = ['Nn', 'Nn', 'R', 'X']

class MyTable(QTableWidget):
    def __init__(self, header, *args):
        QTableWidget.__init__(self, *args)
        self.header = header
        self.setHorizontalHeaderLabels(header)
        self.setColumnWidth(0, 39)
        self.setColumnWidth(1, 40)
        self.resizeRowsToContents()
        self.setVerticalScrollBarPolicy(Qt.ScrollBarAlwaysOn)
        self.setHorizontalScrollBarPolicy(Qt.ScrollBarAlwaysOn)

class MyMainWindow(QMainWindow):
    def __init__(self, parent=None):
        super(MyMainWindow, self).__init__(parent)
        self.form_widget = MainWidget(self)
        _widget = QWidget()
        _layout = QVBoxLayout(_widget)
        _layout.addWidget(self.form_widget)
        self.setCentralWidget(_widget)

        #menu
        menubar = self.menuBar()
        file = menubar.addMenu('&File')

        exit = QAction('Exit', self)
        exit.setShortcut('Ctrl+Q')
        self.connect(exit, SIGNAL('triggered()'), SLOT('close()'))

        save = QAction('Save', self)
        save.setShortcut('Ctrl+S')
        self.connect(save, SIGNAL('triggered()'), self.Save)

        file.addAction(save)
        file.addAction(exit)

    def Save(self):
        self.rowdata = []
        path = QFileDialog.getSaveFileName(self, 'Save File', '', 'CSV(*.csv)')
        if path:
            with open(path, 'w', newline='') as stream:
                writer = csv.writer(stream, delimiter=';')
                for row in range(MainWidget().table[0].rowCount()):
                    for column in range(self.table[0].columnCount()):
                        item = self.table[0].item(row, column)
                        if item is not None and item.text() != '':
                            self.rowdata.append(float(item.text()))
                        else:
                            self.rowdata.append(0)
                    writer.writerow(self.rowdata)
                    self.rowdata = []

class MainWidget(QWidget):
    def __init__(self, parent):
        QWidget.__init__(self)

        self.setGeometry(250, 150, 400, 300)
        self.setWindowTitle("New")

        self.tab_widget = QTabWidget()
        self.tab1 = QWidget()
        self.tab2 = QWidget()

        self.tab_widget.addTab(self.tab1, "Uzli")
        self.tab_widget.addTab(self.tab2, "Vetvi")

        #Uzli on tab1 
        self.table = {}
        self.table[0] = MyTable(header1, 1, 6, self.tab1)
        self.table[0].resize(525, 430)
        self.table[0].show()

        #Vetvi on tab2 
        self.table[1] = MyTable(header2, 1, 4, self.tab2)
        self.table[1].resize(525, 430)
        self.table[1].show()

        #layers
        self.vbox = QVBoxLayout()
        self.vbox.addWidget(self.tab_widget)
        self.setLayout(self.vbox)

        self.tab1_layout = QGridLayout()
        self.tab1.setLayout(self.tab1_layout)
        self.tab1_layout.addWidget(self.table[0], 1, 0, 5, 1)

        self.tab2_layout = QGridLayout()
        self.tab2.setLayout(self.tab2_layout)
        self.tab2_layout.addWidget(self.table[1], 1, 0, 5, 1)

if __name__ == "__main__":
    import sys

    app = QApplication(sys.argv)
    widget = MyMainWindow()
    widget.resize(640, 480)
    widget.show()
    sys.exit(app.exec_())

当我尝试在此应用程序中使用“文件 - 保存”对话框时:

    for row in range(MainWidget().table[0].rowCount()): 
TypeError: __init__() missing 1 required positional argument: 'parent'

拜托,帮我找个错误。

1 个答案:

答案 0 :(得分:1)

MainWidget()尝试创建MainWidget的另一个实例,显然__init__(self, parent)需要另一个参数parent。使用您创建的原始文件self.form_widget.table[0]...