无法使用PySid为QCombBox分配正确的值

2024-06-16 13:02:44 发布

您现在位置:Python中文网/ 问答频道 /正文

由于Pyside不能被pickle,我决定手动将我的所有系统参数写入一个文件,当通过QFileDialog打开时,该文件可以用来用适当的数据填充所有QWidget…到目前为止,除了一个QWidget dataBox,它是一个单独类中的QComboBox之外,所有的东西都可以工作。你知道吗

正如您将在下面的代码中看到的,我放置了打印行以查看存储参数的列表的值。在stdout中,您可以看到所有的值都是应该的,但是QComboBox.setEditText()方法没有正确地分配值,因此组合框显示设置给它的文本…我链接的图像显示了关于“openFile”大小写的两件事:QComboBox(dataBox)中显示的文本位置奇怪(它可以工作,但是应该居中),并且显示数据的FigureCanvas的两个独立实例显示相同的数据(因为这些框被分配了相同的值,这也是dictionary/QComboBox中的第一个值)。dataBox的这两个实例中应该有dau 0和dau 1,但它们都有dau 0。你知道吗

我尝试了QComboBox类的另一种方法setItemText(),但它甚至没有显示任何数据或填充数据框。你知道吗

以下是saveFileopenFile两种情况下GUI的屏幕截图链接。你知道吗

以下是相关代码full code。我为要点中的格式设置道歉…从文本编辑复制不起作用,所以威尔。你知道吗

这个方法openFile正确地从文件中提取适当的数据,但是setEditText()方法没有正确地设置dataBox来显示它。请务必非常清楚,值DA_1在添加到组合框之前会正确打印,但一旦添加到组合框,就会出现问题……下面是要观察的两行最重要的代码:

print "dataBox value ", self.graphValues[x] # works fineself.opts[opt].dataBox.setEditText(self.graphValues[x]) # doesn't work at all

self.opts是一个字符串字典(图形/绘图的名称),与处理设置图形选项的类的实例成对出现

dataBox是保存所有字符串(图形/绘图名称)的QComboBox

# Read parameters from file and sets sim/graphValues (1.1)
def openFile(self):
    self.filePath, _= QFileDialog.getOpenFileName(self, 'open file', \
        '~/', 'C Files (*.c);;Simulations (*.sim)' )
    fullPath = self.filePath.split("/")
    title = fullPath[-1]
    self.w.setWindowTitle(title)
    if self.filePath[-4:] == ".sim":
        with open(self.filePath, 'r') as f:
            self.simValues = [""]
            value = f.readline()
            while 1:
                if value != "\n":
                    self.simValues.append(value)
                    value = f.readline()
                else: break
            self.graphValues = [""]
            value = f.readline()
            while 1:
                if value != "\n":
                    self.graphValues.append(value)
                    value = f.readline()
                else: break
            self.files = f.readlines()
        self.numSim.setText(self.simValues[1])
        self.numTrials.setText(self.simValues[2])
        self.numTicks.setText(self.simValues[3])
        self.numGraphs.setText(self.simValues[4])
        self.dirPath = self.simValues[5]
        x = 1
        for opt in self.opts:
            self.opts[opt].styleSelect.setEditText(str(self.styles[opt]))
            self.opts[opt].dataBox.addItems(self.files)
            if self.opts[opt].styleSelect.currentText() == "1":
                print "dataBox value ", self.graphValues[x]
                self.opts[opt].dataBox.setEditText(self.graphValues[x]);x+=1
                print "xlabel value ", self.graphValues[x]
                self.opts[opt].xLabel.setText(self.graphValues[x]);x+=1
                print "ylabel value ", self.graphValues[x]
                self.opts[opt].yLabel.setText(self.graphValues[x]);x+=1
                print "xrange value ", self.graphValues[x]
                self.opts[opt].xRange.setText(self.graphValues[x]);x+=1
                print "yrange value ", self.graphValues[x]
                self.opts[opt].yRange.setText(self.graphValues[x]);x+=1
            elif self.opts[opt].styleSelect.currentText() == "2":
                pass
            else:
                pass
        self.plot()

这个方法saveFile并不是真正需要显示的,但是我想澄清的是,问题不在于写入数据。你知道吗

# Saves sim & graph values and writes them to file (1.2)
def saveFile(self):
    self.filePath, _= QFileDialog.getSaveFileName(self, 'save file', \
         '~/', 'Simulations (*.sim)' )
    fullPath = self.filePath.split("/")
    title = fullPath[-1]
    self.w.setWindowTitle(title)
    self.simValues = [str(self.numSim.text()),str(self.numTrials.text()), \
                      str(self.numTicks.text()),str(self.numGraphs.text())]
    self.simValues.append(self.dirPath)
    self.graphValues = []
    for opt in self.opts:
        self.styles[opt] = self.opts[opt].styleSelect.currentText()
        if self.opts[opt].styleSelect.currentText() == "1":
            self.graphValues.append(self.opts[opt].dataBox.currentText())
            self.graphValues.append(self.opts[opt].xLabel.text())
            self.graphValues.append(self.opts[opt].yLabel.text())
            self.graphValues.append(self.opts[opt].xRange.text())
            self.graphValues.append(self.opts[opt].yRange.text())
        elif self.opts[opt].styleSelect.currentText() == "2":
            pass
        else:
            pass
    with open(self.filePath, 'w') as f:
        for item in self.simValues:
            f.write(item+"\n")
        f.write("\n")
        for item in self.graphValues:
            f.write(item+"\n")
        f.write("\n")
        for item in self.files:
            f.write(item+"\n")

以下是标准打印行:

dataBox value  DA_1

xlabel value  other

ylabel value  things

xrange value  len(data)

yrange value  y range

dataBox value  DA_0

xlabel value  some

ylabel value  stuff

xrange value  len(data)

yrange value  y range

Tags: 数据textselfvalueprintoptappendsettext