首先,下面贴上designer处理的界面文件(转换成py后的):
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
|
# -*- coding: utf-8 -*-
# form implementation generated from reading ui file 'tabwidget.ui'
#
# created by: pyqt5 ui code generator 5.12.1
#
# warning! all changes made in this file will be lost!
from pyqt5 import qtcore, qtgui, qtwidgets
class ui_mainwindow( object ):
def setupui( self , mainwindow):
mainwindow.setobjectname( "mainwindow" )
mainwindow.resize( 800 , 600 )
self .centralwidget = qtwidgets.qwidget(mainwindow)
self .centralwidget.setobjectname( "centralwidget" )
self .tabwidget = qtwidgets.qtabwidget( self .centralwidget)
self .tabwidget.setgeometry(qtcore.qrect( 110 , 60 , 521 , 411 ))
self .tabwidget.setobjectname( "tabwidget" )
self .tab = qtwidgets.qwidget()
self .tab.setobjectname( "tab" )
self .tabwidget.addtab( self .tab, "")
self .tab_2 = qtwidgets.qwidget()
self .tab_2.setobjectname( "tab_2" )
self .tabwidget.addtab( self .tab_2, "")
mainwindow.setcentralwidget( self .centralwidget)
self .menubar = qtwidgets.qmenubar(mainwindow)
self .menubar.setgeometry(qtcore.qrect( 0 , 0 , 800 , 26 ))
self .menubar.setobjectname( "menubar" )
mainwindow.setmenubar( self .menubar)
self .statusbar = qtwidgets.qstatusbar(mainwindow)
self .statusbar.setobjectname( "statusbar" )
mainwindow.setstatusbar( self .statusbar)
self .retranslateui(mainwindow)
self .tabwidget.setcurrentindex( 0 )
qtcore.qmetaobject.connectslotsbyname(mainwindow)
def retranslateui( self , mainwindow):
_translate = qtcore.qcoreapplication.translate
mainwindow.setwindowtitle(_translate( "mainwindow" , "mainwindow" ))
self .tabwidget.settabtext( self .tabwidget.indexof( self .tab), _translate( "mainwindow" , "tab 1" ))
self .tabwidget.settabtext( self .tabwidget.indexof( self .tab_2), _translate( "mainwindow" , "tab 2" ))
|
然后,开始处理这个控件:
1、如何将控件最上面的小tab变成透明的?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
|
# -*- coding: utf-8 -*-
import sys
from pyqt5.qtwidgets import qapplication, qmainwindow
from tabwidget import ui_mainwindow
class changetabcolor(ui_mainwindow, qmainwindow):
def __init__( self ):
super (changetabcolor, self ).__init__()
self .setupui( self )
self .setfixedsize( self .width(), self .height())
self .tabwidget.settabsclosable(true)
str = "qtabbar::tab{background-color:rbg(255,255,255,0);}"
self .tabwidget.setstylesheet( str )
def ui_main():
app = qapplication(sys.argv)
w = changetabcolor()
w.show()
sys.exit(app.exec_())
if __name__ = = '__main__' :
ui_main()
|
其中,下面这两句话是将控件的上面的小tab变成透明的关键。
1
2
|
str = "qtabbar::tab{background-color:rbg(255,255,255,0);}"
self .tabwidget.setstylesheet( str )
|
2、如何在选中小tab的时候突出选中的部分(改变颜色)?
还是上面的代码不变,其中str部分加上点内容,改成如下语句:
1
2
|
str = "qtabbar::tab{background-color:rbg(255,255,255,0);}" + \
"qtabbar::tab:selected{color:red;background-color:rbg(255,200,255);} "
|
3、如何处理小tab的点击事件?
在上面的代码中加入一些语句即可实现。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
|
def __init__( self ):
super (changetabcolor, self ).__init__()
self .setupui( self )
self .setfixedsize( self .width(), self .height())
self .tabwidget.settabsclosable(true)
str = "qtabbar::tab{background-color:rbg(255,255,255,0);}" + \
"qtabbar::tab:selected{color:red;background-color:rbg(255,200,255);} "
self .tabwidget.setstylesheet( str )
self .tabwidget.currentchanged.connect( self .slot_small_tab)
def slot_small_tab( self ):
if self .tabwidget.currentindex() = = 0 :
print ( '666' )
elif self .tabwidget.currentindex() = = 1 :
print ( '222' )
else :
pass
|
未完待续。。。
以上这篇对pyqt5中qtabwidget的相关操作详解就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持服务器之家。
原文链接:https://blog.csdn.net/shangxiaqiusuo1/article/details/89889451