将 pyqtgraph 添加到 PyQt6

问题描述 投票:0回答:2

所以我正在使用 Pyqt6 制作 GUI,并希望在 Gui 内添加图形和其他数据,所以当我尝试添加图形时,我收到以下错误:

调用:addWidget(self,QWidget):参数1具有意外类型'PlotWidget'addWidget(self,QWidget,int,int,alignment:Qt.AlignmentFlag = Qt.Alignment()):参数1具有意外类型'PlotWidget'addWidget (self,QWidget,int,int,int,int,alignment:Qt.AlignmentFlag = Qt.Alignment()):参数1具有意外类型'PlotWidget'

我有代码

    self.plt=pyqtgraph.PlotWidget()
    self.plt.plot([1,2,3,4,5],[1,2,3,4,5])
    grid.addWidget(self.plt, 6, 1, 3, 3)
    self.setLayout(grid)  #up I have grid=QGridLayout()
pyqtgraph pyqt6
2个回答
1
投票

我尝试重现您的问题有一段时间了。
我在同一环境下安装PyQt5和PyQt6后成功了。

正如 @musicamante 指出的,首先导入 PyQt6 并仅在 pyqtgraph 之后导入非常重要。 否则 QT_LIB 未正确设置,只能从环境中已安装的软件包中猜测 PyQt 版本。


-1
投票

我有同样的问题,但是在 pyqtgraph 之前导入 PyQt6 不起作用。 你知道这里可能有什么问题吗? 当使用 PyQt5 时,它可以完美工作。

我用这段代码尝试过:

from PyQt6.QtWidgets import*

import sys
import pyqtgraph as pg


class Window(QMainWindow):

    def __init__(self):
        super().__init__()

        # setting title
        self.setWindowTitle("PyQtGraph")

        # setting geometry
        self.setGeometry(100, 100, 600, 500)

        # calling method
        self.UiComponents()

        # showing all the widgets
        self.show()

    # method for components
    def UiComponents(self):

        # creating a widget object
        widget = QWidget()

        # creating a push button object
        btn = QPushButton('Push Button')

        # creating a line edit widget
        text = QLineEdit("Line Edit")

        # creating a check box widget
        check = QCheckBox("Check Box")

        # creating a plot window
        plot = pg.plot()

        # create list for y-axis
        y1 = [5, 5, 7, 10, 3, 8, 9, 1, 6, 2]

        # create horizontal list i.e x-axis
        x = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]

        # create pyqt5graph bar graph item
        # with width = 0.6
        # with bar colors = green
        bargraph = pg.BarGraphItem(x = x, height = y1, width = 0.6, brush ='g')

        # add item to plot window
        # adding bargraph item to the plot window
        plot.addItem(bargraph)

        # Creating a grid layout
        layout = QGridLayout()

        # setting this layout to the widget
        widget.setLayout(layout)

        # adding widgets in the layout in their proper positions
        # button goes in upper-left
        layout.addWidget(btn, 0, 0)

        # text edit goes in middle-left
        layout.addWidget(text, 1, 0)

        # check box widget goes in bottom-left
        layout.addWidget(check, 3, 0)

        # plot window goes on right side, spanning 3 rows
        layout.addWidget(plot, 0, 1, 3, 1)

        # setting this widget as central widget of the main window
        self.setCentralWidget(widget)


# create pyqt5 app
App = QApplication(sys.argv)

# create the instance of our Window
window = Window()

# start the app
sys.exit(App.exec())
© www.soinside.com 2019 - 2024. All rights reserved.