Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

不能在窗口中正常添加动态部件? #202

Open
UF4OVER opened this issue Dec 10, 2024 · 11 comments
Open

不能在窗口中正常添加动态部件? #202

UF4OVER opened this issue Dec 10, 2024 · 11 comments

Comments

@UF4OVER
Copy link

UF4OVER commented Dec 10, 2024

代码如下,应该是没有问题的

import sys
from PyQt5.QtCore import Qt
from PyQt5.QtWidgets import QDesktopWidget, QApplication
from siui.components import SiLineEditWithItemName, SiDenseVContainer, SiOptionCardPlane, SiDenseHContainer
from siui.components.button import SiSwitchRefactor, SiPushButtonRefactor
from siui.components.option_card import SiOptionCardLinear
from siui.components.page import SiPage
from siui.components.spinbox.spinbox import SiIntSpinBox
from siui.components.titled_widget_group import SiTitledWidgetGroup
from siui.components.widgets import (
    SiLabel,
    SiLongPressButton,
)
from siui.core import Si, SiColor, SiGlobal
from siui.templates.application.application import SiliconApplication

class Label(SiLabel):
    def __init__(self, parent, text):
        super().__init__(parent)

        self.setSiliconWidgetFlag(Si.AdjustSizeOnTextChanged)
        self.setAlignment(Qt.AlignCenter)
        self.setFixedHeight(32)
        self.setText(text)
        self.adjustSize()
        self.resize(self.width() + 24, self.height())

    def reloadStyleSheet(self):
        self.setStyleSheet(f"color: {self.getColor(SiColor.TEXT_B)};")
class Autoexcal(SiPage):
    def __init__(self, *args, **kwargs):
        super().__init__(*args, **kwargs)

        self.setPadding(64)
        self.setScrollMaximumWidth(1000)
        self.setScrollAlignment(Qt.AlignLeft)
        self.setTitle("AUTOEXCAL")

        # 创建控件组
        self.titled_widgets_group = SiTitledWidgetGroup(self)
        self.titled_widgets_group.setSiliconWidgetFlag(Si.EnableAnimationSignals)
        self.setup_set_widgets()
        self.setup_function_widgets()

        # 添加页脚的空白以增加美观性
        self.titled_widgets_group.addPlaceholder(64)
        # 设置控件组为页面对象
        self.setAttachment(self.titled_widgets_group)

    def setup_set_widgets(self):
        # 密堆积容器
        with self.titled_widgets_group as group:
            group.addTitle("设置")
            choose_boswer_btu = SiLongPressButton(self)
            choose_boswer_btu.resize(128, 32)
            choose_boswer_btu.setHint("长按选择文件夹")
            choose_boswer_btu.attachment().setText("选择文件夹")

            choose_boswer_sw = SiSwitchRefactor(self)
            choose_boswer_sw.toggled.connect(lambda checked: choose_boswer_btu.setEnabled(checked))

            self.boswer_filter = SiOptionCardLinear(self)
            self.boswer_filter.setTitle("浏览器所在文件夹", "启用以自定义选择浏览器")
            self.boswer_filter.load(SiGlobal.siui.iconpack.get("ic_fluent_folder_add_filled"))
            self.boswer_filter.addWidget(choose_boswer_sw)
            self.boswer_filter.addWidget(choose_boswer_btu)

            self.port_int_spin_box = SiIntSpinBox(self)
            self.port_int_spin_box.resize(128, 32)
            self.port_int_spin_box.setMaximum(65535)
            self.port_int_spin_box.setMinimum(1024)
            self.port_int_spin_box.setValue(9002)

            choose_port_sw = SiSwitchRefactor(self)
            choose_port_sw.toggled.connect(lambda checked: self.port_int_spin_box.setEnabled(checked))

            self.choose_port_card = SiOptionCardLinear(self)
            self.choose_port_card.setTitle("端口号", "启用以自定义端口号")
            self.choose_port_card.load(SiGlobal.siui.iconpack.get("ic_fluent_plug_connected_filled"))
            self.choose_port_card.addWidget(choose_port_sw)
            self.choose_port_card.addWidget(self.port_int_spin_box)
        group.addWidget(self.boswer_filter)
        group.addWidget(self.choose_port_card)

    def setup_function_widgets(self):
        with self.titled_widgets_group as group:
            choose_switch = SiSwitchRefactor(self)
            add_weights_btu = SiPushButtonRefactor(self)
            add_weights_btu.setText("添加数据流")
            add_weights_btu.resize(128, 32)

            remove_weights_btu = SiPushButtonRefactor(self)
            remove_weights_btu.setText("移除数据流")
            remove_weights_btu.resize(128, 32)
            self.data_stream_list = []
            self.data_stream_num = 1

            self.data_stream_container = SiDenseHContainer(self)
            self.data_stream_container.setFixedHeight(320)
            self.data_stream_container.setFixedWidth(320)
            self.data_stream_container_v1 = SiDenseVContainer(self)
            self.data_stream_container_v1.setFixedWidth(160)
            self.data_stream_container_v2 = SiDenseVContainer(self)
            self.data_stream_container_v2.setFixedWidth(160)

            self.data_stream_container.addWidget(self.data_stream_container_v1)
            self.data_stream_container.addWidget(self.data_stream_container_v2)

            def add_weights_btu_clicked():
                self.data_stream_num += 1
                print(self.data_stream_num)

                data_stream1 = SiLineEditWithItemName(self)
                data_stream1.setName(f"数据{self.data_stream_num}起始")
                data_stream1.lineEdit().setFixedHeight(32)
                data_stream1.lineEdit().setText("(0,0)")

                self.data_stream_container_v1.addWidget(data_stream1)
                self.data_stream_container_v1.update()

                data_stream2 = SiLineEditWithItemName(self)
                data_stream2.lineEdit().setFixedHeight(32)
                data_stream2.setName(f"数据{self.data_stream_num}结束")
                data_stream2.lineEdit().setText("(0,0)")

                self.data_stream_container_v2.addWidget(data_stream2)
                self.data_stream_container_v2.update()

                self.data_stream_list.append((data_stream1, data_stream2))
                group.show()
                self.update()

            def remove_weights_btu_clicked():
                if self.data_stream_num > 1:
                    print(self.data_stream_num)
                    self.data_stream_num -= 1
                    data_stream1, data_stream2 = self.data_stream_list.pop()

                    self.data_stream_container_v1.removeWidget(data_stream1)
                    self.data_stream_container_v2.removeWidget(data_stream2)

                    data_stream1.deleteLater()
                    data_stream2.deleteLater()

                    group.show()

            add_weights_btu.clicked.connect(add_weights_btu_clicked)
            remove_weights_btu.clicked.connect(remove_weights_btu_clicked)

            info_ = Label(self, "启用以自定义添加数据")

            customize_the_input_box = SiOptionCardPlane(self)
            customize_the_input_box.setTitle("自定义输入框")
            customize_the_input_box.header().addWidget(choose_switch, "right")
            customize_the_input_box.body().addWidget(self.data_stream_container)
            customize_the_input_box.footer().addWidget(info_)
            customize_the_input_box.footer().addWidget(add_weights_btu, "right")
            customize_the_input_box.footer().addWidget(remove_weights_btu, "right")
            customize_the_input_box.footer().setFixedHeight(40)
            customize_the_input_box.body().addPlaceholder(12)
            customize_the_input_box.adjustSize()

            # group.addWidget(customize_the_input_box)

            choose_switch.toggled.connect(lambda checked: customize_the_input_box.body().setEnabled(checked))
            choose_switch.toggled.connect(lambda checked: customize_the_input_box.footer().setEnabled(checked))
            group.addWidget(customize_the_input_box)


class MySiliconApp(SiliconApplication):
    def __init__(self, *args, **kwargs):
        super().__init__(*args, **kwargs)

        screen_geo = QDesktopWidget().screenGeometry()
        self.setMinimumSize(1024, 380)
        self.resize(1366, 916)
        self.move((screen_geo.width() - self.width()) // 2, (screen_geo.height() - self.height()) // 2)
        self.layerMain().setTitle("111")
        self.setWindowTitle("111")

        self.layerMain().addPage(Autoexcal(self),
                                 icon=SiGlobal.siui.iconpack.get("ic_fluent_home_filled"),
                                 hint="test", side="top")
        self.layerMain().setPage(0)

        SiGlobal.siui.reloadAllWindowsStyleSheet()


if __name__ == "__main__":
    app = QApplication(sys.argv)
    window = MySiliconApp()
    window.show()
    sys.exit(app.exec_())
@UF4OVER
Copy link
Author

UF4OVER commented Dec 12, 2024

以上的问题有解决吗?

@ChinaIceF
Copy link
Owner

你好,请提供你的问题的最小实现,以便于我查找问题出处

@UF4OVER
Copy link
Author

UF4OVER commented Dec 13, 2024

首先,我在具名输入框中添加了getText()方法

    def getText(self):
        return self.lineEdit().text()

image

import os
import sys

from PyQt5.QtCore import Qt
from PyQt5.QtWidgets import QDesktopWidget, QApplication
from siui.components import SiLineEditWithItemName, SiOptionCardPlane, SiDenseHContainer, SiPushButton
from siui.components.button import SiSwitchRefactor
from siui.components.combobox import SiComboBox
from siui.components.page import SiPage
from siui.components.titled_widget_group import SiTitledWidgetGroup
from siui.components.widgets import (
    SiLabel,
)
from siui.core import Si, SiColor, SiGlobal
from siui.templates.application.application import SiliconApplication


class Label(SiLabel):
    def __init__(self, parent, text):
        super().__init__(parent)

        self.setSiliconWidgetFlag(Si.AdjustSizeOnTextChanged)
        self.setAlignment(Qt.AlignCenter)
        self.setFixedHeight(32)

        self.setText(text)
        self.adjustSize()
        self.resize(self.width() + 24, self.height())
        self.setVisible(True)
        self.update()


    def reloadStyleSheet(self):
        self.setStyleSheet(f"color: {self.getColor(SiColor.TEXT_B)};")


class Autoexcal(SiPage):
    def __init__(self, *args, **kwargs):
        super().__init__(*args, **kwargs)
        # -------------------------------start------------------------------------- #
        self.setPadding(64)
        self.setScrollMaximumWidth(1000)
        self.setScrollAlignment(Qt.AlignLeft)
        self.setTitle("AUTOEXCAL")
        # 创建控件组
        self.titled_widgets_group = SiTitledWidgetGroup(self)
        self.titled_widgets_group.setSiliconWidgetFlag(Si.EnableAnimationSignals)
        # -------------------------------const------------------------------------- #
        self.info_labels = []
        # -------------------------------widget------------------------------------ #
        self.setup_rules_groups()
        # -------------------------------finish------------------------------------ #
        SiGlobal.siui.reloadStyleSheetRecursively(self)
        # 添加页脚的空白以增加美观性
        self.titled_widgets_group.addPlaceholder(64)
        # 设置控件组为页面对象
        self.setAttachment(self.titled_widgets_group)

    def setup_rules_groups(self):
        rule_card_plane = SiOptionCardPlane(self)  # 创建选项卡
        rule_card_plane.setTitle("自定义规则")

        def add_rule_card_plane_body_widget():
            # 获取下拉框与具名输入框中的数据
            info_label = Label(self,
                               f"{self.get_data_for_combox}添加到-->{self.get_ele_name_for_combox}{self.ele_name_input.getText()}元素")
            print(info_label)  # 打印正常
            info_label.setVisible(True)  # 确保可视,似乎没什么用
            rule_card_plane.body().addWidget(info_label)  # 添加
            rule_card_plane.body().adjustSize()  # 更新控件大小
            rule_card_plane.body().update()
            rule_card_plane.adjustSize()
            self.info_labels.append(info_label)  # 存储标签引用
            group.adjustSize()  # 更新控件组大小
            group.update()  
        def remove_rule_card_plane_body_widget():
            if self.info_labels:
                label_to_remove = self.info_labels.pop()  # 获取并移除最后一个标签
                rule_card_plane.body().removeWidget(label_to_remove)
                label_to_remove.deleteLater()  # 删除标签实例
                rule_card_plane.body().adjustSize()
                rule_card_plane.body().update()
                rule_card_plane.adjustSize()
                group.adjustSize()  # 更新控件组大小

        with self.titled_widgets_group as group:
            group.addTitle("规则")
            self.rule_card_plane_h = SiDenseHContainer(self)

            self.custom_rule_tu = SiSwitchRefactor(self)
            self.custom_rule_tu.toggled.connect(lambda: rule_card_plane.body().setEnabled(False))
            self.custom_rule_tu.toggled.connect(lambda: rule_card_plane.footer().setEnabled(False))

            self.choose_data_flu = SiComboBox(self)
            self.choose_data_flu.resize(128, 32)
            self.choose_data_flu.addOption("数据1", value="数据1")
            self.choose_data_flu.addOption("数据2", value="数据2")
            self.choose_data_flu.addOption("数据3", value="数据3")
            self.choose_data_flu.menu().setShowIcon(False)
            self.choose_data_flu.menu().setIndex(0)
            self.choose_data_flu.menu().valueChanged.connect(self.get_ele_name_for_combox)

            self.choose_ele = SiComboBox(self)
            self.choose_ele.resize(128, 32)
            self.choose_ele.addOption("@id=", value="@id=")
            self.choose_ele.addOption("@tag()=", value="@tag()=")
            self.choose_ele.addOption("@text()=", value="@text()=")
            self.choose_ele.menu().setShowIcon(False)
            self.choose_ele.menu().setIndex(0)
            self.choose_data_flu.menu().valueChanged.connect(self.get_data_for_combox)

            self.ele_name_input = SiLineEditWithItemName(self)
            self.ele_name_input.setName("元素名称")
            self.ele_name_input.lineEdit().setText("txtpoint")
            self.ele_name_input.resize(350, 32)

            self.addrule_btu = SiPushButton(self)
            self.addrule_btu.attachment().setText("添加规则")
            self.addrule_btu.setFixedSize(128, 32)
            self.addrule_btu.clicked.connect(add_rule_card_plane_body_widget)

            self.remove_rule_btu = SiPushButton(self)
            self.remove_rule_btu.attachment().setText("删除规则")
            self.remove_rule_btu.setFixedSize(128, 32)
            self.remove_rule_btu.clicked.connect(remove_rule_card_plane_body_widget)

            self.rule_card_plane_h.addWidget(self.choose_data_flu)
            self.rule_card_plane_h.addWidget(Label(self, "定义到---->"))
            self.rule_card_plane_h.addWidget(self.choose_ele)
            self.rule_card_plane_h.addWidget(self.ele_name_input)

            info_ = Label(self, "元素默认后缀自增")

            rule_card_plane.header().addWidget(self.custom_rule_tu, "right")
            rule_card_plane.body().addWidget(self.rule_card_plane_h)
            rule_card_plane.footer().addWidget(info_)
            rule_card_plane.footer().addWidget(self.addrule_btu, "right")
            rule_card_plane.footer().addWidget(self.remove_rule_btu, "right")
            rule_card_plane.footer().setFixedHeight(40)
            rule_card_plane.body().addPlaceholder(12)
            rule_card_plane.adjustSize()


            group.addWidget(rule_card_plane)

    def get_data_for_combox(self, data_name):  # 获取下拉框数据
        self.data_for_combox = data_name

    def get_ele_name_for_combox(self, data_name):  # 获取下拉框数据
        self.ele_name_for_combox = data_name


class MySiliconApp(SiliconApplication):
    def __init__(self, *args, **kwargs):
        super().__init__(*args, **kwargs)

        screen_geo = QDesktopWidget().screenGeometry()
        self.setMinimumSize(1024, 380)
        self.resize(1366, 916)
        self.move((screen_geo.width() - self.width()) // 2, (screen_geo.height() - self.height()) // 2)

        self.layerMain().addPage(Autoexcal(self),
                                 icon=SiGlobal.siui.iconpack.get("ic_fluent_home_filled"),
                                 hint="test", side="top")
        self.layerMain().setPage(0)

        SiGlobal.siui.reloadAllWindowsStyleSheet()


if __name__ == "__main__":
    app = QApplication(sys.argv)
    window = MySiliconApp()
    window.show()
    sys.exit(app.exec_())

@UF4OVER
Copy link
Author

UF4OVER commented Dec 13, 2024

但是在运行的时候是这样的,容器的大小更新是没有问题的

GIF 2024-12-13 10-21-59

@UF4OVER
Copy link
Author

UF4OVER commented Dec 14, 2024

有解决吗?

@ChinaIceF
Copy link
Owner

应该是没有重载样式表。新创建的控件需要运行reloadStyleSheet()方法才能正常显示。

不过在目前的重构方案中,正在重新设计动态加载控件的逻辑,以后就无需调用这个方法就可以正常动态创建和删除控件了。

@ChinaIceF
Copy link
Owner

以前这个逻辑太唐了,所以以后会弃用这个方法。

@UF4OVER
Copy link
Author

UF4OVER commented Dec 14, 2024

感谢,但事实上是重载了也没有显示
image

@Tzeross
Copy link

Tzeross commented Dec 15, 2024

下拉框组件也是这样的问题,SiComboBox(self) 硬编码addOption没问题,动态addOption不显示文字。
另外请教Options怎么clear?

@F0rw4rdc
Copy link

调用一下控件实例的show方法

@UF4OVER
Copy link
Author

UF4OVER commented Dec 22, 2024

即便是调用了show方法,也并没有作用

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants