将组合框窗口小部件添加到Pyqt5中的QTableView中

时间:2019-03-05 22:49:15

标签: python pyqt pyqt5 qtableview qcombobox

我正在将组合框窗口小部件添加到QTableView列1,但是由于某些原因,组合框将不会显示。我试图将组合框窗口小部件作为替换“ combobox_insert”的每一列的每一行的第一个元素插入。另外,如果有人能为每行(例如“高”,“中”和“低”)的编辑标题提供指导,我将不胜感激。我在下面附加了代码。

from PyQt5.QtGui import *
from PyQt5.Qt import *
import sys, csv

class Window (QWidget):
    def __init__ (self):
        QWidget.__init__(self, None)
        self.setWindowTitle('Table')

class View (Window):
    def __init__ (self, parent=None):
        super(View, self).__init__()


        self.setFixedSize(850, 500)


        self.tableView = QTableView()
        self.tableView.clicked.connect(self.viewClicked)


        self.tableView.setStyleSheet("QTableView{gridline-color: black}")

        self.headers = ["▽", "High", "Medium", "Low"] 
        combo_box_options = ["Option 1","Option 2","Option 3"]
        combo = QComboBox()
        for t in combo_box_options:
                combo.addItem(t)
        tableData0 = [
                     [QCheckBox(''), "combobox_insert", "value", "norm_value"  ], [QCheckBox(''), "combobox_insert", "value", "norm_value" ],
                     [QCheckBox(''), "combobox_insert", "value", "norm_value" ],
                     ]


        self.model = MyTableModel(tableData0, self.headers)
        self.tableView.setModel(self.model)
        #self.model.setVerticalHeaderLabels(['High', 'Medium', 'Low'])


        self.selectRow = self.model.rowCount(QModelIndex())


        self.filters = "CSV files (*.csv)"


        self.fileName = None


        self.buttonNew = QPushButton('NEW', self)
        self.buttonAdd = QPushButton('add', self)
        self.buttonDell = QPushButton('Del', self)


        self.group = QButtonGroup()
        self.group.addButton(self.buttonNew)
        self.group.addButton(self.buttonAdd)
        self.group.addButton(self.buttonDell)


        self.buttonNew.clicked.connect(self.handleNew)

        self.buttonAdd.clicked.connect(self.insertRows)
        self.buttonDell.clicked.connect(self.removeRows)


        layout = QHBoxLayout()

        layout.addWidget(self.buttonNew)

        layout.addWidget(self.buttonAdd)
        layout.addWidget(self.buttonDell)


        Vlayout = QVBoxLayout()
        Vlayout.addWidget(self.tableView)
        Vlayout.addLayout(layout)


        self.setLayout(Vlayout)

    #saving processing0-> save as a csv


    def handleNew(self):
        print ("handleNew")
        self.fileName = ''

        defaultValue =[
        [QCheckBox(''), "combobox_insert", "value", "norm_value"]
        ]

        self.model = None
        self.model = MyTableModel(defaultValue, self.headers)
        print(defaultValue)
        self.tableView.setModel(self.model)

    #View insert and add line to model 1 above the selected line
    def insertRows(self, position, rows=1, index=QModelIndex()):
        print("position: %d"%position)
        print("rows: %d" % rows)
        print("rowCount: %d" % self.model.rowCount(QModelIndex()))

        position = self.selectRow
        self.model.beginInsertRows(QModelIndex(), position, position + rows - 1)
        for row in range(rows):
            self.model.list.insert(position, [QCheckBox(''), "combobox_insert", "value", "norm_value"])

        self.model.endInsertRows()
        return True

    #Delete the row at the selected position from the model
    def removeRows(self, position, rows=1, index=QModelIndex()):
        print("Removing at position: %s"%position)
        position = self.selectRow
        self.model.beginRemoveRows(QModelIndex(), position, position + rows - 1)
        self.model.list = self.model.list[:position] + self.model.list[position + rows:]
        self.model.endRemoveRows()
        return True

    def viewClicked(self, indexClicked):
        print('indexClicked() row: %s  column: %s'%(indexClicked.row(), indexClicked.column() ))
        self.selectRow = indexClicked.row()

class MyTableModel(QAbstractTableModel):

    def __init__(self, list, headers = [], parent = None):
        QAbstractTableModel.__init__(self, parent)
        self.list = list
        self.headers = headers

    def rowCount(self, parent):
        return len(self.list)

    def columnCount(self, parent):
        return len(self.list[0])

    def flags(self, index):
        row = index.row()
        column = index.column()
        if column == 0:
            return Qt.ItemIsUserCheckable | Qt.ItemIsEnabled
        else:
            return Qt.ItemIsEditable | Qt.ItemIsEnabled | Qt.ItemIsSelectable

    def data(self, index, role):

        row = index.row()
        column = index.column()

        if role == Qt.EditRole:
            return self.list[row][column]

        if role == Qt.CheckStateRole and column == 0:

            if self.list[row][column].isChecked():
                return QVariant(Qt.Checked)
            else:
                return QVariant(Qt.Unchecked)



        if role == Qt.DisplayRole:

            row = index.row()
            column = index.column()
            value = self.list[row][column]

            return value

    def setData(self, index, value, role = Qt.EditRole):
        row = index.row()
        column = index.column()

        if role == Qt.EditRole:
            self.list[row][column] = value
            self.dataChanged.emit(index, index)
            return True

        if role == Qt.CheckStateRole and column == 0:
            self.list[row][column] = QCheckBox('')
            if value == Qt.Checked:
                self.list[row][column].setChecked(True)
            else:
                self.list[row][column].setChecked(False)
            self.dataChanged.emit(index, index)
            return True
        return False


    def headerData(self, section, orientation, role):

        if role == Qt.DisplayRole:

            if orientation == Qt.Horizontal:

                if section < len(self.headers):
                    return self.headers[section]
                else:
                    return "not implemented"
            else:
                return "%d" % (section + 1)

if __name__ == '__main__':

    app = QApplication(sys.argv)
    table = View()
    table.show()
    app.exec_()

0 个答案:

没有答案