第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號安全,請及時綁定郵箱和手機(jī)立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

如何使用按鈕啟用和禁用 QHeaderView 過濾器

如何使用按鈕啟用和禁用 QHeaderView 過濾器

qq_遁去的一_1 2023-07-11 17:09:56
我想首先有一個在 QHeaderView 下面沒有過濾器的普通視圖和一個用于激活(顯示)過濾器的按鈕:單擊“過濾器”按鈕(具有屬性“button.setCheckable(True)”)后,我想得到:注意:過濾器按鈕被按下?,F(xiàn)在,當(dāng)我再次單擊“過濾器”按鈕(取消按下它)時,我希望 QHeaderView 下面帶有過濾器的行消失:我該怎么做,換句話說,如何讓 QTableView 的 QHeaderView 成為舊的普通視圖?在應(yīng)用程序中擁有這種功能將非常方便,用戶可以隨時使用按鈕(或任何其他方式)切換 QHeaderView 下面的過濾器行。應(yīng)用程序的行為是在未按下按鈕并且過濾器行消失時取消視圖中的所有過濾器,但這對于這個問題并不重要。
查看完整描述

1 回答

?
絕地?zé)o雙

TA貢獻(xiàn)1946條經(jīng)驗(yàn) 獲得超4個贊

您必須創(chuàng)建一個處理編輯器可見性的屬性:


class FilterHeader(QHeaderView):

    filterActivated = pyqtSignal()


    def __init__(self, parent):

        super().__init__(Qt.Horizontal, parent)

        self._visible_editors = False

        self._editors = []

        self._padding = 4

        self.setStretchLastSection(True)

        # self.setResizeMode(QHeaderView.Stretch)

        self.setDefaultAlignment(Qt.AlignLeft | Qt.AlignVCenter)

        self.setSortIndicatorShown(False)

        self.sectionResized.connect(self.adjustPositions)

        parent.horizontalScrollBar().valueChanged.connect(self.adjustPositions)


    @property

    def visible_editors(self):

        return self._visible_editors


    @visible_editors.setter

    def visible_editors(self, is_visible):

        self._visible_editors = is_visible

        for editor in self._editors:

            editor.setVisible(self.visible_editors)

        self.updateGeometries()


    def setFilterBoxes(self, count):

        while self._editors:

            editor = self._editors.pop()

            editor.deleteLater()

        for index in range(count):

            editor = self.create_editor(self.parent(), index)

            editor.setVisible(self.visible_editors)

            self._editors.append(editor)

        self.adjustPositions()


    def create_editor(self, parent, index):

        if index == 1:  # Empty

            editor = QWidget()

        elif index == 2:  # Number filter (>|=|<)

            editor = Widget(parent)

            editor.linee.returnPressed.connect(self.filterActivated)

            editor.btn.clicked.connect(self.changebuttonsymbol)

        elif index == 3:

            editor = QComboBox(parent)

            editor.addItems(["", "Combo", "One", "Two", "Three"])

            editor.currentIndexChanged.connect(self.filterActivated)

        elif index == 4:

            editor = QPushButton(parent)

            editor.clicked.connect(self.filterActivated)

            editor.setText("Button")

        elif index == 5:

            editor = QCheckBox(parent)

            editor.clicked.connect(self.filterActivated)

            editor.setTristate(True)

            editor.setCheckState(Qt.Checked)

            editor.setText("CheckBox")

        else:

            editor = QLineEdit(parent)

            editor.setPlaceholderText("Filter")

            editor.returnPressed.connect(self.filterActivated)

        return editor


    def sizeHint(self):

        size = super().sizeHint()

        if self._editors and self.visible_editors:

            height = self._editors[0].sizeHint().height()

            size.setHeight(size.height() + height + self._padding)

        return size


    def updateGeometries(self):

        if self._editors and self.visible_editors:

            height = self._editors[0].sizeHint().height()

            self.setViewportMargins(0, 0, 0, height + self._padding)

        else:

            self.setViewportMargins(0, 0, 0, 0)

        super().updateGeometries()

        self.adjustPositions()


    def adjustPositions(self):

        for index, editor in enumerate(self._editors):

            if not isinstance(editor, QWidget):

                continue

            height = editor.sizeHint().height()

            compensate_y = 0

            compensate_x = 0

            if type(editor) is QComboBox:

                compensate_y = +2

            elif type(editor) in (QWidget, Widget):

                compensate_y = -1

            elif type(editor) is QPushButton:

                compensate_y = -1

            elif type(editor) is QCheckBox:

                compensate_y = 4

                compensate_x = 4

            editor.move(

                self.sectionPosition(index) - self.offset() + 1 + compensate_x,

                height + (self._padding // 2) + 2 + compensate_y,

            )

            editor.resize(self.sectionSize(index), height)


    def filterText(self, index):

        for editor in self._editors:

            if hasattr(editor, "text") and callable(editor.text):

                return editor.text()

        return ""


    def setFilterText(self, index, text):

        for editor in self._editors:

            if hasattr(editor, "setText") and callable(editor.setText):

                editor.setText(text)


    def clearFilters(self):

        for editor in self._editors:

            editor.clear()


    def changebuttonsymbol(self):

        nbtn = self.sender()

        if nbtn.text() == "=":

            nbtn.setText(">")

        elif nbtn.text() == ">":

            nbtn.setText("<")

        else:

            nbtn.setText("=")



class Window(QWidget):

    def __init__(self):

        super(Window, self).__init__()

        self.filter_button = QPushButton("Filter")

        self.filter_button.setCheckable(True)

        self.filter_button.setChecked(True)

        self.filter_button.clicked.connect(self.on_button_clicked)

        self.view = QTableView()

        self.view.horizontalHeader().setStretchLastSection(True)


        button_layout = QHBoxLayout()

        button_layout.addStretch()

        button_layout.addWidget(self.filter_button)


        layout = QVBoxLayout(self)

        layout.addLayout(button_layout)

        layout.addWidget(self.view)


        header = FilterHeader(self.view)

        self.view.setHorizontalHeader(header)

        self.view.verticalHeader().setVisible(False)

        # model = QStandardItemModel(self.view)

        model = QStandardItemModel(5, 7, self.view)

        for i in range(5):

            for j in range(7):

                item = QStandardItem(str(i + j))

                model.setItem(i, j, item)

        model.setHorizontalHeaderLabels("One Two Three Four Five Six Seven".split())

        self.view.setModel(model)

        header.setFilterBoxes(model.columnCount())

        header.filterActivated.connect(self.handleFilterActivated)


        self.on_button_clicked()


    def handleFilterActivated(self):

        header = self.view.horizontalHeader()

        for index in range(header.count()):

            if index != 4:

                print(index, header.filterText(index))

            else:

                print("Button")


    def on_button_clicked(self):

        self.view.horizontalHeader().visible_editors = self.filter_button.isChecked()



查看完整回答
反對 回復(fù) 2023-07-11
  • 1 回答
  • 0 關(guān)注
  • 142 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補(bǔ)貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動學(xué)習(xí)伙伴

公眾號

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號