首页 > 解决方案 > 如何访问作为 List 中的 Box 的小部件

问题描述

在下面的代码中,我正在访问垂直滚动列表,并且 e 设置为我之后的条目。

我认为这只是一个具有 2 个小部件的 QHLayout 的小部件,但我尝试访问失败。

通过下面的尝试,我得到 builtin_function_or_method' 对象没有属性 'hbox

class GDMLColourMapEntry(QtGui.QWidget) :
    def __init__(self,colour,material) :
        super().__init__()
        print('Map Entry : '+str(colour))
        self.colour = colour
        self.hbox = QtGui.QHBoxLayout()
        self.hbox.addWidget(GDMLColour(colour))
        self.hbox.addWidget(material)
        self.setLayout(self.hbox)

    def dataPicker(self):
        print('DataPicker')


class GDMLColourMapList(QtGui.QScrollArea) :
    def __init__(self,matList) :
        super().__init__()
        # Scroll Area which contains the widgets, set as the centralWidget
        # Widget that contains the collection of Vertical Box
        selfwidget = QtGui.QWidget()
        self.matList = matList
        # The Vertical Box that contains the Horizontal Boxes of  labels and buttons
        self.vbox = QtGui.QVBoxLayout()
        self.widget.setLayout(self.vbox)

        #Scroll Area Properties
        self.setVerticalScrollBarPolicy(QtGui.Qt.ScrollBarAlwaysOn)
        self.setHorizontalScrollBarPolicy(QtGui.Qt.ScrollBarAlwaysOff)
        self.setWidgetResizable(True)
        self.setWidget(self.widget)

    def addEntry(self, colour) :
        print('Add Entry')
        mat = GDMLMaterial(self.matList)
        self.vbox.addWidget(GDMLColourMapEntry(colour,mat))

     
class GDMLColourMap(QtGui.QDialog) :     
    #class GDMLColourMap(QtGui.QMainWindow) :
    def __init__(self, parent) :
        super(GDMLColourMap, self).__init__(parent, QtCore.Qt.Tool)
        self.initUI()

    def initUI(self):  
        self.result = userCancelled
        # create our window
        # define window           xLoc,yLoc,xDim,yDim
        self.setGeometry( 250, 250, 550, 350)
        self.setWindowTitle("Map FreeCAD Colours to GDML Materials")
        self.setMouseTracking(True)
        lay = QtGui.QGridLayout(self)

        materialList = self.getGDMLMaterials()
        self.mapList = GDMLColourMapList(materialList)
        mat = GDMLMaterial(self.matList)
        self.vbox.addWidget(GDMLColourMapEntry(colour,mat))
   
    def lookupColour(self, col) :
        print('Lookup Colour')
        idx = self.colorList.index(col)
        print(idx)
        e = self.mapList.vbox.itemAt(idx)
        print(e)
        f = e.hbox.itemAt(1)
        print(f)
        return(255)

标签: pythonpython-3.xpyqtpyqt5

解决方案


固定与

def lookupColour(self, col) :
    print('Lookup Colour')
    idx = self.colorList.index(col)
    print(idx)
    entry = self.mapList.vbox.itemAt(idx).widget()
    print(entry)
    mat = entry.hbox.itemAt(1).widget().currentText()
    print(mat)
    return mat
    

推荐阅读