含有章节索引的中文 文章模板

::-- zhuyj [2008-06-06 07:36:43]

1. Layout management in PyQt4

Important thing in programming is the layout management. Layout management is the way how we place the widgets on the window. The management can be done in two ways. We can use absolute positioning or layout classes.

1.1. layout classes 布局类化

Absolute positioning绝对位置 The programmer specifies the position and the size of each widget in pixels. When you use absolute positioning, you have to understand several things.

the size and the position of a widget do not change, if you resize a window

applications might look different on various platforms

changing fonts in your application might spoil the layout

if you decide to change your layout, you must completely redo your layout, which is tedious and time consuming

   1 #!/usr/bin/python
   2 # absolute.py
   3 import sys
   4 from PyQt4 import QtGui
   5 class Absolute(QtGui.QWidget):
   6     def __init__(self, parent=None):
   7         QtGui.QWidget.__init__(self, parent)
   8         self.setWindowTitle('Communication')
   9         label = QtGui.QLabel('Couldn\'t', self)
  10         label.move(15, 10)
  11         label = QtGui.QLabel('care', self)
  12         label.move(35, 40)
  13         label = QtGui.QLabel('less', self)
  14         label.move(55, 65)
  15         label = QtGui.QLabel('And', self)
  16         label.move(115, 65)
  17         label = QtGui.QLabel('then', self)
  18         label.move(135, 45)
  19         label = QtGui.QLabel('you', self)
  20         label.move(115, 25)
  21         label = QtGui.QLabel('kissed', self)
  22         label.move(145, 10)
  23         label = QtGui.QLabel('me', self)
  24         label.move(215, 10)
  25         self.resize(250, 150)
  26 app = QtGui.QApplication(sys.argv)
  27 qb = Absolute()
  28 qb.show()
  29 sys.exit(app.exec_())

We simply call the move() method to position our widgets. In our case QLabel-s. We position them by providing the x and the y coordinates. The beginning of the coordinate system is at the left top corner. The x values grow from left to right. The y values grow from top to bottom.

absolute.jpg Figure: absolute positioning

1.2. Box Layout

盒子布局 Layout management with layout classes is much more flexible and practical. It is the preferred way to place widgets on a window. The basic layout classes are QHBoxLayout<QVBoxLayout. They line up widgets horizontally and vertically.

Imagine that we wanted to place two buttons in the right bottom corner. To create such a layout, we will use one horizontal and one vertical box. To create the neccessary space, we will add a stretch factor.

   1 #!/usr/bin/python
   2 # boxlayout.py
   3 import sys
   4 from PyQt4 import QtGui
   5 class BoxLayout(QtGui.QWidget):
   6     def __init__(self, parent=None):
   7         QtGui.QWidget.__init__(self, parent)
   8         self.setWindowTitle('box layout')
   9         ok = QtGui.QPushButton("OK")
  10         cancel = QtGui.QPushButton("Cancel")
  11         hbox = QtGui.QHBoxLayout()
  12         hbox.addStretch(1)
  13         hbox.addWidget(ok)
  14         hbox.addWidget(cancel)
  15         vbox = QtGui.QVBoxLayout()
  16         vbox.addStretch(1)
  17         vbox.addLayout(hbox)
  18         self.setLayout(vbox)
  19         self.resize(300, 150)
  20 app = QtGui.QApplication(sys.argv)
  21 qb = BoxLayout()
  22 qb.show()
  23 sys.exit(app.exec_())

 ok = QtGui.QPushButton("OK")
 cancel = QtGui.QPushButton("Cancel")

Here we create two push buttons.

 hbox = QtGui.QHBoxLayout()
 hbox.addStretch(1)
 hbox.addWidget(ok)
 hbox.addWidget(cancel)

We create a horizontal box layout. Add a stretch factor and both buttons.

 vbox = QtGui.QVBoxLayout()
 vbox.addStretch(1)
 vbox.addLayout(hbox)

To create the necessary layout, we put a horizontal lauout into a vertical one.

 self.setLayout(vbox)

Finally, we set the main layout of the window.

boxlayout.jpg Figure: box layout

1.3. QGridLayout

The most universal layout class is the grid layout. This layout divides the space into rows and columns. To create a grid layout, we use the QGridLayout class.

   1 #!/usr/bin/python
   2 # gridlayout.py
   3 import sys
   4 from PyQt4 import QtGui
   5 class GridLayout(QtGui.QWidget):
   6     def __init__(self, parent=None):
   7         QtGui.QWidget.__init__(self, parent)
   8         self.setWindowTitle('grid layout')
   9         names = ['Cls', 'Bck', '', 'Close', '7', '8', '9', '/',
  10             '4', '5', '6', '*', '1', '2', '3', '-',
  11             '0', '.', '=', '+']
  12         grid = QtGui.QGridLayout()
  13         j = 0
  14         pos = [(0, 0), (0, 1), (0, 2), (0, 3),
  15                 (1, 0), (1, 1), (1, 2), (1, 3),
  16                 (2, 0), (2, 1), (2, 2), (2, 3),
  17                 (3, 0), (3, 1), (3, 2), (3, 3 ),
  18                 (4, 0), (4, 1), (4, 2), (4, 3)]
  19         for i in names:
  20             button = QtGui.QPushButton(i)
  21             if j == 2:
  22                 grid.addWidget(QtGui.QLabel(''), 0, 2)
  23             else: grid.addWidget(button, pos[j][0], pos[j][1])
  24             j = j + 1
  25         self.setLayout(grid)
  26 app = QtGui.QApplication(sys.argv)
  27 qb = GridLayout()
  28 qb.show()
  29 sys.exit(app.exec_())

In our example, we create a grid of buttons. To fill one gap, we add one QLabel widget.

 grid = QtGui.QGridLayout()

Here we create a grid layout.

 if j == 2:
     grid.addWidget(QtGui.QLabel(''), 0, 2)
 else: grid.addWidget(button, pos[j][0], pos[j][1])

To add a widget to a grid, we call the addWidget() method. The arguments are the widget, the row and the column number.

gridlayout.jpg Figure: grid layout

Widgets can span multiple columns or rows in a grid. In the next example we illustrate this.

   1  #!/usr/bin/python
   2 # gridlayout2.py
   3 import sys
   4 from PyQt4 import QtGui
   5 class GridLayout2(QtGui.QWidget):
   6     def __init__(self, parent=None):
   7         QtGui.QWidget.__init__(self, parent)
   8         self.setWindowTitle('grid layout')
   9         title = QtGui.QLabel('Title')
  10         author = QtGui.QLabel('Author')
  11         review = QtGui.QLabel('Review')
  12         titleEdit = QtGui.QLineEdit()
  13         authorEdit = QtGui.QLineEdit()
  14         reviewEdit = QtGui.QTextEdit()
  15         grid = QtGui.QGridLayout()
  16         grid.setSpacing(10)
  17         grid.addWidget(title, 1, 0)
  18         grid.addWidget(titleEdit, 1, 1)
  19         grid.addWidget(author, 2, 0)
  20         grid.addWidget(authorEdit, 2, 1)
  21         grid.addWidget(review, 3, 0)
  22         grid.addWidget(reviewEdit, 3, 1, 5, 1)
  23         self.setLayout(grid)
  24         self.resize(350, 300)
  25 app = QtGui.QApplication(sys.argv)
  26 qb = GridLayout2()
  27 qb.show()
  28 sys.exit(app.exec_())

 grid = QtGui.QGridLayout()
 grid.setSpacing(10)

We create a grid layout and set spacing between widgets.

 grid.addWidget(reviewEdit, 3, 1, 5, 1)

If we add a widget to a grid, we can provide row span and column span of the widget. In our case, we make the reviewEdit widget span 5 rows.

gridlayout2.jpg Figure: grid layout2

2. 交流

Layout_Management_层管理 (last edited 2010-11-10 00:45:23 by zhuyj)