pyside-如何从gridLayout删除小部件 [英] pyside - how to delete widgets from gridLayout

查看:174
本文介绍了pyside-如何从gridLayout删除小部件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在QT Designer中构建了一个ui,然后使用pyside-uic将其转换为python文件,然后编写了一些代码以编程方式对其进行编辑.换句话说,我有一个按钮Add Row,单击该按钮将其重命名为Remove1,并创建另一个名称为Add Row的按钮,并将其添加到布局中.

I built a ui in QT Designer and then used pyside-uic turned it into a python file and have then written some code to edit it programmatically. In otherwords I have a pushbutton Add Row that when clicked will rename itself to Remove1 and create another pusbutton name it Add Row and add it to the layout.

单击Add Row时的代码,更改名称和信号/插槽:

Code when clicking Add Row, changes the name and the signals/slots:

self.pb_Row[-1].setText('Remove'+str(self.nRows))
self.pb_Row[-1].clicked.disconnect( self.addRow )
self.pb_Row[-1].clicked.connect( self.removeRow )

点击Remove时的

代码,删除所选按钮:

Code when clicking Remove, removes selected button:

iRow = int(self.sender().objectName().split('_')[-1])-1
ind = self.PropertyLayout.indexOf(self.pb_Row[iRow])
t = self.PropertyLayout.takeAt(ind)
t.widget().deleteLater()
# self.pb_Row[iRow].hide()
# self.pb_Row[iRow].deleteLater()
self.pb_Row.pop(iRow)

这很好,直到您添加至少一个然后将其删除,下次再将其弄乱为止.基本上,当我有两个按钮并删除一个按钮然后尝试添加一个按钮时,它的行为不正常.行为不端是指新按钮位于旧按钮的上方,有时显示在下方而不是上方.

This works just fine until you add at least one and then remove it, the next time round it messes up. Basically, it misbehaves when I have two buttons and remove one and then try to add one. By misbehaves I mean that the new button ends up on top of the old, sometimes it appears below instead of above.

此外,按照当前的行数,如果我使用.hide()函数,它并不会真正重组gridlayout中的内容.我不太确定应该使用哪个.

Also, with the lines as they currently are it doesn't really reorganise things in the gridlayout, if I use the .hide() function it does. I'm not quite sure which I should be using.

谢谢!

这是一个产生不良结果的序列:

Here is a sequence that produces undesirable results:

重新开始

点击添加后

单击删除"后(到目前为止一切正常),然后单击添加"(无明显差异)

After clicking remove (all fine so far), then click Add (no visible difference)

再次单击添加后

单击Remove2之后,Remove1从其下方显示

After clicking Remove2, Remove1 appears from under it

import numpy as np
import sys
from PySide import QtCore, QtGui
import matplotlib.pyplot as plt

from ModesInFiber import Ui_fiberModesMainWindow

class Ui_fiberModesMainWindow(object):
    def setupUi(self, fiberModesMainWindow):
        fiberModesMainWindow.setObjectName("fiberModesMainWindow")
        fiberModesMainWindow.resize(653, 597)
        self.centralwidget = QtGui.QWidget(fiberModesMainWindow)
        self.centralwidget.setObjectName("centralwidget")
        self.horizontalLayout_2 = QtGui.QHBoxLayout(self.centralwidget)
        self.horizontalLayout_2.setObjectName("horizontalLayout_2")
        self.MainLayout = QtGui.QGridLayout()
        self.MainLayout.setObjectName("MainLayout")
        self.PropertyLayout = QtGui.QGridLayout()
        self.PropertyLayout.setObjectName("PropertyLayout")
        self.lbl_Name = QtGui.QLabel(self.centralwidget)
        self.lbl_Name.setObjectName("lbl_Name")
        self.PropertyLayout.addWidget(self.lbl_Name, 0, 1, 1, 1)
        self.pb_addRow_1 = QtGui.QPushButton(self.centralwidget)
        self.pb_addRow_1.setObjectName("pb_addRow_1")
        self.PropertyLayout.addWidget(self.pb_addRow_1, 1, 5, 1, 1)
        self.ledit_Name_1 = QtGui.QLineEdit(self.centralwidget)
        self.ledit_Name_1.setObjectName("ledit_Name_1")
        self.PropertyLayout.addWidget(self.ledit_Name_1, 1, 1, 1, 1)
        self.MainLayout.addLayout(self.PropertyLayout, 0, 0, 1, 1)
        spacerItem2 = QtGui.QSpacerItem(20, 40, QtGui.QSizePolicy.Minimum, QtGui.QSizePolicy.Expanding)
        self.MainLayout.addItem(spacerItem2, 1, 0, 1, 1)
        self.horizontalLayout_2.addLayout(self.MainLayout)
        fiberModesMainWindow.setCentralWidget(self.centralwidget)

        self.retranslateUi(fiberModesMainWindow)
        QtCore.QMetaObject.connectSlotsByName(fiberModesMainWindow)
#         fiberModesMainWindow.setTabOrder(self.ledit_Name_1, self.ledit_Width_1)
#         fiberModesMainWindow.setTabOrder(self.ledit_Width_1, self.cmb_RIType_1)
#         fiberModesMainWindow.setTabOrder(self.cmb_RIType_1, self.ledit_RIParam_1)
#         fiberModesMainWindow.setTabOrder(self.ledit_RIParam_1, self.pb_addRow_1)

    def retranslateUi(self, fiberModesMainWindow):
        fiberModesMainWindow.setWindowTitle(QtGui.QApplication.translate("fiberModesMainWindow", "MainWindow", None, QtGui.QApplication.UnicodeUTF8))
        self.lbl_Name.setText(QtGui.QApplication.translate("fiberModesMainWindow", "Name", None, QtGui.QApplication.UnicodeUTF8))
        self.pb_addRow_1.setText(QtGui.QApplication.translate("fiberModesMainWindow", "Add Row", None, QtGui.QApplication.UnicodeUTF8))


class DesignerMainWindow(QtGui.QMainWindow, Ui_fiberModesMainWindow):

    def __init__(self, parent = None):
        super(DesignerMainWindow, self).__init__(parent)
        self.setupUi(self)
        self.pb_addRow_1.clicked.connect( self.addRow )

        self.ledit_Name     = [ self.ledit_Name_1 ]
        self.pb_Row      = [ self.pb_addRow_1 ]

        # number of rows
        self.nRows = 1


    def addRow( self ):

        self.ledit_Name[-1].setEnabled(False)
        self.pb_Row[-1].setText('Remove'+str(self.nRows))
        self.pb_Row[-1].clicked.disconnect( self.addRow )
        self.pb_Row[-1].clicked.connect( self.removeRow )

        self.nRows += 1

        self.ledit_Name.append( QtGui.QLineEdit(self.centralwidget) )
        self.ledit_Name[-1].setObjectName('ledit_Name_'+str(self.nRows))
        self.PropertyLayout.addWidget( self.ledit_Name[-1], self.nRows, 1, 1, 1)

        self.pb_Row.append( QtGui.QPushButton(self.centralwidget) )
        self.pb_Row[-1].setObjectName( 'pb_addRow_'+str(self.nRows) )
        self.pb_Row[-1].setText('Add Row')
        self.pb_Row[-1].clicked.connect( self.addRow )
        self.PropertyLayout.addWidget( self.pb_Row[-1], self.nRows, 5, 1, 1)


    def removeRow( self ):

        iRow = int(self.sender().objectName().split('_')[-1])-1
        self.nRows -= 1

        ind = self.PropertyLayout.indexOf(self.ledit_Name[iRow])
        t = self.PropertyLayout.takeAt(ind)
        t.widget().setParent(None)
#             t.widget().deleteLater()
#             self.ledit_Name[iRow].hide()
#             self.ledit_Name[iRow].deleteLater()
#             self.ledit_Name[iRow].setParent(None)
        self.ledit_Name.pop(iRow)

        ind = self.PropertyLayout.indexOf(self.pb_Row[iRow])
        t = self.PropertyLayout.takeAt(ind)
        t.widget().setParent(None)
#             t.widget().deleteLater()
#             self.pb_Row[iRow].hide()
#             self.pb_Row[iRow].deleteLater()
#             self.pb_Row[iRow].setParent(None)
        self.pb_Row.pop(iRow)

        for iAfterRow in range(iRow, self.nRows):
            self.ledit_Name[iAfterRow].setObjectName( 'ledit_Name_' + str(iAfterRow+1) )
            self.pb_Row[iAfterRow].setObjectName( 'ledit_Name_' + str(iAfterRow+1) )

        print 'Remove row', iRow

if __name__ == '__main__':
    app = QtGui.QApplication( sys.argv )
    dmw = DesignerMainWindow()
    dmw.show()
    sys.exit( app.exec_() )

推荐答案

此处的问题是由QGridLayout的实现细节引起的.

The problem here is caused by an implementation detail of QGridLayout.

每当从QGridLayout中删除项目时,即使 visual 行或列的数量可以减少,逻辑行和列的数量也永远不会减少.因此,您应始终使用 itemAtPosition .

Whenever items are deleted from a QGridLayout, the number of logical rows and columns will never decrease, even though the number of visual rows or colums may do. Because of this, you should always work directly with the items in the QGridLayout using methods such as getItemPosition and itemAtPosition.

下面是使用此方法的示例中的DesignerMainWindow类的重写.显然,可能需要进行一些调整才能使用您的实际应用程序.

Below is a re-write of the DesignerMainWindow class from the example using this approach. Obviously, it may need tweaking somewhat to work with your real application.

class DesignerMainWindow(QtGui.QMainWindow, Ui_fiberModesMainWindow):
    def __init__(self, parent = None):
        super(DesignerMainWindow, self).__init__(parent)
        self.setupUi(self)
        self.pb_addRow_1.clicked.connect( self.addRow )

    def addRow( self ):
        rows = self.PropertyLayout.rowCount()
        columns = self.PropertyLayout.columnCount()
        for column in range(columns):
            layout = self.PropertyLayout.itemAtPosition(rows - 1, column)
            if layout is not None:
                widget = layout.widget()
                if isinstance(widget, QtGui.QPushButton):
                    widget.setText('Remove %d' % (rows - 1))
                    widget.clicked.disconnect(self.addRow)
                    widget.clicked.connect(self.removeRow)
                else:
                    widget.setEnabled(False)
        widget = QtGui.QLineEdit(self.centralwidget)
        self.PropertyLayout.addWidget(widget, rows, 1, 1, 1)
        widget = QtGui.QPushButton(self.centralwidget)
        widget.setText('Add Row')
        widget.clicked.connect(self.addRow)
        self.PropertyLayout.addWidget(widget, rows, columns - 1, 1, 1)

    def removeRow(self):
        index = self.PropertyLayout.indexOf(self.sender())
        row = self.PropertyLayout.getItemPosition(index)[0]
        for column in range(self.PropertyLayout.columnCount()):
            layout = self.PropertyLayout.itemAtPosition(row, column)
            if layout is not None:
                layout.widget().deleteLater()
                self.PropertyLayout.removeItem(layout)

这篇关于pyside-如何从gridLayout删除小部件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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