def add_item(self, data_dict, index=None): """Add a item""" button = MToolButton() button.setText(data_dict.get("text")) if data_dict.get("svg"): button.svg(data_dict.get("svg")) if data_dict.get("tooltip"): button.setProperty("toolTip", data_dict.get("tooltip")) if data_dict.get("clicked"): button.clicked.connect(data_dict.get("clicked")) if data_dict.get("text"): if data_dict.get("svg") or data_dict.get("icon"): button.text_beside_icon() else: button.text_only() else: button.icon_only() if self._button_group.buttons(): separator = MLabel(self._separator).secondary() self._label_list.append(separator) self._main_layout.insertWidget(self._main_layout.count() - 1, separator) self._main_layout.insertWidget(self._main_layout.count() - 1, button) if index is None: self._button_group.addButton(button) else: self._button_group.addButton(button, index)
class MainWindow(QWidget, MFieldMixin): def __init__(self, parent=None): super(MainWindow, self).__init__(parent) self.setWindowTitle('test') self._init_ui() def _init_ui(self): self.resize(250, 350) self.button_login = MToolButton().svg('user_line.svg').text_beside_icon() self.button_login.setText('Login') self.button_login.clicked.connect(self.onclick) button_lay = QVBoxLayout() button_lay.addWidget(self.button_login) self.tree=QTreeWidget() self.tree.setColumnCount(1) self.tree.setHeaderHidden(1) self.tree.setGeometry(QRect(1,1,250,500)) self.tree.itemClicked.connect(self.testt) new_widget_1 = QTreeWidgetItem(self.tree) new_widget_1.setText(0,u'技能培训') new_widget_2 = QTreeWidgetItem(self.tree) new_widget_2.setText(0, u'企业培训') new_widget_3 = QTreeWidgetItem(self.tree) new_widget_3.setText(0, u'职业技能培训') new_widget_3_1 = QTreeWidgetItem(new_widget_3) new_widget_3_1.setText(0, u'餐饮厨艺') new_widget_3_2 = QTreeWidgetItem(new_widget_3) new_widget_3_2.setText(0, u'家庭服务') new_widget_3_3 = QTreeWidgetItem(new_widget_3) new_widget_3_3.setText(0, u'运输与物流') tree_layout = QVBoxLayout() tree_layout.addWidget(self.tree) main_lay = QVBoxLayout() main_lay.addLayout(button_lay) main_lay.addLayout(tree_layout) main_lay.addStretch() self.setLayout(main_lay) def testt(self,item): print ('aaaa',item) def onclick(self): self.login = Login() self.login.show() dayu_theme.apply(self.login)
def _init_ui(self): size_lay = QtWidgets.QVBoxLayout() sub_lay1 = QtWidgets.QHBoxLayout() sub_lay1.addWidget( MToolButton().svg("left_line.svg").icon_only().huge()) sub_lay1.addWidget( MToolButton().svg("right_line.svg").icon_only().large()) sub_lay1.addWidget(MToolButton().svg("up_line.svg").icon_only()) sub_lay1.addWidget( MToolButton().svg("up_line.svg").icon_only().small()) sub_lay1.addWidget( MToolButton().svg("down_line.svg").icon_only().tiny()) sub_lay1.addStretch() size_lay.addLayout(sub_lay1) button2 = MToolButton().svg("detail_line.svg").icon_only() button2.setEnabled(False) button7 = MToolButton().svg("trash_line.svg").icon_only() button7.setCheckable(True) state_lay = QtWidgets.QHBoxLayout() state_lay.addWidget(button2) state_lay.addWidget(button7) state_lay.addStretch() button_trash = MToolButton().svg("trash_line.svg").text_beside_icon() button_trash.setText("Delete") button_login = MToolButton().svg("user_line.svg").text_beside_icon() button_login.setText("Login") button_lay = QtWidgets.QHBoxLayout() button_lay.addWidget(button_trash) button_lay.addWidget(button_login) sub_lay2 = QtWidgets.QHBoxLayout() sub_lay2.addWidget(button2) sub_lay2.addWidget(button7) main_lay = QtWidgets.QVBoxLayout() main_lay.addWidget(MDivider("different button_size")) main_lay.addLayout(size_lay) main_lay.addWidget(MDivider("disabled & checkable")) main_lay.addLayout(state_lay) main_lay.addWidget(MDivider("type=normal")) main_lay.addLayout(button_lay) main_lay.addStretch() self.setLayout(main_lay)
def _init_ui(self): size_lay = QVBoxLayout() sub_lay1 = QHBoxLayout() sub_lay1.addWidget(MToolButton().svg('left_line.svg').icon_only()) sub_lay1.addWidget(MToolButton().svg('right_line.svg').icon_only()) sub_lay1.addWidget(MToolButton().svg('up_line.svg').icon_only()) sub_lay1.addWidget(MToolButton().svg('down_line.svg').icon_only()) sub_lay1.addStretch() size_lay.addLayout(sub_lay1) button2 = MToolButton().svg('detail_line.svg').icon_only() button2.setEnabled(False) button7 = MToolButton().svg('trash_line.svg').icon_only() button7.setCheckable(True) state_lay = QHBoxLayout() state_lay.addWidget(button2) state_lay.addWidget(button7) state_lay.addStretch() button_trash = MToolButton().svg('trash_line.svg').text_beside_icon() button_trash.setText('Delete') button_login = MToolButton().svg('user_line.svg').text_beside_icon() button_login.setText('Login') button_lay = QHBoxLayout() button_lay.addWidget(button_trash) button_lay.addWidget(button_login) sub_lay2 = QHBoxLayout() sub_lay2.addWidget(button2) sub_lay2.addWidget(button7) main_lay = QVBoxLayout() main_lay.addWidget(MDivider('different button_size')) main_lay.addLayout(size_lay) main_lay.addWidget(MDivider('disabled & checkable')) main_lay.addLayout(state_lay) main_lay.addWidget(MDivider('type=normal')) main_lay.addLayout(button_lay) main_lay.addStretch() self.setLayout(main_lay)
class MNewTag(QWidget): sig_add_tag = Signal(str) def __init__(self, text='New Tag', parent=None): super(MNewTag, self).__init__(parent) self.setAttribute(Qt.WA_StyledBackground) self._add_button = MToolButton().tiny().svg( 'add_line.svg').text_beside_icon() self._add_button.setText(text) self._add_button.clicked.connect(self._slot_show_edit) self._line_edit = MLineEdit().tiny() self._line_edit.returnPressed.connect(self._slot_return_pressed) self._line_edit.setVisible(False) self._main_lay = QGridLayout() self._main_lay.setContentsMargins(3, 3, 3, 3) self._main_lay.addWidget(self._add_button, 0, 0) self._main_lay.addWidget(self._line_edit, 0, 0) self.setLayout(self._main_lay) def set_completer(self, completer): self._line_edit.setCompleter(completer) def _slot_show_edit(self): self._line_edit.setVisible(True) self._add_button.setVisible(False) self._line_edit.setFocus(Qt.MouseFocusReason) def _slot_return_pressed(self): self._line_edit.setVisible(False) self._add_button.setVisible(True) if self._line_edit.text(): self.sig_add_tag.emit(self._line_edit.text()) self._line_edit.clear() def focusOutEvent(self, *args, **kwargs): self._line_edit.setVisible(False) self._add_button.setVisible(True) return super(MNewTag, self).focusOutEvent(*args, **kwargs)
class MNewTag(QWidget): """New Tag input component.""" sig_add_tag = Signal(str) def __init__(self, text="New Tag", parent=None): super(MNewTag, self).__init__(parent) self.setAttribute(Qt.WA_StyledBackground) self._add_button = MToolButton().text_beside_icon().small().svg( "add_line.svg") self._add_button.setText(text) self._add_button.clicked.connect(self._slot_show_edit) self._line_edit = MLineEdit().small() self._line_edit.returnPressed.connect(self._slot_return_pressed) self._line_edit.setVisible(False) self._line_edit.installEventFilter(self) self._main_lay = QGridLayout() self._main_lay.setContentsMargins(3, 3, 3, 3) self._main_lay.addWidget(self._add_button, 0, 0) self._main_lay.addWidget(self._line_edit, 0, 0) self.setLayout(self._main_lay) scale_x, _ = get_scale_factor() style = QssTemplate(""" MNewTag{ border: @border@unit dashed @border_color; } MNewTag MToolButton:hover{ border:none; } """) self.setStyleSheet( style.substitute( border_color=utils.fade_color(dayu_theme.secondary_text_color, "35%"), unit=dayu_theme.unit, border=1 * scale_x, )) def set_completer(self, completer): """Set the input completer""" self._line_edit.setCompleter(completer) def _slot_show_edit(self): self._line_edit.setVisible(True) self._add_button.setVisible(False) self._line_edit.setFocus(Qt.MouseFocusReason) def _slot_return_pressed(self): self._line_edit.setVisible(False) self._add_button.setVisible(True) if self._line_edit.text(): self.sig_add_tag.emit(self._line_edit.text()) self._line_edit.clear() self.update() def focusOutEvent(self, *args, **kwargs): """Override focusOutEvent to change the edit mode to button mode.""" self._line_edit.setVisible(False) self._add_button.setVisible(True) return super(MNewTag, self).focusOutEvent(*args, **kwargs) def eventFilter(self, widget, event): if widget is self._line_edit: if event.type() == QEvent.Type.KeyPress and event.key( ) == Qt.Key_Escape: self._line_edit.setVisible(False) self._add_button.setVisible(True) return super(MNewTag, self).eventFilter(widget, event)