作者:周周ZHOUZHOUZHOU | 来源:互联网 | 2023-07-26 13:15
我已经建立了一个标题行,我希望将其显示在顶部。但是,我希望图像按钮保持左对齐,而搜索栏+按钮位于中心。
我还尝试将背景色设置为orange
,但似乎只在图像按钮上设置了背景色,而不是整行设置。
这是一个演示:
from PyQt5.QtGui import *
from PyQt5.QtWidgets import *
from PyQt5.QtCore import *
class ImageButton(QWidget):
def __init__(self,img_location):
QWidget.__init__(self)
self.img_location = img_location
self.button = QToolButton(self)
self.button.setToolButtonStyle(Qt.ToolButtonTextUnderIcon)
self.button.clicked.connect(self.handleButton)
self.button.setIcon(QIcon(img_location))
self.button.setIconSize(QSize(30,30))
layout = QVBoxLayout(self)
layout.setContentsMargins(0,0)
layout.addWidget(self.button)
def handleButton(self):
pass
class HeaderBar(QWidget):
def __init__(self,parent=None):
super(HeaderBar,self).__init__(parent)
self.setStyleSheet('background-color: orange')
self.setfixedWidth(700)
self.render_header()
def render_header(self):
logo_imgbtn = ImageButton("test.png")
search_btn = QPushButton()
search_btn.setText("Search!")
search_btn.resize(30,30)
search_btn.setfixedWidth(40)
search_btn.clicked.connect(self.handleSearch)
search_btn.setautoDefault(True)
search_btn.setStyleSheet('background-color: black')
self.searchbox = QLineEdit()
self.searchbox.resize(280,40)
self.searchbox.setfixedWidth(280)
self.searchbox.returnpressed.connect(search_btn.click)
self.searchbox.setStyleSheet('background-color: brown')
layout = QHBoxLayout(self)
layout.setContentsMargins(0,0)
layout.addWidget(logo_imgbtn)
layout.addWidget(self.searchbox)
layout.addWidget(search_btn)
self.setLayout(layout)
def handleSearch(self):
print(self.searchbox.text())
class MainWindow(QMainWindow):
def __init__(self,parent=None):
super(MainWindow,self).__init__(parent)
self.render()
def render(self):
self.header = HeaderBar()
layout = QVBoxLayout()
layout.setContentsMargins(0,0)
layout.addWidget(self.header)
layout.addStretch()
layout.setalignment(Qt.AlignCenter)
cOntent= QWidget()
content.setLayout(layout)
w,h = (1024,670)
self.resize(w,h)
self.setCentralWidget(content)
self.show()
def set_scrollbar_y(self):
self.state.scroll_pos_y = self.scroll.verticalScrollBar().value()
if __name__ == '__main__':
app = QApplication([])
window = MainWindow()
app.exec_()
尤其是,我将橙色背景设置如下:
class HeaderBar(QWidget):
def __init__(self,self).__init__(parent)
self.setStyleSheet('background-color: orange')
self.setfixedWidth(700)
self.render_header()
这是结果:
观察:
- 标题小部件没有整行的背景色,而是似乎仅应用于图像按钮。
如何解决以上问题?