如何在 PyQt4 中相对于标签大小的变化保持按钮不变 [英] How to keep Push Buttons constant in relative to change of Label Size in PyQt4

查看:20
本文介绍了如何在 PyQt4 中相对于标签大小的变化保持按钮不变的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个图像作为我在 PyQt4 程序中的标签.该程序还包含一些按钮.现在,当我最大化我的窗口时,虽然标签得到扩展,但按钮保留了它们的位置.我希望按钮应该保持它们相对于标签的相对位置,即使在最大化时也是如此.

I have an image as my label in a PyQt4 Program. This program also contains some push buttons. Now, when I maximize my window, though the label gets expanded but the push button retain their place. I want that the push buttons should maintain their relative position with respect to the label, even when maximized.

我的代码如下:

class Ui_MainWindow(object):
   def setupUi(self, MainWindow):

       self.centralwidget = QtGui.QWidget(MainWindow)
       MainWindow.setCentralWidget(self.centralwidget)
       lay = QtGui.QVBoxLayout(self.centralwidget)
       self.centralwidget.setObjectName(_fromUtf8("centralwidget"))
       self.label = QtGui.QLabel(self.centralwidget)
       self.label.setText(_fromUtf8(""))
       self.label.setPixmap(QtGui.QPixmap(_fromUtf8("Capture.PNG")))
       self.label.setObjectName(_fromUtf8("label"))
       self.label.setScaledContents(True)
       lay.addWidget(self.label)
       self.Langdon = QtGui.QPushButton(self.centralwidget)
       self.Langdon.setGeometry(QtCore.QRect(290, 90, 75, 23))
       self.Langdon.setObjectName(_fromUtf8("Langdon"))     
       self.ChainLakes = QtGui.QPushButton(self.centralwidget)
       self.ChainLakes.setGeometry(QtCore.QRect(50, 290, 85, 23))
       self.ChainLakes.setObjectName(_fromUtf8("ChainLakes"))

   if __name__ == "__main__":
      import sys
      app = QtGui.QApplication(sys.argv)
      MainWindow = QtGui.QMainWindow()
      ui = Ui_MainWindow()
      ui.setupUi(MainWindow)
      MainWindow.show()
      sys.exit(app.exec_())

我应该如何修改我的代码??

How shall I modify my code??

推荐答案

您对按钮使用绝对定位,而对标签使用相对定位(因为它在布局中).由于按钮是绝对定位的,因此只要容器大小(您的主窗口或中央小部件)发生变化,您就有责任在自己周围移动它们.OTOH,如果您使用布局定位它们,则布局会根据需要移动它们(与标签一样).

You're using absolute positioning for the buttons, but relative for the label (because it is in the layout). Since the buttons are positioned absolutely, you're responsible for moving them around yourself whenever the container size (your main window or central widget) changes. OTOH if you position them using a layout, the layout then moves them around as needed (as with the label).

我强烈建议您找到一个适合您需求的基于布局的解决方案(它们非常灵活,但有时可能需要一些小技巧).除此之外,您需要子类化容器(您的 centralWidget QWidget)并重新实现 QWidget::resizeEvent() 处理程序.在该处理程序中,您将根据容器的当前大小和标签的当前大小/位置将绝对定位的元素(按钮)移动到新位置.

I highly recommend you find a layout-based solution which would suit your needs (they're very flexible but can sometimes take a bit of fiddling). Barring that, you'll need to subclass the container (your centralWidget QWidget) and re-implement the QWidget::resizeEvent() handler. Inside that handler you would move the absolutely positioned elements (the buttons) to a new position based on current size of the container and current size/position of the label.

我对 PyQt 不够方便,无法给出具体示例(抱歉),但我确实为可能有用的 C++ 问题提供了类似的答案:QPushButton 在另一个小部件顶部对齐

I'm not handy enough with PyQt to give a concrete example (sorry), but I did provide a similar answer to a C++ question which may be useful: QPushButton alignment on top another widget

这篇关于如何在 PyQt4 中相对于标签大小的变化保持按钮不变的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆