gpt4 book ai didi

python - 如何继承 QTableWidget 和我的类 MainWindow?

转载 作者:太空宇宙 更新时间:2023-11-03 14:59:27 27 4
gpt4 key购买 nike

首先,我对 python 比较陌生,对 PyQt 也很陌生。我正在迈出面向对象编程的第一步,并且一直在查看在线教程,但我陷入了多重继承问题。

我有一个名为 CreateTable() 的类,其目的是在我的应用程序窗口中创建一个 QTableWidget,当单击一行时,它会打开一个上下文菜单,然后您可以选择用于绘制所选行的选项。其中图形选项连接到另一个类 Plotter()。

我的问题是 CreateTable 需要从 PyQt 继承 QTableWidget 和我的 MainWindow 类,因为我需要引用布局变量才能将我的图形嵌入到我的应用程序窗口中。

我尝试继承的代码如下,大量借用自此处:How does Python's super() work with multiple inheritance?

class QTable(QTableWidget):
def __init__(self):
super(QTable, self).__init__()
class PassMain(MainWindow):
def __init__(self):
super(PassMain, self).__init__()
class PassMainTable(PassMain, QTable):
def __init__(self):
super(PassMainTable, self).__init__()

主要问题是当我尝试将图形放置在主窗口布局中时。

self.vboxRight.addWidget(self.Graph) 

这是我用于创建表和调用绘图仪的代码

class CreateTable(PassMainTable): #QTableWidget
def __init__(self, Data, row, col, colHeaders, rowHeaders): #Index, ColumnHeaders
super(CreateTable, self).__init__()


self.setSelectionBehavior(self.SelectRows)

print("Start initialization")
self.ColHeader = colHeaders
self.setRowCount(row)
self.setColumnCount(col)
self.data = Data
self.setHorizontalHeaderLabels(colHeaders)

print("Right before for loop")

n = len(Data)
m = len(colHeaders)

for i in range(n):
DataValues = self.data.iloc[i,:]
print("values are {}".format(DataValues))
#m = len(values)
ConvertedVals = pd.to_numeric(DataValues)

ValList = DataValues.values.tolist()
print(ValList)

for j in range(0,m):
self.item = QTableWidgetItem(str(round(ValList[j],5)))
#print("{}, {}".format(i, j))
self.setItem(i,j, self.item)

def contextMenuEvent(self, event):

menu = QMenu(self)
graphAction = menu.addAction("Graph")
compareAction = menu.addAction("Compare")
scatterAction = menu.addAction("Plot types")
aboutAction = menu.addAction("about")
quitAction = menu.addAction("quit")
printAction = menu.addAction("Print Row")
action = menu.exec_(self.mapToGlobal(event.pos()))
if action == quitAction:
qApp.quit()
elif action == printAction:
self.selected = self.selectedItems()
n = len(self.selected)
print("n is {}".format(n))
for i in range(n):
self.selected[i] = str(self.selected[i].text())
for i in range(n):
self.selected[i] = float(self.selected[i])
print(self.selected)
elif action == graphAction:
self.selected = self.selectedItems()
n = len(self.selected)
for i in range(n):
self.selected[i] = str(self.selected[i].text())
for i in range(n):
self.selected[i] = float(self.selected[i])
print("right before plotter called")

self.Graph = Plotter(self.selected, self.ColHeader)

self.vboxRight.addWidget(self.Graph)
else:
print("u clicked something other than quit")

更糟糕的是,PyQt 将我的所有错误捕获为异常,因此我得到的所有错误都是“进程已完成,退出代码 1”

如果您需要进一步引用我的完整代码,我在此处提供了链接:https://github.com/Silvuurleaf/Data-Analysis-and-Visualization-GUI/blob/master/Plotter3.1.py

谢谢你们,我感谢你们能给我的任何帮助。

最佳答案

为了共享数据,不需要继承小部件,只需使用信号,这是 PyQt 中异步共享数据的自然方式。例如,在您的情况下,我们将创建一个名为 dataSignal 的信号,根据您想要使用变量 self.selectedself.selected 的代码观察到的情况。 ColHeader,第一个是 list 类型,第二个是 numpy.ndarray,因此我们将构建信号:

class CreateTable(QTableWidget): #QTableWidget
dataSignal = pyqtSignal(list, np.ndarray)
def __init__(self, Data, row, col, colHeaders, rowHeaders): #Index, ColumnHeaders
super(CreateTable, self).__init__()


self.setSelectionBehavior(self.SelectRows)

print("Start initialization")
self.ColHeader = colHeaders
self.setRowCount(row)
self.setColumnCount(col)
self.data = Data
self.setHorizontalHeaderLabels(colHeaders)

print("Right before for loop")

n = len(Data)
m = len(colHeaders)

for i in range(n):
DataValues = self.data.iloc[i,:]
print("values are {}".format(DataValues))
#m = len(values)
ConvertedVals = pd.to_numeric(DataValues)

ValList = DataValues.values.tolist()
print(ValList)

for j in range(0,m):
self.item = QTableWidgetItem(str(round(ValList[j],5)))
#print("{}, {}".format(i, j))
self.setItem(i,j, self.item)

def contextMenuEvent(self, event):

menu = QMenu(self)
graphAction = menu.addAction("Graph")
compareAction = menu.addAction("Compare")
scatterAction = menu.addAction("Plot types")
aboutAction = menu.addAction("about")
quitAction = menu.addAction("quit")
printAction = menu.addAction("Print Row")
action = menu.exec_(self.mapToGlobal(event.pos()))
if action == quitAction:
qApp.quit()
elif action == printAction:
self.selected = self.selectedItems()
n = len(self.selected)
print("n is {}".format(n))
for i in range(n):
self.selected[i] = str(self.selected[i].text())
for i in range(n):
self.selected[i] = float(self.selected[i])
print(self.selected)
elif action == graphAction:
self.selected = self.selectedItems()
n = len(self.selected)
for i in range(n):
self.selected[i] = str(self.selected[i].text())
for i in range(n):
self.selected[i] = float(self.selected[i])
print("right before plotter called")

print(type(self.selected), type(self.ColHeader))
self.dataSignal.emit(self.selected, self.ColHeader)

else:
print("u clicked something other than quit")

然后在 MainWindow 类中我们创建一个插槽,在其中创建 Plotter 对象并将其添加到布局中。

def dataPlotter(self, x_data,y_data):
self.Graph = Plotter(x_data, y_data)
self.vboxRightBottom.addWidget(self.Graph)

为此,我们在创建 CreateTable 对象时连接信号:

self.Table = CreateTable(self.BaseStats, row, col, colHeaders, rowHeaders)
self.Table.dataSignal.connect(self.dataPlotter)
self.vboxRightBottom.addWidget(self.Table)

完整代码为here

关于python - 如何继承 QTableWidget 和我的类 MainWindow?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45242591/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com