我决定在我的一个脚本中添加一个GUI.该脚本是一个简单的Web scraper.我决定使用工作线程作为下载和解析数据可能需要一段时间.我决定使用PySide,但我对Qt的了解非常有限.
由于脚本应该在遇到验证码时等待用户输入,我决定它应该等到QLineEdit发生火灾returnPressed然后将其内容发送到工作线程,以便它可以发送它进行验证.这应该比忙碌更好 - 等待按下返回键.
似乎等待信号并不像我想象的那样直截了当,在搜索了一段时间后,我遇到了几个类似于此的解决方案.跨线程的信令和工作线程中的本地事件循环使我的解决方案有点复杂.
经过几个小时的修补后,它仍然无法正常工作.
应该发生什么:
QEventLoop通过调用开始self.loop.exec_()QEventLoop通过调用loop.quit()在一个工作线程,其经由连接槽self.line_edit.returnPressed.connect(self.worker.stop_waiting)在所述main_window类怎么了:
...往上看...
退出QEventLoop不起作用.调用后self.loop.isRunning()返回.返回,因此线程在奇怪的情况下似乎没有死亡.线程仍然停在线上.因此,即使事件循环告诉我它不再运行,线程也会执行事件循环.Falseexit()self.isRunningTrueself.loop.exec_()
GUI响应工作线程类的插槽.我可以看到文本beeing发送到工作线程,事件循环的状态和线程本身,但在上面提到的行执行后没有任何内容.
代码有点复杂,因此我添加了一些伪代码-python-mix而忽略了不重要的:
class MainWindow(...):
# couldn't find a way to send the text with the returnPressed signal, so I
# added a helper signal, seems to work though. Doesn't work in the
# constructor, might be a PySide bug? …Run Code Online (Sandbox Code Playgroud) 我正在使用带有Qt集成的Eclipse的PyDev插件.我安装了PySide,我遇到了SVG图像格式的问题.我知道当我运行我的应用程序时,C:\Python27\Lib\site-packages\PySide\plugins\imageformats找到了位于的格式.除了SVG格式之外的所有格式.我可以删除qico4.dll,它不再找到它们并将其重新放入并再次找到它们.
我在我的代码中使用这一行: plugs = QtGui.QImageReader.supportedImageFormats()
它从qsvg4.dll找到除SVG格式以外的所有格式?为什么会这样?我搜索,搜索和搜索,似乎无法找出原因.格式是否应以支持的图像格式显示?使用SVG图像需要做些什么吗?我可以使用.ico文件很好,需要qico4.dll并位于同一个地方,这就是为什么我不明白问题是什么?任何帮助表示赞赏!
我正在尝试在PyQt中创建一个可编辑的表.这是仅显示表格的代码:
import sys
from PyQt4 import QtGui, QtCore
from PyQt4.QtCore import *
from PyQt4.QtGui import *
# données à représenter
my_array = [['00','01','02'],
['10','11','12'],
['20','21','22']]
def main():
app = QApplication(sys.argv)
w = MyWindow()
w.show()
sys.exit(app.exec_())
# création de la vue et du conteneur
class MyWindow(QWidget):
def __init__(self, *args):
QWidget.__init__(self, *args)
tablemodel = MyTableModel(my_array, self)
tableview = QTableView()
tableview.setModel(tablemodel)
layout = QVBoxLayout(self)
layout.addWidget(tableview)
self.setLayout(layout)
# création du modèle
class MyTableModel(QAbstractTableModel):
def __init__(self, datain, parent = None, *args):
QAbstractTableModel.__init__(self, parent, *args)
self.arraydata …Run Code Online (Sandbox Code Playgroud) 我有一些困难QGraphicsView和QGraphicsScene.当我在场景中缩放/取消缩放并使用mousePressEvent创建项目时,我的位置有一个偏移量.如何避免这种情况?
event.pos() 似乎是问题..
from PyQt4 import QtCore, QtGui
class graphicsItem (QtGui.QGraphicsItem):
def __init__ (self):
super(graphicsItem, self).__init__()
self.rectF = QtCore.QRectF(0,0,10,10)
def boundingRect (self):
return self.rectF
def paint (self, painter=None, style=None, widget=None):
painter.fillRect(self.rectF, QtCore.Qt.red)
class graphicsScene (QtGui.QGraphicsScene):
def __init__ (self, parent=None):
super (graphicsScene, self).__init__ (parent)
class graphicsView (QtGui.QGraphicsView):
def __init__ (self, parent = None):
super (graphicsView, self).__init__ (parent)
self.parent = parent
def mousePressEvent(self, event):
super (graphicsView, self).mousePressEvent(event)
item = graphicsItem()
position = QtCore.QPointF(event.pos()) - item.rectF.center()
item.setPos(position.x() , position.y()) …Run Code Online (Sandbox Code Playgroud) 我是PySide的新手.我有一个主窗口对象,一次显示一个小部件.我一直在尝试更改类的中央窗口小部件,QMainWindow以便在按下按钮时替换窗口中的可见窗口小部件.问题是按下的按钮是在Widget类中,而不是在主窗口类中.
说...
class App(QtGui.QMainWindow):
def __init__(self):
super(App, self).__init__()
self.initUI()
def initUI(self):
self.statusBar().showMessage('Listo.') #Status Bar
self.login_screen = LoginScreen()
self.logged_in_screen = LoggedInScreen()
self.setCentralWidget(self.login_screen)
self.setGeometry(300, 300, 450, 600) #Window Size
self.setWindowTitle('PyTransactio - Client') #Window Title
self.setWindowIcon(QtGui.QIcon('icon.png')) #App Icon
self.show()
Run Code Online (Sandbox Code Playgroud)
按下的按钮位于login_screen实例中.单击按钮时调用的方法位于LoginScreen类中:
def login(self):
""" Send login data to the server in order to log in """
#Process
self.setParent(None)
Run Code Online (Sandbox Code Playgroud)
设置父窗口小部件以从主窗口中None删除窗口小部件(login_screen).logged_in_screen当按下loginButton(在login_screen窗口小部件内)时,我应该怎么做才能将另一个窗口小部件(例如)作为主窗口的中央窗口小部件?
也许登录方法应该在主窗口类中?如果是这样,我如何连接按下的按钮login_screen与主窗口的方法?
我正在使用PyQT 4.8.3创建一个包含两个QListWidgets的对话框,它们都允许多个选择.
我发现如果启用了这些QListWidgets,只有当QListWidget具有焦点时,所选项目才会以蓝色突出显示,而没有焦点,高亮显示为浅灰色.
我还发现如果禁用QListWidgets,尽管没有焦点,所选项目仍以蓝色突出显示.

当用户从一个列表转到另一个列表时,他们会发现这非常令人困惑.
作为开发人员,我发现浅灰色/未聚焦,蓝色/禁用行为是不可取的.我将不胜感激任何有关修改它们的建议.
我已经查看了QListWidget,QListView和QAbstractView的文档而没有找到任何适用的东西,我也查看了样式表文档而没有任何运气.
我用PySide和QML编写了某种安装程序.按设计,它必须是多重的.我想划分插槽从QML表单调用到不同的对象(理想情况下,进入模块,但据我所知,带插槽的对象必须继承QObject,哪些模块不能做).因此,当页面被更改时,'controller'也必须更改,但是,当我使用setContextProperty在QML中显示它时,我遇到了问题.有没有办法从QML调用setContextProperty?或者可能有一些更正确的方法存在?谢谢.
我正在尝试将自定义信号(在TCP客户端类中)连接到使用服务器发送的数据更新日志的方法等等.
这是TCP客户端类的声明:
class CarSocket(QObject):
logSignal = Signal(str, str)
...
def __init__(self, ...):
super(CarSocket, self).__init__()
...
Run Code Online (Sandbox Code Playgroud)
我正在尝试连接的方法logSignal:
def addToLog(self, text, mode='NORMAL'):
if mode == 'RAW':
toAdd = text
else:
toAdd = "<p>{}</p> \n <hr> \n".format(text)
self.log.logEdit.append(toAdd)
Run Code Online (Sandbox Code Playgroud)
所以,我在初始化我的应用程序时写了这一行:
self.carSocket.logSignal.connect(self.addToLog)
Run Code Online (Sandbox Code Playgroud)
当我执行它时,我得到一个非常奇怪的错误:
Traceback (most recent call last):
File "/home/ahmed/workspace/autonomee/main.py", line 286, in <module>
window = MainWindow()
File "/home/ahmed/workspace/autonomee/main.py", line 115, in __init__
self.carSocket.logSignal.connect(self.addToLog)
TypeError: connect() takes exactly 3 arguments (4 given)
[Finished in 0.5s with exit code 1] …Run Code Online (Sandbox Code Playgroud) 在尝试确定事件功能中按下了哪个鼠标按钮时,我在PySide中遇到了问题.我特别需要它来忽略鼠标移动事件,因为它在左右两个鼠标按钮上都有效.
如果按下场景上的右键,我想忽略鼠标移动事件.有帮助吗?
我目前正在从PyQt切换到PySide.
使用PyQt我使用在SO上找到的代码转换QImage为a :Numpy.Array
def convertQImageToMat(incomingImage):
''' Converts a QImage into an opencv MAT format '''
incomingImage = incomingImage.convertToFormat(4)
width = incomingImage.width()
height = incomingImage.height()
ptr = incomingImage.bits()
ptr.setsize(incomingImage.byteCount())
arr = np.array(ptr).reshape(height, width, 4) # Copies the data
return arr
Run Code Online (Sandbox Code Playgroud)
但是ptr.setsize(incomingImage.byteCount())不能与PySide一起使用,因为这是PyQt void*支持的一部分.
我的问题是:如何将QImage转换为Numpy.Array使用PySide.
编辑:
Version Info
> Windows 7 (64Bit)
> Python 2.7
> PySide Version 1.2.1
> Qt Version 4.8.5
Run Code Online (Sandbox Code Playgroud)