如何使用self.myQListWidget.itemSelectionChanged.connect()获得属于自定义QListWidgetItem的QComboBox的值?

时间:2018-07-18 07:56:52

标签: python pyqt pyqt5 qlistwidget

我有一个QListWidget,其中填充了从自定义小部件生成的项目的多个实例。此自定义窗口小部件包含QComboBox。我有它,所以当更改任何QComboBoxes时,底部的绘图都会更新,但是当用户在列表中选择特定的行时如何更新绘图?我不知道如何获取所选项目的QComboBox的当前值。

class MainWidget(QWidget):

    def showselection(self):

        # get current selection from QComboBox for selected item
        # run self.bias_graph.updateplot passing in current selection

    def __init__(self, parent):

        super().__init__(parent)

        grid = QGridLayout()

        self.bias_graph = BiasGraph(self, width=5, height=4)

        self.multiplexerList = QListWidget()

        for i in range(10):

            item = QListWidgetItem(self.multiplexerList)
            itemWidget = MultiplexerListItem(self.bias_graph, i)
            item.setSizeHint(itemWidget.sizeHint())
            self.multiplexerList.addItem(item)
            self.multiplexerList.setItemWidget(item, itemWidget)

        self.multiplexerList.itemSelectionChanged.connect(self.showselection)

        heading1 = QLabel('Multiplexer')
        heading2 = QLabel('Waveform')
        heading1.setFixedHeight(50)
        heading2.setFixedHeight(50)

        grid.addWidget(heading1, 0, 0)
        grid.addWidget(heading2, 0, 1)
        grid.addWidget(self.multiplexerList, 1, 0, 1, 2)

        bottomleft = self.bias_graph

        grid.addWidget(bottomleft, 11, 0)

        self.setLayout(grid)

        self.show()

class MultiplexerListItem(QWidget):

    def __init__(self, main_plot, i,  parent = None):

        super().__init__(parent)

        grid = QGridLayout()

        grid.addWidget(QLabel(str(i + 1)), 0, 0)

        waveselect = QComboBox()
        waveselect.addItem("A")
        waveselect.addItem("B")
        waveselect.activated[str].connect(main_plot.updateplot)
        grid.addWidget(waveselect, 0, 1)

        self.setLayout(grid)

class BiasGraph(FigureCanvas):

    def __init__(self, parent = None, width=5, height=4, dpi=100):

        fig = Figure(figsize = (width, height), dpi = dpi)
        self.axes = fig.add_subplot(111)

        FigureCanvas.__init__(self, fig)
        self.setParent(parent)

        FigureCanvas.setSizePolicy(self,
            QSizePolicy.Expanding,
            QSizePolicy.Expanding)
        FigureCanvas.updateGeometry(self)

        default = []
        A = [0, 0, -1, -2, -3, -2, -1, 0, 0]
        B = [0, 0, 1, 2, 3, 2, 1, 0, 0]
        self.data = {"A" : A, "B" : B}

        self.plot(default)

    def updateplot(self, text):

        self.plot(self.data[text])

    def plot(self, X):

        self.axes.clear()
        ax = self.figure.add_subplot(111)
        ax.plot(X, 'r-')
        ax.set_title('Bias Waveform')
        self.draw()

1 个答案:

答案 0 :(得分:0)

首先,您必须使用from PyQt5 import QtCore, QtGui, QtWidgets class TabBar(QtWidgets.QTabBar): def tabSizeHint(self, index): s = QtWidgets.QTabBar.tabSizeHint(self, index) s.transpose() return s def paintEvent(self, event): painter = QtWidgets.QStylePainter(self) opt = QtWidgets.QStyleOptionTab() for i in range(self.count()): self.initStyleOption(opt, i) painter.drawControl(QtWidgets.QStyle.CE_TabBarTabShape, opt) painter.save() s = opt.rect.size() s.transpose() r = QtCore.QRect(QtCore.QPoint(), s) r.moveCenter(opt.rect.center()) opt.rect = r c = self.tabRect(i).center() painter.translate(c) painter.rotate(90) painter.translate(-c) painter.drawControl(QtWidgets.QStyle.CE_TabBarTabLabel, opt) painter.restore() # for i in range(self.count()): # self.initStyleOption(opt, i) # c = self.tabRect(i) # c.moveLeft(35) # painter.drawControl(QtWidgets.QStyle.CE_TabBarTabShape, opt) # # painter.setPen(QColor(255, 255, 255)) # painter.drawText(c, QtCore.Qt.AlignVCenter | QtCore.Qt.TextDontClip, self.tabText(i)) # if i == 0: # painter.drawImage(QtCore.QRectF(8, 8, 20, 20), QtGui.QImage("images/logo.png")) # if i == 1: # painter.drawImage(QtCore.QRectF(8, 44, 20, 20), QtGui.QImage("images/data.png")) # if i == 2: # painter.drawImage(QtCore.QRectF(8, 82, 20, 20), QtGui.QImage("images/browse.png")) # if i == 3: # painter.drawImage(QtCore.QRectF(8, 120, 20, 20), QtGui.QImage("images/off.png")) # if i == 4: # painter.drawImage(QtCore.QRectF(8, 158, 20, 20), QtGui.QImage("images/cal.png")) # if i == 5: # painter.drawImage(QtCore.QRectF(8, 196, 20, 20), QtGui.QImage("images/fol.png")) # if i == 6: # painter.drawImage(QtCore.QRectF(8, 232, 20, 20), QtGui.QImage("images/exc.png")) # painter.end() class TabWidget(QtWidgets.QTabWidget): def __init__(self, *args, **kwargs): QtWidgets.QTabWidget.__init__(self, *args, **kwargs) self.setTabBar(TabBar(self)) self.setTabPosition(QtWidgets.QTabWidget.West) 获得选定的项目,然后使用currentItem()获得小部件,这将返回与该项目相关联的itemWidget(),但是目前很难获得MultiplexerListItem,为此,您必须成为QComboBox类的QComboBox成员。然后访问MultiplexerListItem和相应的文本。

QComboBox