Beispiel #1
0
    def _slot_context_menu(self, point):
        context_menu = MMenu(parent=self)
        logical_column = self.logicalIndexAt(point)
        model = utils.real_model(self.model())
        if logical_column >= 0 and model.header_list[logical_column].get('checkable', False):
            action_select_all = context_menu.addAction(self.tr('Select All'))
            action_select_none = context_menu.addAction(self.tr('Select None'))
            action_select_invert = context_menu.addAction(self.tr('Select Invert'))
            self.connect(action_select_all, SIGNAL('triggered()'),
                         functools.partial(self._slot_set_select, logical_column, Qt.Checked))
            self.connect(action_select_none, SIGNAL('triggered()'),
                         functools.partial(self._slot_set_select, logical_column, Qt.Unchecked))
            self.connect(action_select_invert, SIGNAL('triggered()'),
                         functools.partial(self._slot_set_select, logical_column, None))
            context_menu.addSeparator()

        fit_action = context_menu.addAction(self.tr('Fit Size'))
        fit_action.triggered.connect(functools.partial(self._slot_set_resize_mode, True))
        context_menu.addSeparator()
        for column in range(self.count()):
            action = context_menu.addAction(model.headerData(column, Qt.Horizontal, Qt.DisplayRole))
            action.setCheckable(True)
            action.setChecked(not self.isSectionHidden(column))
            action.toggled.connect(functools.partial(self._slot_set_section_visible, column))
        context_menu.exec_(QCursor.pos() + QPoint(10, 10))
Beispiel #2
0
 def _slot_context_menu(self, point):
     context_menu = MMenu(parent=self)
     action_select_all = context_menu.addAction('Select All')
     action_select_none = context_menu.addAction('Select None')
     action_select_invert = context_menu.addAction('Select Invert')
     action_select_all.triggered.connect(functools.partial(self._slot_set_select, True))
     action_select_none.triggered.connect(functools.partial(self._slot_set_select, False))
     action_select_invert.triggered.connect(functools.partial(self._slot_set_select, None))
     context_menu.exec_(QCursor.pos() + QPoint(10, 10))
    def _init_ui(self):
        self.register_field("button1_selected", "北京")
        self.register_field("button1_selected_text",
                            lambda: self.field("button1_selected"))
        button1 = MPushButton(text="Normal Menu")
        menu1 = MMenu(parent=self)
        menu1.set_data(["北京", "上海", "广州", "深圳"])
        button1.setMenu(menu1)
        button1.clicked.connect(button1.showMenu)
        label1 = MLabel()

        self.bind("button1_selected",
                  menu1,
                  "value",
                  signal="sig_value_changed")
        self.bind("button1_selected_text", label1, "text")

        self.register_field("button2_selected", ["北京"])
        self.register_field("button2_selected_text",
                            lambda: ", ".join(self.field("button2_selected")))
        button2 = MPushButton(text="Multi Select Menu")
        menu2 = MMenu(exclusive=False, parent=self)
        menu2.set_data(["北京", "上海", "广州", "深圳"])
        button2.setMenu(menu2)
        button2.clicked.connect(button2.showMenu)
        label2 = MLabel()
        self.bind("button2_selected",
                  menu2,
                  "value",
                  signal="sig_value_changed")
        self.bind("button2_selected_text", label2, "text")

        self.register_field("button3_selected", "")
        self.register_field("button3_selected_text",
                            lambda: self.field("button3_selected"))
        button3 = MPushButton(text="回调函数获取选项")
        menu3 = MMenu(parent=self)
        menu3.set_load_callback(lambda: ["北京", "上海", "广州", "深圳"])
        button3.setMenu(menu3)
        button3.clicked.connect(button2.showMenu)
        label3 = MLabel()
        self.bind("button3_selected",
                  menu3,
                  "value",
                  signal="sig_value_changed")
        self.bind("button3_selected_text", label3, "text")

        self.register_field("button4_selected", "")
        self.register_field("button4_selected_text",
                            lambda: " / ".join(self.field("button4_selected")))
        button4 = MPushButton(text="级联选择")
        menu4 = MMenu(cascader=True, parent=self)
        menu4.set_data([
            "北京/故宫", "北京/天坛", "北京/王府井", "江苏/南京/夫子庙", "江苏/苏州/拙政园", "江苏/苏州/狮子林"
        ])
        button4.setMenu(menu4)
        button4.clicked.connect(button4.showMenu)
        label4 = MLabel()
        self.bind("button4_selected",
                  menu4,
                  "value",
                  signal="sig_value_changed")
        self.bind("button4_selected_text", label4, "text")

        sub_lay1 = QtWidgets.QHBoxLayout()
        sub_lay1.addWidget(button1)
        sub_lay1.addWidget(label1)
        sub_lay2 = QtWidgets.QHBoxLayout()
        sub_lay2.addWidget(button2)
        sub_lay2.addWidget(label2)
        sub_lay3 = QtWidgets.QHBoxLayout()
        sub_lay3.addWidget(button3)
        sub_lay3.addWidget(label3)
        sub_lay4 = QtWidgets.QHBoxLayout()
        sub_lay4.addWidget(button4)
        sub_lay4.addWidget(label4)

        sub_lay5 = QtWidgets.QHBoxLayout()
        button = MPushButton(text="滚动菜单")
        menu = MMenu(parent=self)
        items = [
            "北京", "上海", "广州", "深圳", "北戴河", "BBC/data", "BBC/hello", "American"
        ]
        menu.set_data(items)
        menu.setProperty("max_scroll_count", 3)
        menu.setProperty("scrollable", True)
        menu.setProperty("searchable", True)
        button.setMenu(menu)
        sub_lay5.addWidget(button)

        sub_lay6 = QtWidgets.QHBoxLayout()
        button = MPushButton(text="搜索菜单")
        menu = MMenu(parent=self)
        items = ["北京", "上海", "广州", "深圳", "北戴河"]
        for item in items:
            menu.addAction(item)
        sub_menu = MMenu(parent=self, title="BBC")
        for item in ["data", "hello"]:
            _menu = MMenu(parent=self, title=item)
            _menu.addAction("tencent")
            _menu.addAction("baidu")
            _menu.addAction("google")
            _menu.setProperty("searchable", True)
            sub_menu.addMenu(_menu)
        menu.addMenu(sub_menu)
        menu.setProperty("searchable", True)
        button.setMenu(menu)
        sub_lay6.addWidget(button)

        main_lay = QtWidgets.QVBoxLayout()
        main_lay.addWidget(MDivider("Select"))
        main_lay.addLayout(sub_lay1)
        main_lay.addLayout(sub_lay2)
        main_lay.addLayout(sub_lay3)
        main_lay.addWidget(MDivider("级联选择"))
        main_lay.addLayout(sub_lay4)
        main_lay.addLayout(sub_lay5)
        main_lay.addLayout(sub_lay6)
        main_lay.addStretch()
        self.setLayout(main_lay)