我有一个布局,需要将小部件向上推。我知道我能做到
layout = QtWidgets.QVBoxLayout()
layout.addWidget(test_widget)
layout.addStretch()
Run Code Online (Sandbox Code Playgroud)
addStretch 会将所有小部件推到顶部。但是,我使用的布局是 QGridLayout 并且 .addSretch() 不适用于网格布局。如何填充窗口底部的所有空白空间,并使用网格布局将所有小部件推到顶部?
我只是尝试在 Windows 10 64 位版本上测试新发布的 PySide2 (5.11) 版本。但是“Hello World”示例不起作用。我在 PyCharm 中使用 Python 3.6。我使用的解释器来自 Anaconda。所以我 pip 安装了 PySide2 版本,并尝试通过“conda install ...”安装旧版本的 PySide2。两个安装都有效,但我收到两个库的相同错误消息。
错误消息在一个单独的屏幕中弹出:“此应用程序无法启动,因为无法初始化 Qt 平台插件。重新安装应用程序可能会解决此问题。可用的平台插件是:minimal、offscreen、windows。”
所以我绝对可以在正确的文件夹中看到插件文件。我尝试重新安装。删除所有内容并尝试其他IDE。但没有什么能解决问题。
任何帮助表示赞赏。
我想在我的 pyside2 应用程序中获取鼠标位置。(不是 QCursor 提供的桌面鼠标位置),我尝试了两种方法。波纹管是我的代码。
import sys
from PySide2 import QtGui, QtWidgets, QtCore
class Palette(QtWidgets.QGraphicsScene):
def __init__(self, parent=None):
super().__init__(parent)
def mousePressEvent(self, event):
print(event.pos()) # always return (0,0)
print(QtWidgets.QWidget.mapToGlobal(QtCore.QPoint(0, 0))) #makes parameter type error
print(QtWidgets.QWidget.mapToGlobal(QtWidgets.QWidget)) # makes parameter type error
print(QtWidgets.QWidget.mapToGlobal(QtWidgets.QWidget.pos())) # makes parameter type error
class MainWindow(QtWidgets.QMainWindow):
def __init__(self):
super().__init__()
palette = Palette(self)
view = QtWidgets.QGraphicsView(palette, self)
view.resize(500, 500)
if __name__ == "__main__":
app = QtWidgets.QApplication(sys.argv)
main_window = MainWindow()
main_window.resize(500, 500)
main_window.show()
app.exec_()
Run Code Online (Sandbox Code Playgroud)
我很想知道如何让我的鼠标定位...
我想将 PyQtGraph PlotWidget 实现到 PySide2 应用程序中。使用 PyQt5 一切正常。使用 PySide2 我得到底部显示的错误。我已经发现,有一些工作正在进行中,但似乎也有一些人设法使这项工作正常进行。但是,我还做不到。我使用的是 Pyqtgraph 0.10 而不是开发者分支。我要不要改变?我需要做什么?
from PySide2.QtWidgets import QApplication, QMainWindow, QGraphicsView, QVBoxLayout, QWidget
import sys
import pyqtgraph as pg
class WdgPlot(QWidget):
def __init__(self, parent=None):
super(WdgPlot, self).__init__(parent)
self.layout = QVBoxLayout(self)
self.pw = pg.PlotWidget(self)
self.pw.plot([1,2,3,4])
self.pw.show()
self.layout.addWidget(self.pw)
self.setLayout(self.layout)
if __name__ == '__main__':
app = QApplication(sys.argv)
w = WdgPlot()
w.show()
sys.exit(app.exec_())
Run Code Online (Sandbox Code Playgroud)
错误:
QtGui.QGraphicsView.__init__(self, parent)
TypeError: arguments did not match any overloaded call:
QGraphicsView(parent: QWidget = None): argument 1 has unexpected type 'WdgPlot'
QGraphicsView(QGraphicsScene, parent: QWidget …Run Code Online (Sandbox Code Playgroud) 我正在尝试使用 Python 创建新的 QML 类型,但在注册 QML 类型时遇到问题。但是,我收到一个错误:
TypeError: 'PySide2.QtQml.qmlRegisterType' called with wrong argument types:
PySide2.QtQml.qmlRegisterType(module, str, int, int, str)
Supported signatures:
PySide2.QtQml.qmlRegisterType(type, str, int, int, str)
Run Code Online (Sandbox Code Playgroud)
所以我知道它期待一种类型,但是,在这篇博文中它做了类似的事情:
qmlRegisterType(PieChart, 'Charts', 1, 0, 'PieChart')
Run Code Online (Sandbox Code Playgroud)
在我的 main.py 中,我有这个:
...
if __name__ == '__main__':
# Declare QApplication
app=QApplication([])
qmlRegisterType(CamFeed, 'CFeed', 1, 0, 'CamFeed')
...
Run Code Online (Sandbox Code Playgroud)
CamFeed.py 看起来像这样:
from PySide2.QtQuick import QQuickPaintedItem
from PySide2.QtGui import QPainter
from PySide2.QtCore import QObject
class CamFeed(QQuickPaintedItem):
def __init__(self, parent=None):
super().__init__(parent)
# Re-implementation of the virtual function
def …Run Code Online (Sandbox Code Playgroud) 我正在尝试实现一个小部件(一个临时工具箱),它将放置在场景的右侧。它应该看起来像这样:

在移动和缩放视图时将其正确放置在那里并保持相同的大小和位置是一个问题。
小部件本身是一个 QComboBox。我尝试将其作为 QGraphicsWidget 放入场景中,将 ZValue 设置为较高的数字并将其设置为 QGraphicsLinearLayout。不幸的是,我无法将其与左侧对齐,而且我觉得我的做法是错误的。
这是一个草图:
from PySide2 import QtWidgets, QtCore, QtGui
import sys
def hex2QColor(color):
r = int(color[0:2], 16)
g = int(color[2:4], 16)
b = int(color[4:6], 16)
return QtGui.QColor(r, g, b)
class rotateGroupBox(QtWidgets.QGroupBox):
"""Create content to fill the widget"""
def __init__(self, parent=None):
super(rotateGroupBox, self).__init__(parent)
self.setTitle("Rotation")
self.l_rotate = QtWidgets.QSpinBox()
self.l_rotate.setRange(0, 360)
self.r_rotate = QtWidgets.QSpinBox()
self.r_rotate.setRange(0, 360)
self.l_label = QtWidgets.QLabel("Left: ")
self.r_label = QtWidgets.QLabel("Right: ")
layout = QtWidgets.QVBoxLayout()
l1 = QtWidgets.QHBoxLayout()
l1.setContentsMargins(0,0,0,0)
l1.addWidget(self.l_label)
l1.addWidget(self.l_rotate)
l2 = QtWidgets.QHBoxLayout() …Run Code Online (Sandbox Code Playgroud) 我正在尝试创建一个自定义QQuickItem来在我的应用程序中显示图表。我的 C++ 版本可以工作,但我的 Python 版本无法工作。我相信这是因为Q_DECLARE_METATYPE和qRegisterMetaType没有移植到 PySide2。虽然我下面的示例没有演示这一点,但我的要求是饼图不是静态的(它可以随时更改),并且我可以拥有可以在运行时添加或删除的任意数量的饼图。
我的程序将运行,但它不显示任何内容,并且我在控制台上收到以下错误:
QMetaProperty::read: Unable to handle unregistered datatype 'QAbstractListModel*' for property 'CustomPieChart::model'
QMetaProperty::read: Unable to handle unregistered datatype 'QAbstractListModel*' for property 'CustomPieChart::model'
qrc:/PieChartView.qml:24:17: Unable to assign [undefined] to QAbstractItemModel*
Run Code Online (Sandbox Code Playgroud)
我的代码的精简版本如下:
主要.py
QMetaProperty::read: Unable to handle unregistered datatype 'QAbstractListModel*' for property 'CustomPieChart::model'
QMetaProperty::read: Unable to handle unregistered datatype 'QAbstractListModel*' for property 'CustomPieChart::model'
qrc:/PieChartView.qml:24:17: Unable to assign [undefined] to QAbstractItemModel*
Run Code Online (Sandbox Code Playgroud)
饼图模型.py
import sys
from PySide2.QtCore import QUrl
from PySide2.QtGui import QIcon …Run Code Online (Sandbox Code Playgroud) 设置背景颜色很好,但我的界面有一个 Qwidget,它会在某个时刻更改其背景颜色。这将导致程序崩溃,当我注释掉颜色改变代码时崩溃停止。
下面的代码在运行一段时间后肯定会崩溃。
import sys
from PySide2.QtWidgets import QWidget,QPushButton,QApplication,QListWidget,QGridLayout,\
QLabel,QMainWindow,QLineEdit,QScrollArea,QVBoxLayout,QMessageBox
from PySide2.QtCore import QTimer,QDateTime,QSize,Qt
from PySide2.QtGui import *
import requests,json
import configparser #
import os
import time
import threading #
from pynput import keyboard #
import requests,json
from math import *
from functools import partial
class MainWindow(QMainWindow):
def __init__(self):
super(MainWindow, self).__init__()
self.resize(320,100)
self.setWindowTitle('aaaa')
self.setStyleSheet("QMainWindow\
{\
background-color:white;\
color:white;\
margin:0px\
}")
self.InitializeWindow()
self.i=0
def InitializeWindow(self):
self.statebar=QWidget(self)
self.statebar.setGeometry(0, 40, 320, 54)
print(sys._getframe().f_lineno)
self.statebar.setStyleSheet("QWidget{background-color:#1EC5CD}")
self.th_countdown=threading.Thread(target=self.countdown,args=())
self.th_countdown.setDaemon(True)
self.th_countdown.start()
self.message_main=QLabel(self.statebar)
self.message_main.setGeometry(150,7,40,37)
self.message_main.setStyleSheet("background:transparent;color:white;margin:0px;font-size:45px;font-weight:bold")
self.message_main.setWordWrap(True)
self.message_main.hide()
self.message_main.setAlignment(Qt.AlignTop|Qt.AlignLeft)
self.message_signature=QLabel(self.statebar) …Run Code Online (Sandbox Code Playgroud) 我试图在 a 上绘制一些东西QPixmap,但它在清理过程中抛出错误。
from PySide2.QtGui import QPixmap, QPainter
from PySide2.QtWidgets import QApplication
app = QApplication()
width = 200
height = 100
pixmap = QPixmap(width, height)
painter = QPainter(pixmap)
painter.drawLine(0, 0, 100, 100)
print('Done.')
Run Code Online (Sandbox Code Playgroud)
当我运行该程序时,我看到“完成”消息,然后出现错误。
Done.
QPaintDevice: Cannot destroy paint device that is being painted
Process finished with exit code 139 (interrupted by signal 11: SIGSEGV)
Run Code Online (Sandbox Code Playgroud)
我见过一些具有相同错误的类似问题,但不清楚是什么导致了错误或如何避免它。
我有一个主窗口,按下按钮即可打开一个对话框。我想覆盖对话框的接受功能,以便我可以实现一些应在接受对话框之前执行的自定义功能。这是我的主窗口的示例代码。
from PySide2 import QtCore, QtGui, QtWidgets
from add_new import Add_new_dialog
import sys
class Ui_MainWindow(object):
def setupUi(self, MainWindow):
MainWindow.setObjectName("MainWindow")
MainWindow.resize(500, 100)
self.centralwidget = QtWidgets.QWidget(MainWindow)
self.centralwidget.setObjectName("centralwidget")
self.pushNew_Bed = QtWidgets.QPushButton(self.centralwidget)
self.pushNew_Bed.setObjectName("pushNew_Bed")
self.pushNew_Bed.resize(QtCore.QSize(500,100))
self.pushNew_Bed.clicked.connect(self.on_add_new_clicked)
def retranslateUi(self, MainWindow):
_translate = QtCore.QCoreApplication.translate
MainWindow.setWindowTitle(_translate("MainWindow", "MainWindow"))
self.pushNew_Bed.setText(_translate("MainWindow", "Add New Bed"))
def on_add_new_clicked(self):
Dialog = QtWidgets.QDialog()
Dialog.ui = Add_new_dialog()
Dialog.ui.setupUi(Dialog)
#dialog.setAttribute(QtCore.Qt.WA_DeleteOnClose)
retValue = Dialog.exec_()
if retValue == 1:
print("Accepted")#dialog.calendarWidget.selectedDate())
if __name__ == "__main__":
app = QtWidgets.QApplication(sys.argv)
MainWindow = QtWidgets.QMainWindow()
ui = Ui_MainWindow()
ui.setupUi(MainWindow)
MainWindow.show()
sys.exit(app.exec_())
Run Code Online (Sandbox Code Playgroud)
这是对话框。
from PySide2 …Run Code Online (Sandbox Code Playgroud)