当QScrollArea入口为selected/clicked时发出什么样的信号?

What kind of signal is emitted when QScrollArea entry is selected/clicked?

我正在花时间弄清楚在以下情况下会发出什么样的信号:

基本上就是 QScrollArea 包含多个 QTableWidgets:

class ScrollArea(QtGui.QScrollArea):
    def __init__(self):
        super(ScrollArea, self).__init__()
        self.scroll_widget = QtGui.QWidget()
        self.scroll_layout = QtGui.QVBoxLayout()
        self.setVerticalScrollBarPolicy(QtCore.Qt.ScrollBarAlwaysOn)
        self.setHorizontalScrollBarPolicy(QtCore.Qt.ScrollBarAlwaysOff)
        self.setWidgetResizable(True)
        self.__create_content()
        self.setWidget(self._content_widget)
        self.scroll_layout.addWidget(self)
        self.scroll_widget.setLayout(self.scroll_layout)

    def __create_content(self):
        self._content_widget = QtGui.QWidget()
        self._content_widget_layout = QtGui.QVBoxLayout()
        self._content_widget.setLayout(self._content_widget_layout)

    def add_item(self, item):
        self._content_widget_layout.addWidget(item)

我正在为 QApplication 使用 Plastique 样式。从上图可以看出,当点击QScrollArea里面的item时,会出现蓝色边框。我想知道的是绘制边框时发出的信号是什么?我需要此信息,以便在单击按钮(左侧)时向所选 QTableWidget 添加一行。

你还可以看到每个table里面都有一个'x',当按下'x'时,QTableWidget会从QScrollArea中移除。如果前面的问题有解决方案,我还可以根据用户选择删除 QTableWidget 而不是用户单击 'x'.

要获取具有焦点的小部件,您可以使用 QApplicationfocusChanged 信号:

from PyQt4 import QtCore, QtGui


class HorizontalHeader(QtGui.QHeaderView):
    def __init__(self, parent=None):
        super(HorizontalHeader, self).__init__(QtCore.Qt.Horizontal, parent)
        self.button = QtGui.QToolButton(self, text="x")
        self.sectionResized.connect(self.handleSectionResized)

    def handleSectionResized(self):
        last_ix = self.count() - 1
        pos = QtCore.QPoint(self.sectionViewportPosition(last_ix) + self.sectionSize(last_ix) , 0)
        self.button.move(pos)

    def showEvent(self, event):
        self.handleSectionResized()
        super(HorizontalHeader, self).showEvent(event)


class TableView(QtGui.QTableView):
    def __init__(self, *args, **kwargs):
        super(TableView, self).__init__(*args, **kwargs)
        header = HorizontalHeader(self)
        header.button.clicked.connect(self.deleteLater)
        self.setHorizontalHeader(header)
        QtGui.qApp.focusChanged.connect(self.onFocusChanged)

    def onFocusChanged(self, old, new):
        if new == self:
            self.deleteLater()

if __name__ == '__main__':
    import sys
    app = QtGui.QApplication(sys.argv)
    scrollArea = QtGui.QScrollArea()
    scrollArea.setWidgetResizable(True)
    widget = QtGui.QWidget()
    scrollArea.setWidget(widget)
    lay = QtGui.QVBoxLayout(widget)

    for i in range(10):
        w = TableView()
        model = QtGui.QStandardItemModel(4, 2, w)
        w.setModel(model)
        lay.addWidget(w)

    scrollArea.show()
    sys.exit(app.exec_())