PyQt給自定義的控制項新增右鍵選單

2021-10-07 04:08:30 字數 3657 閱讀 4495

# -*- coding: utf-8 -*-

# !/usr/bin/env python

import os, sys

from qt import qtcore, qtgui, qtwidgets, qtcompat

#定義自定義的控制項跟右鍵選單

class

mywid

(qtwidgets.qwidget)

:def

__init__

(self, parent=

none):

super

(mywid, self)

.__init__(parent)

self.create_ui(

)#qwidget 自帶的預設訊號與右鍵選單的customcontextmenurequested

self.setcontextmenupolicy(qtcore.qt.customcontextmenu)

#使用customcontextmenurequested 必須要設定這行**

self.customcontextmenurequested.connect(self.showmenu)

defcreate_ui

(self)

: self.verticallayout = qtwidgets.qvboxlayout(self)

self.verticallayout.setobjectname(

"verticallayout"

) self.horizontallayout = qtwidgets.qhboxlayout(

) self.horizontallayout.setobjectname(

"horizontallayout"

) self.label_fill_light_rotatey = qtwidgets.qlabel(self)

self.label_fill_light_rotatey.setminimumsize(qtcore.qsize(

200,30)

) self.label_fill_light_rotatey.setobjectname(

"label_fill_light_rotatey"

) self.horizontallayout.addwidget(self.label_fill_light_rotatey)

self.lineedit_fill_light_rotatey = qtwidgets.qlineedit(self)

self.lineedit_fill_light_rotatey.setminimumsize(qtcore.qsize(70,

30)) self.lineedit_fill_light_rotatey.setobjectname(

"lineedit_fill_light_rotatey"

) self.horizontallayout.addwidget(self.lineedit_fill_light_rotatey)

self.horizontalslider_fill_light_rotatey = qtwidgets.qslider(self)

self.horizontalslider_fill_light_rotatey.setminimumsize(qtcore.qsize(

300,0)

) self.horizontalslider_fill_light_rotatey.setorientation(qtcore.qt.horizontal)

self.horizontalslider_fill_light_rotatey.setobjectname(

"horizontalslider_fill_light_rotatey"

) self.horizontallayout.addwidget(self.horizontalslider_fill_light_rotatey)

self.verticallayout.addlayout(self.horizontallayout)

self.label_fill_light_rotatey.settext(

"fill_light_rotatey"

) self.lineedit_fill_light_rotatey.setproperty(

"objattr"

,"fill_light_rotatey"

)#自定義選單函式

defshowmenu

(self, pos)

:

self.menu = qtwidgets.qmenu(self)

action_add = qtwidgets.qaction(self.menu)

action_add.settext(

"add"

) action_del = qtwidgets.qaction(self.menu)

action_del.settext(

"delete"

) self.menu.addaction(action_add)

self.menu.addaction(action_del)

self.menu.move(qtgui.qcursor.pos())

self.menu.show(

)#主視窗

class

test_01

(qtwidgets.qmainwindow)

:def

__init__

(self, parent=

none):

super

(test_01, self)

.__init__(parent)

self.create_ui(

) self.setwindowtitle(

"tool name"

)# 視窗名字

defcreate_ui

(self)

: self.ui = self

self.ui.centralwidget = qtwidgets.qwidget(self)

self.ui.main_layout = qtwidgets.qvboxlayout(self.ui.centralwidget)

wid1 = mywid(self)

wid2 = mywid(self)

self.ui.main_layout.addwidget(wid1)

self.ui.main_layout.addwidget(wid2)

self.ui.setcentralwidget(self.ui.centralwidget)

defmain()

:try

: handle.close(

) handle.deletelater(

)except exception:

pass

handle = test_01(

) handle.show())

)if __name__ ==

"__main__"

: main(

)

python 沒有控制項 PyQt自定義控制項未顯示

我是pyqt的新手。在 我試圖在乙個類中放入乙個qtableview,這樣我就可以定義它在類中的行為,而不必將它與所有其他 混合,但是當我這樣做時,它就不會顯示出來了。在 這是我學習的準則。它是從 edit table in pyqt using qabstracttablemodel 借來的。稍微...

android自定義控制項新增自定義屬性

1 如果是自定義控制項,請在style.xml中或attrs.xml中宣告屬性 attr declare styleable 2 在theme中使用自定義的屬性,可以再多個主題中定義不同的屬性值 customize your theme here.color colorprimaryitem col...

自定義控制項新增事件

事件,委託,總是看起來有點繞,不過本文不介紹這些,只說說如何為自定義控制項新增自定義事件。本文所說的自定義控制項,非繼承其他現有microsoft控制項或者組合控制項,而是完全繼承usercontrol的東西。既然不是繼承自其他現有控制項或者控制項組合,在特定需求下,有新增自定義事件的可能性,或者對...