ขณะนี้ฉันมี QScrollArea ที่กำหนดโดย:
self.results_grid_scrollarea = QScrollArea()
self.results_grid_widget = QWidget()
self.results_grid_layout = QGridLayout()
self.results_grid_layout.setSizeConstraint(QLayout.SetMinAndMaxSize)
self.results_grid_widget.setLayout(self.results_grid_layout)
self.results_grid_scrollarea.setWidgetResizable(True)
self.results_grid_scrollarea.setWidget(self.results_grid_widget)
self.results_grid_scrollarea.setViewportMargins(0,20,0,0)
ซึ่งวางซ้อนกันอย่างมีความสุขภายในเลย์เอาต์/วิดเจ็ตอื่น ๆ ปรับขนาดตามที่คาดไว้ ฯลฯ
เพื่อจัดเตรียมส่วนหัวสำหรับคอลัมน์กริด ฉันกำลังใช้ QGridLayout อื่นที่อยู่เหนือพื้นที่เลื่อนโดยตรง ซึ่งใช้งานได้... แต่ดูแปลก ๆ เล็กน้อย แม้ว่าจะจัดสไตล์อย่างเหมาะสม โดยเฉพาะอย่างยิ่งเมื่อแถบเลื่อนตามความต้องการ (แนวตั้ง) ปรากฏขึ้นหรือหายไป ตามความจำเป็นและส่วนหัวไม่สอดคล้องกับคอลัมน์กริดอีกต่อไป ฉันรู้ว่าเป็นสุนทรียศาสตร์...แต่ฉันก็ค่อนข้างจู้จี้จุกจิก ;)
วิดเจ็ตอื่น ๆ จะถูกเพิ่ม/ลบไปยัง self.results_grid_layout
โดยทางโปรแกรมที่อื่น บรรทัดสุดท้ายด้านบนที่ฉันเพิ่งเพิ่มไปเมื่อเร็ว ๆ นี้เนื่องจากฉันคิดว่ามันจะง่ายต่อการใช้พื้นที่ระยะขอบที่สร้างขึ้น เอกสาร สำหรับสถานะ setViewportMargins:
ตั้งค่าระยะขอบรอบๆ พื้นที่การเลื่อน สิ่งนี้มีประโยชน์สำหรับแอปพลิเคชัน เช่น สเปรดชีตที่มีแถวและคอลัมน์ "ล็อก" พื้นที่ขอบเว้นว่างไว้ วางวิดเจ็ตในพื้นที่ที่ไม่ได้ใช้
แต่ฉันไม่สามารถหาวิธีการบรรลุเป้าหมายนี้ได้ตลอดชีวิต และ GoogleFu ของฉันก็ได้ละทิ้งฉันในวันนี้ หรือมีข้อมูล/ตัวอย่างเพียงเล็กน้อยเกี่ยวกับวิธีการบรรลุเป้าหมายนี้
หัวของฉันกำลังบอกฉันว่าฉันสามารถกำหนดวิดเจ็ตได้เพียงอันเดียวซึ่งควบคุมโดยเลย์เอาต์ (ที่มีวิดเจ็ตอื่น ๆ จำนวนเท่าใดก็ได้) ให้กับพื้นที่เลื่อน - อย่างที่ฉันเคยทำไปแล้ว ถ้าฉันเพิ่มพูด QHeaderview เช่นแถว 0 ของ gridlayout มันจะปรากฏใต้ระยะขอบของวิวพอร์ตและเลื่อนไปพร้อมกับเค้าโครงที่เหลือ หรือฉันขาดอะไรบางอย่างไปแต่มองไม่เห็นเนื้อไม้แทนต้นไม้?
ฉันแค่กำลังเรียนรู้ Python/Qt ดังนั้นความช่วยเหลือ พอยน์เตอร์ และ/หรือตัวอย่าง (ควรเป็น Python แต่ไม่จำเป็น) จะได้รับการชื่นชม!
แก้ไข: หลังจากทำตามคำแนะนำที่ให้ไว้ (ฉันคิดว่า) ฉันจึงได้โปรแกรมทดสอบเล็กๆ น้อยๆ ต่อไปนี้เพื่อทดลองใช้:
import sys
from PySide.QtCore import *
from PySide.QtGui import *
class MainWindow(QMainWindow):
def __init__(self, parent=None):
super(MainWindow, self).__init__(parent)
self.setMinimumSize(640, 480)
self.container_widget = QWidget()
self.container_layout = QVBoxLayout()
self.container_widget.setLayout(self.container_layout)
self.setCentralWidget(self.container_widget)
self.info_label = QLabel(
"Here you can see the problem.... I hope!\n"
"Once the window is resized everything behaves itself.")
self.info_label.setWordWrap(True)
self.headings_widget = QWidget()
self.headings_layout = QGridLayout()
self.headings_widget.setLayout(self.headings_layout)
self.headings_layout.setContentsMargins(1,1,0,0)
self.heading_label1 = QLabel("Column 1")
self.heading_label1.setContentsMargins(16,0,0,0)
self.heading_label2 = QLabel("Col 2")
self.heading_label2.setAlignment(Qt.AlignCenter)
self.heading_label2.setMaximumWidth(65)
self.heading_label3 = QLabel("Column 3")
self.heading_label3.setContentsMargins(8,0,0,0)
self.headings_layout.addWidget(self.heading_label1,0,0)
self.headings_layout.addWidget(self.heading_label2,0,1)
self.headings_layout.addWidget(self.heading_label3,0,2)
self.headings_widget.setStyleSheet(
"background: green; border-bottom: 1px solid black;" )
self.grid_scrollarea = QScrollArea()
self.grid_widget = QWidget()
self.grid_layout = QGridLayout()
self.grid_layout.setSizeConstraint(QLayout.SetMinAndMaxSize)
self.grid_widget.setLayout(self.grid_layout)
self.grid_scrollarea.setWidgetResizable(True)
self.grid_scrollarea.setWidget(self.grid_widget)
self.grid_scrollarea.setViewportMargins(0,30,0,0)
self.headings_widget.setParent(self.grid_scrollarea)
### Add some linedits to the scrollarea just to test
rows_to_add = 10
## Setting the above to a value greater than will fit in the initial
## window will cause the lineedits added below to display correctly,
## however - using the 10 above, the lineedits do not expand to fill
## the scrollarea's width until you resize the window horizontally.
## What's the best way to fix this odd initial behaviour?
for i in range(rows_to_add):
col1 = QLineEdit()
col2 = QLineEdit()
col2.setMaximumWidth(65)
col3 = QLineEdit()
row = self.grid_layout.rowCount()
self.grid_layout.addWidget(col1,row,0)
self.grid_layout.addWidget(col2,row,1)
self.grid_layout.addWidget(col3,row,2)
### Define Results group to hold the above sections
self.test_group = QGroupBox("Results")
self.test_layout = QVBoxLayout()
self.test_group.setLayout(self.test_layout)
self.test_layout.addWidget(self.info_label)
self.test_layout.addWidget(self.grid_scrollarea)
### Add everything to the main layout
self.container_layout.addWidget(self.test_group)
def resizeEvent(self, event):
scrollarea_vpsize = self.grid_scrollarea.viewport().size()
scrollarea_visible_size = self.grid_scrollarea.rect()
desired_width = scrollarea_vpsize.width()
desired_height = scrollarea_visible_size.height()
desired_height = desired_height - scrollarea_vpsize.height()
new_geom = QRect(0,0,desired_width+1,desired_height-1)
self.headings_widget.setGeometry(new_geom)
def main():
app = QApplication(sys.argv)
form = MainWindow()
form.show()
app.exec_()
if __name__ == '__main__':
main()
มีบางอย่างตามบรรทัดเหล่านี้เป็นวิธีการที่คุณชี้ไปหรือไม่? ทุกอย่างทำงานตามที่คาดไว้เหมือนกับสิ่งที่ฉันทำ ยกเว้นพฤติกรรมเริ่มต้นแปลก ๆ ก่อนที่ผู้ใช้จะปรับขนาดหน้าต่าง เมื่อปรับขนาดทุกอย่างแล้ว ทุกอย่างก็เรียบร้อยดี ฉันอาจจะคิดมากอีกครั้งหรืออย่างน้อยก็มองข้ามบางสิ่งบางอย่าง... มีความคิดอะไรบ้าง?
grid_layout.setSizeConstraint
ดูเหมือนว่าจะแก้ปัญหาได้ - person ekhumoro   schedule 24.02.2012