<small id='qag99'></small><noframes id='qag99'>

  1. <tfoot id='qag99'></tfoot>
      <bdo id='qag99'></bdo><ul id='qag99'></ul>
  2. <legend id='qag99'><style id='qag99'><dir id='qag99'><q id='qag99'></q></dir></style></legend>

      <i id='qag99'><tr id='qag99'><dt id='qag99'><q id='qag99'><span id='qag99'><b id='qag99'><form id='qag99'><ins id='qag99'></ins><ul id='qag99'></ul><sub id='qag99'></sub></form><legend id='qag99'></legend><bdo id='qag99'><pre id='qag99'><center id='qag99'></center></pre></bdo></b><th id='qag99'></th></span></q></dt></tr></i><div id='qag99'><tfoot id='qag99'></tfoot><dl id='qag99'><fieldset id='qag99'></fieldset></dl></div>

      如何在 PyQt4 中相对于标签大小的变化保持按钮不

      时间:2023-08-05

      • <i id='Qq1q9'><tr id='Qq1q9'><dt id='Qq1q9'><q id='Qq1q9'><span id='Qq1q9'><b id='Qq1q9'><form id='Qq1q9'><ins id='Qq1q9'></ins><ul id='Qq1q9'></ul><sub id='Qq1q9'></sub></form><legend id='Qq1q9'></legend><bdo id='Qq1q9'><pre id='Qq1q9'><center id='Qq1q9'></center></pre></bdo></b><th id='Qq1q9'></th></span></q></dt></tr></i><div id='Qq1q9'><tfoot id='Qq1q9'></tfoot><dl id='Qq1q9'><fieldset id='Qq1q9'></fieldset></dl></div>
          <bdo id='Qq1q9'></bdo><ul id='Qq1q9'></ul>
            1. <legend id='Qq1q9'><style id='Qq1q9'><dir id='Qq1q9'><q id='Qq1q9'></q></dir></style></legend>
                <tbody id='Qq1q9'></tbody>

                <small id='Qq1q9'></small><noframes id='Qq1q9'>

              1. <tfoot id='Qq1q9'></tfoot>
                本文介绍了如何在 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 中相对于标签大小的变化保持按钮不变的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!

                上一篇:我想把文本放在 pyqt QCalendarWidget 下一篇:为什么我的 QFileSystemModel QModelIndex 无法获取子节

                相关文章

                最新文章

                <small id='I4Sb4'></small><noframes id='I4Sb4'>

              2. <legend id='I4Sb4'><style id='I4Sb4'><dir id='I4Sb4'><q id='I4Sb4'></q></dir></style></legend>
                <tfoot id='I4Sb4'></tfoot>
                • <bdo id='I4Sb4'></bdo><ul id='I4Sb4'></ul>
                <i id='I4Sb4'><tr id='I4Sb4'><dt id='I4Sb4'><q id='I4Sb4'><span id='I4Sb4'><b id='I4Sb4'><form id='I4Sb4'><ins id='I4Sb4'></ins><ul id='I4Sb4'></ul><sub id='I4Sb4'></sub></form><legend id='I4Sb4'></legend><bdo id='I4Sb4'><pre id='I4Sb4'><center id='I4Sb4'></center></pre></bdo></b><th id='I4Sb4'></th></span></q></dt></tr></i><div id='I4Sb4'><tfoot id='I4Sb4'></tfoot><dl id='I4Sb4'><fieldset id='I4Sb4'></fieldset></dl></div>