如何在PyQt5中发出dataChanged(How to emit dataChanged in PyQt5)
下面的代码在self.emit行中断开。 它在PyQt4中工作正常。 如何解决这个代码,使它在PyQt5中工作?
from PyQt5 import QtCore, QtGui, QtWidgets from PyQt5.QtCore import QObject, pyqtSignal class ItemDelegate(QtWidgets.QItemDelegate): def __init__(self, parent): QtWidgets.QItemDelegate.__init__(self, parent) def createEditor(self, parent, option, index): return QtWidgets.QLineEdit() @QtCore.pyqtSlot() def setModelData(self, editor, model, index): self.emit(QtCore.SIGNAL("dataChanged(QModelIndex,QModelIndex)"), index, index)
稍后编辑:
工作解决方案:
from PyQt5 import QtCore, QtGui, QtWidgets from PyQt5.QtCore import QObject, pyqtSignal class Communicate(QObject): data_changed = pyqtSignal(QtCore.QModelIndex, QtCore.QModelIndex) class ItemDelegate(QtWidgets.QItemDelegate): def __init__(self, parent): QtWidgets.QItemDelegate.__init__(self, parent) self.c = Communicate() @QtCore.pyqtSlot() def setModelData(self, editor, model, index): self.c.data_changed.emit(index, index)
The code below breaks on self.emit line. It works fine in PyQt4. How to fix this code so it works in PyQt5?
from PyQt5 import QtCore, QtGui, QtWidgets from PyQt5.QtCore import QObject, pyqtSignal class ItemDelegate(QtWidgets.QItemDelegate): def __init__(self, parent): QtWidgets.QItemDelegate.__init__(self, parent) def createEditor(self, parent, option, index): return QtWidgets.QLineEdit() @QtCore.pyqtSlot() def setModelData(self, editor, model, index): self.emit(QtCore.SIGNAL("dataChanged(QModelIndex,QModelIndex)"), index, index)
Edited later:
A working solution:
from PyQt5 import QtCore, QtGui, QtWidgets from PyQt5.QtCore import QObject, pyqtSignal class Communicate(QObject): data_changed = pyqtSignal(QtCore.QModelIndex, QtCore.QModelIndex) class ItemDelegate(QtWidgets.QItemDelegate): def __init__(self, parent): QtWidgets.QItemDelegate.__init__(self, parent) self.c = Communicate() @QtCore.pyqtSlot() def setModelData(self, editor, model, index): self.c.data_changed.emit(index, index)
原文:https://stackoverflow.com/questions/38177144
最满意答案
是的,有一种方法可以在不从通用基类继承参数的情况下做到这一点。 它没有很好的记录,只是在IDynamicParameters.GetDynamicParameters方法的评论中暗示。 以下是对该主题的更详细的处理。
首先,使用[Parameter]属性声明属性的公共参数来创建一个类:
internal class MyCommonParmeters { [Parameter] public string Foo { get; set; } [Parameter] public int Bar { get; set; } ... }
然后,每个想要使用这些公共参数的Cmdlet都应该实现IDynamicParameters接口,以返回MyCommonParameters类的成员实例:
[Cmdlet(VerbsCommon.Add, "Froz")] public class AddFroz : PSCmdlet, IDynamicParameters { private MyCommonParmeters MyCommonParameters = new MyCommonParmeters(); object IDynamicParameters.GetDynamicParameters() { return this.MyCommonParameters; } ...
通过这种方法,PowerShell命令参数绑定器将查找并填充MyCommonParameters实例上的参数,就像它们是Cmdlet类的成员一样。
Yes, there is a way to do this without inheriting from a common base class for the parameters. Its not not well documented, only hinted at in the IDynamicParameters.GetDynamicParameters method's remarks. Here's a more detailed treatment of the topic.
First, create a class with your common parameters declared as properties with the [Parameter] attributes:
internal class MyCommonParmeters { [Parameter] public string Foo { get; set; } [Parameter] public int Bar { get; set; } ... }
Then each Cmdlet that wants to use these common parameters should implement the IDynamicParameters interface to return a member instance of the MyCommonParameters class:
[Cmdlet(VerbsCommon.Add, "Froz")] public class AddFroz : PSCmdlet, IDynamicParameters { private MyCommonParmeters MyCommonParameters = new MyCommonParmeters(); object IDynamicParameters.GetDynamicParameters() { return this.MyCommonParameters; } ...
With this approach, the PowerShell command parameter binder will find and populate the parameters on the MyCommonParameters instance just as if they were members of the Cmdlet classes.
相关问答
更多-
是的,有一种方法可以在不从通用基类继承参数的情况下做到这一点。 它没有很好的记录,只是在IDynamicParameters.GetDynamicParameters方法的评论中暗示。 以下是对该主题的更详细的处理。 首先,使用[Parameter]属性声明属性的公共参数来创建一个类: internal class MyCommonParmeters { [Parameter] public string Foo { get; set; } [Parameter] publ ...
-
这里的关键是首先写一个可以从.Net访问的API,然后你可以很容易地将对这个API的调用封装到一个暴露了PowerShell cmdlet类的DLL中。 如果您希望某人能够远程管理您的服务,那么我认为最好的做法是创建一个web服务或WCF服务来完成此操作,然后您可以将其包装在PowerShell中。 如果你看看codeplex,你会发现一些管理远程基于Web的服务的例子,例如GoGrid和SQL Data Services,它们会给你一些示例代码来帮助你入门。 The key here is to star ...
-
测试自己的cmdlet(Test own cmdlets)[2023-04-03]
不是很优雅。 但仍然比什么都没有: 这里 。 Not very elegant. But still more than nothing: here. -
Primalscript / Primalforms无法识别powershell cmdlet(Primalscript/Primalforms not recognizing powershell cmdlets)[2022-03-11]
Get-CimInstance需要PowerShell版本3及更高版本...请确保在PowerShell版本菜单中进行设置 另一个选项(如果你的PowerShell版本是2或更少)是使用Get-WmiObject而不是Get-CimInstance所以它应该如下所示: $freeRam = Get-WmiObject -ClassName win32_operatingsystem | Select-Object -expand FreePhysicalMemory Get-CimInstance Re ... -
是的,我在这里再次回答我自己的问题(而不是删除我的帖子,因为它可以帮助将来的某个人)。 似乎属性名称区分大小写,因此我需要替换: $ _。samaacountname 同 $ _。SAM帐户 之后就像一个魅力 Right, here I am answering my own question again (rather than deleting my post, incase it helps someone in the future). It seems the property name is c ...
-
您需要取消注释ISS的内容。 做这样的事情: $iss = [System.Management.Automation.Runspaces.InitialSessionState]::CreateDefault() $iss.ImportPSModule($module) 将$ module替换为Exchange模块的名称,或者仅使用模块名称填充变量。 然后在创建RunspacePool时执行以下操作: $runspacePool = [System.Management.Automation.Runsp ...
-
O, 我认为可能对您有很大价值的是PowerShell帮助系统。 这是最好的之一! 只需使用Get-Help about_commonparameters就可以发现PowerShell中每个cmdlet都可以使用的所有常用参数 会给你一个关于以下参数的好主意: -Debug -ErrorAction -ErrorVariable -InformationAction -InformationVariable -OutVariable -OutBuffer -PipelineVariable -Verbose ...
-
是否有可能/如何使用某些cmdlet停止PowerShell?(Is it possible to/how do you stop powershell using certain cmdlets?)[2023-07-15]
没有必要在powershell中关闭cmdlet,因为这会破坏拥有它的原因。 无论如何,大多数“正常”的东西都存在,你可以使用get-alias命令找到它。 C:\> get-alias CommandType Name ----------- ---- Alias % -> ForEach-Object Alias ? -> Where-Object Alias ?? -> Invoke-NullCoalescing Alias ... -
我建议使用PowerShell自己的帮助: Get-Help Get-MailboxStatistics -Full 然后,您可以调查诸如-Server -Identity之类的参数。 -Full参数将为您提供示例。 I suggest using PowerShell's own help with: Get-Help Get-MailboxStatistics -Full Then you can investigate parameters such as -Server -Identity. T ...
-
我找到了一个更简单的解决方案,我相信它可以满足我的需求。 我不使用构造函数,而是使用自定义getter来创建我的计算属性: public class User { public string username {get; set;} public string userprincipalname {get; set;} public string status {get; set;} public string status_value { get ...