我正在尝试根据窗口框架的大小动态调整标签的大小。最初,该程序在全屏模式下打开,并采用屏幕的宽度/高度分辨率以使标签之间的间距均匀。这可以正常工作。如果我最小化窗口,它也会动态地正确调整大小,但是如果我尝试在之后最大化或调整窗口大小,它将不会动态调整。
我正在使用resizeEvent()
和pyqtSignal()
来检测何时调整窗口大小。我将此插槽与一个函数相连,以使用frameGeometry()
获取窗口的尺寸,然后设置新选项卡的宽度和高度。但是在初始调整大小之后,它似乎并没有改变。我该如何解决?
from PyQt4 import QtCore, QtGui
import base64
import sys
def exit_application():
"""Exit program event handler"""
sys.exit(1)
class HorizontalTabBar(QtGui.QTabBar):
def __init__(self, width, height, width_ratio=6, height_ratio=20, tabs=6, parent=None):
super(HorizontalTabBar, self).__init__(parent)
self.tabs = tabs
self.width_ratio = self.tabs
self.height_ratio = height_ratio
self.width = width/self.width_ratio
self.height = height/self.height_ratio
def tabSizeHint(self, index):
return QtCore.QSize(self.width, self.height)
def paintEvent(self, event):
painter = QtGui.QStylePainter(self)
tab_options = QtGui.QStyleOptionTab()
for tab in range(self.count()):
self.initStyleOption(tab_options, tab)
painter.drawControl(QtGui.QStyle.CE_TabBarTabShape, tab_options)
painter.save()
size = tab_options.rect.size()
size.transpose()
center = self.tabRect(tab).center()
painter.translate(center)
painter.rotate(0)
painter.translate(-center)
painter.drawControl(QtGui.QStyle.CE_TabBarTabLabel, tab_options);
painter.restore()
def set_new_tab_dimensions(self, width, height):
self.width = width/self.width_ratio
self.height = height/self.height_ratio
class TabWidget(QtGui.QTabWidget):
resized = QtCore.pyqtSignal()
def __init__(self, *args, **kwargs):
QtGui.QTabWidget.__init__(self)
self.horizontal_tab_bar = HorizontalTabBar(kwargs.pop('width'), kwargs.pop('height'))
self.setTabBar(self.horizontal_tab_bar)
self.setTabPosition(QtGui.QTabWidget.South)
self.setTabShape(QtGui.QTabWidget.Rounded)
def resizeEvent(self, event):
self.resized.emit()
screen_width = self.frameGeometry().width()
screen_height = self.frameGeometry().height()
print(screen_height, screen_width)
self.horizontal_tab_bar.set_new_tab_dimensions(screen_width, screen_height)
return super(TabWidget, self).resizeEvent(event)
class AdvancedTab(QtGui.QWidget):
def __init__(self, parent=None):
super(AdvancedTab, self).__init__(parent)
self.setWindowModality(QtCore.Qt.ApplicationModal)
self.setWindowFlags(QtCore.Qt.FramelessWindowHint)
self.advanced_tab_locked_init()
self.advanced_tab_unlocked_init()
self.advanced_tab_layout = QtGui.QGridLayout()
# Two layouts: locked and unlocked
# 0 - locked
# 1 - unlocked
self.advanced_tab_page_switch = QtGui.QStackedWidget()
self.advanced_tab_page_switch.addWidget(self.advanced_tab_locked)
self.advanced_tab_page_switch.addWidget(self.advanced_tab_unlocked)
self.advanced_tab_page_switch.setCurrentIndex(1)
self.advanced_tab_layout.addWidget(self.advanced_tab_page_switch,0,0,alignment=QtCore.Qt.AlignLeft)
def advanced_tab_locked_init(self):
"""Advanced tab lock screen"""
self.advanced_tab_locked = QtGui.QWidget()
self.advanced_tab_locked_layout = QtGui.QVBoxLayout()
self.test = QtGui.QLabel('sdds')
self.advanced_tab_locked_layout.addWidget(self.test)
self.advanced_tab_locked.setLayout(self.advanced_tab_locked_layout)
def advanced_tab_unlocked_init(self):
self.advanced_tab_unlocked = QtGui.QWidget()
self.advanced_tab_unlocked_layout = QtGui.QGridLayout()
for i in range(1,10):
for j in range(1,10):
self.advanced_tab_unlocked_layout.addWidget(QtGui.QPushButton("B"+str(i)+str(j)),i,j)
self.advanced_tab_unlocked.setLayout(self.advanced_tab_unlocked_layout)
def get_advanced_tab_layout(self):
return self.advanced_tab_layout
if __name__ == '__main__':
# Create main application window
app = QtGui.QApplication(sys.argv)
app.setStyleSheet('QWidget {font-size:18px}')
app.setStyle(QtGui.QStyleFactory.create("Cleanlooks"))
mw = QtGui.QMainWindow()
# mw.setWindowFlags(QtCore.Qt.FramelessWindowHint)
mw.showMaximized()
screen_height = QtGui.QApplication.desktop().screenGeometry().height()
screen_width = QtGui.QApplication.desktop().screenGeometry().width()
main_window_tab = TabWidget(width=screen_width, height=screen_height, tabs=6)
operation_one_tab = QtGui.QWidget()
advanced_tab = QtGui.QWidget()
operation_three_tab = QtGui.QWidget()
configuration_tab = QtGui.QWidget()
diagnostic_tab = QtGui.QWidget()
data_tab = QtGui.QWidget()
advanced_tab_widget = AdvancedTab()
# Main window tab layout
advanced_tab.setLayout(advanced_tab_widget.get_advanced_tab_layout())
main_window_tab.addTab(advanced_tab, "Operation 2")
main_window_tab.addTab(operation_one_tab, "Operation 1")
main_window_tab.addTab(operation_three_tab, "Operation 3")
main_window_tab.addTab(configuration_tab, "Configuration")
main_window_tab.addTab(diagnostic_tab, "Diagnostic")
main_window_tab.addTab(data_tab, "Data")
mw.setCentralWidget(main_window_tab)
mw.show()
QtGui.QShortcut(QtGui.QKeySequence('Ctrl+Q'), main_window_tab, exit_application)
sys.exit(app.exec_())
答案 0 :(得分:0)
解决方法是手动更改resizeEvent
中的样式表。
from PyQt4 import QtCore, QtGui
import base64
import sys
def exit_application():
"""Exit program event handler"""
sys.exit(1)
class HorizontalTabBar(QtGui.QTabBar):
def __init__(self, parent=None):
super(HorizontalTabBar, self).__init__(parent)
def paintEvent(self, event):
painter = QtGui.QStylePainter(self)
tab_options = QtGui.QStyleOptionTab()
for tab in range(self.count()):
self.initStyleOption(tab_options, tab)
painter.drawControl(QtGui.QStyle.CE_TabBarTabShape, tab_options)
painter.save()
size = tab_options.rect.size()
size.transpose()
center = self.tabRect(tab).center()
painter.translate(center)
painter.rotate(0)
painter.translate(-center)
painter.drawControl(QtGui.QStyle.CE_TabBarTabLabel, tab_options)
painter.restore()
class TabWidget(QtGui.QTabWidget):
def __init__(self, tabs):
QtGui.QTabWidget.__init__(self)
self.width_ratio = tabs
self.height_ratio = 20
self.horizontal_tab_bar = HorizontalTabBar()
self.setTabBar(self.horizontal_tab_bar)
self.setTabPosition(QtGui.QTabWidget.South)
self.setTabShape(QtGui.QTabWidget.Rounded)
def resizeEvent(self, event):
self.screen_width = self.frameGeometry().width()/self.width_ratio
self.screen_height = self.frameGeometry().height()/self.height_ratio
self.setStyleSheet("QTabBar::tab { width: %d px; height: %d px;}" % (self.screen_width, self.screen_height))
return super(TabWidget, self).resizeEvent(event)
class AdvancedTab(QtGui.QWidget):
def __init__(self, parent=None):
super(AdvancedTab, self).__init__(parent)
self.setWindowModality(QtCore.Qt.ApplicationModal)
self.setWindowFlags(QtCore.Qt.FramelessWindowHint)
self.advanced_tab_locked_init()
self.advanced_tab_unlocked_init()
self.advanced_tab_layout = QtGui.QGridLayout()
# Two layouts: locked and unlocked
# 0 - locked
# 1 - unlocked
self.advanced_tab_page_switch = QtGui.QStackedWidget()
self.advanced_tab_page_switch.addWidget(self.advanced_tab_locked)
self.advanced_tab_page_switch.addWidget(self.advanced_tab_unlocked)
self.advanced_tab_page_switch.setCurrentIndex(1)
self.advanced_tab_layout.addWidget(self.advanced_tab_page_switch,0,0,alignment=QtCore.Qt.AlignLeft)
def advanced_tab_locked_init(self):
"""Advanced tab lock screen"""
self.advanced_tab_locked = QtGui.QWidget()
self.advanced_tab_locked_layout = QtGui.QVBoxLayout()
self.test = QtGui.QLabel('sdds')
self.advanced_tab_locked_layout.addWidget(self.test)
self.advanced_tab_locked.setLayout(self.advanced_tab_locked_layout)
def advanced_tab_unlocked_init(self):
self.advanced_tab_unlocked = QtGui.QWidget()
self.advanced_tab_unlocked_layout = QtGui.QGridLayout()
for i in range(1,10):
for j in range(1,10):
self.advanced_tab_unlocked_layout.addWidget(QtGui.QPushButton("B"+str(i)+str(j)),i,j)
self.advanced_tab_unlocked.setLayout(self.advanced_tab_unlocked_layout)
def get_advanced_tab_layout(self):
return self.advanced_tab_layout
if __name__ == '__main__':
# Create main application window
app = QtGui.QApplication(sys.argv)
app.setStyleSheet('QWidget {font-size:18px}')
app.setStyle(QtGui.QStyleFactory.create("Cleanlooks"))
mw = QtGui.QMainWindow()
# mw.setWindowFlags(QtCore.Qt.FramelessWindowHint)
mw.showMaximized()
main_window_tab = TabWidget(tabs=6)
operation_one_tab = QtGui.QWidget()
advanced_tab = QtGui.QWidget()
operation_three_tab = QtGui.QWidget()
configuration_tab = QtGui.QWidget()
diagnostic_tab = QtGui.QWidget()
data_tab = QtGui.QWidget()
advanced_tab_widget = AdvancedTab()
# Main window tab layout
advanced_tab.setLayout(advanced_tab_widget.get_advanced_tab_layout())
main_window_tab.addTab(advanced_tab, "Operation 2")
main_window_tab.addTab(operation_one_tab, "Operation 1")
main_window_tab.addTab(operation_three_tab, "Operation 3")
main_window_tab.addTab(configuration_tab, "Configuration")
main_window_tab.addTab(diagnostic_tab, "Diagnostic")
main_window_tab.addTab(data_tab, "Data")
mw.setCentralWidget(main_window_tab)
mw.show()
QtGui.QShortcut(QtGui.QKeySequence('Ctrl+Q'), main_window_tab, exit_application)
sys.exit(app.exec_())