Connecting QTableView signal in PySide, wrong parameters? - qt

I have a class that creates a window and a treeview. The code that creates the treeview is pretty simple and is in the init method:
tableView = QTableView()
tableView.setModel(model)
tableView.clicked.connect(self.foo)
Where 'foo' is the name of the function (a member of the same class) that should accept the callback. The function's signature is as follows:
def foo(something):
print something
From what (admittedly little) I understand, the 'something' parameter should've been an instance of QModelIndex, but it isn't. Doing a print(something) on the variable indicated that I've sent foo(...) the window class. What am I missing here? I assumed this was the right way to do this, based on:
http://qt-project.org/wiki/Signals_and_Slots_in_PySide
Any ideas?
Thank you for your help.

First argument to a method is the instance itself which is passed implicitly and generally named as self. In your version, something becomes the instance, not the passed parameter. Your method should look like:
def foo(self, something):
print something
As a side note, normally you would get an error while passing a parameter to a method that doesn't accept any. Like:
class Foo(object):
def bar(something):
print something
f = Foo()
f.bar(1)
#Traceback (most recent call last):
# File "<stdin>", line 1, in <module>
#TypeError: bar() takes exactly 1 argument (2 given)
But in Qt, you can connect a signal to a slot that accepts less parameters. Qt will call the slot without that parameter. So, although the clicked signal passes the QModelIndex, you can still connect this signal to a method that doesn't accept a parameter (like your foo). In result, you'll get this silent 'bug'.

Related

Why can't I call the methods method on a Perl 6's ClassHOW object?

I can call ^methods on an object and list the method names I can call:
my $object = 'Camelia';
my #object_methods = $object.^methods;
#object_methods.map( { .gist } ).sort.join("\n").say;
^methods returns a list which I store in #object_methods, then later I transform that list of method thingys by calling gist on each one to get the human-sensible form of that method thingy.
But, the ^ in ^methods is an implied .HOW, as show at the end of the object documentation this should work too:
my $object = 'Camelia';
my #object_methods = $object.HOW.methods;
But, I get an error:
Too few positionals passed; expected 2 arguments but got 1
in any methods at gen/moar/m-Metamodel.nqp line 490
in block <unit> at...
And, for what it's worth, this is an awful error message for a language that's trying to be person-friendly about that sort of thing. The file m-Metamodel.nqp isn't part of my perl6 installation. It's not even something I can google because, as the path suggests, it's something that a compilation generates. And, that compilation depends on the version.
A regular method call via . passes the invocant as implicit first argument to the method. A meta-method call via .^ passes two arguments: the meta-object as invocant, and the instance as first positional argument.
For example
$obj.^can('sqrt')
is syntactic sugar for
$obj.HOW.can($obj, 'sqrt')
In your example, this would read
my #object_methods = $object.HOW.methods($object);

Python function object on Map function going weird. (Spark)

I have a dictionary that maps a key to a function object. Then, using Spark 1.4.1 (Spark may not even be relevant for this question), I try to map each object in the RDD using a function object retrieved from the dictionary (acts as look-up table). e.g. a small snippet of my code:
fnCall = groupFnList[0].fn
pagesRDD = pagesRDD.map(lambda x: [x, fnCall(x[0])]).map(shapeToTuple)
Now, it has fetched from a namedtuple the function object. Which I temporarily 'store' (c.q. pointing to fn obj) in FnCall. Then, using the map operations I want the x[0] element of each tuple to be processed using that function.
All works fine and good in that there indeed IS a fn object, but it behaves in a weird way.
Each time I call an action method on the RDD, even without having used a fn obj in between, the RDD values have changed! To visualize this I have created dummy functions for the fn objects that just output a random integer. After calling the fn obj on the RDD, I can inspect it with .take() or .first() and get the following:
pagesRDD.first()
>>> [(u'myPDF1.pdf', u'34', u'930', u'30')]
pagesRDD.first()
>>> [(u'myPDF1.pdf', u'23', u'472', u'11')]
pagesRDD.first()
>>> [(u'myPDF1.pdf', u'4', u'69', u'25')]
So it seems to me that the RDD's elements have the functions bound to them in some way, and each time I do an action operation (like .first(), very simple) it 'updates' the RDD's contents.
I don't want this to happen! I just want the function to process the RDD ONLY when I call it with a map operation. How can I 'unbind' this function after the map operation?
Any ideas?
Thanks!
####### UPDATE:
So apparently rewriting my code to call it like pagesRDD.map(fnCall) should do the trick, but why should this even matter? If I call
rdd = rdd.map(lambda x: (x,1))
rdd.first()
>>> # some output
rdd.first()
>>> # same output as before!
So in this case, using a lambda function it would not get bound to the rdd and would not be called each time I do a .take()-like action. So why is that the case when I use a fn object INSIDE the lambda? Logically it just does not make sense to me. Any explanation on this?
If you redefine your functions that their parameter is an iterable. Your code should look like this.
pagesRDD = pagesRDD.map(fnCall).map(shapeToTuple)

robotframework is adding arguments where there are none: Type Error: takes no arguments (1 given)

Robot is telling me that I'm providing too many arguments to my keyword. I've boiled it down to a base case where I have a keyword that should do nothing:
def do_nothing():
"""
Does absolutly nothing
"""
Calling this keywork like this:
*** Test Cases ***
testCaseOne
do_nothing
Give this result:
TypeError: do_nothing() takes no arguments (1 given)
Adding a parameter to the keyword definition fixes the problem. Why does robot seem to pass 1 parameter to each keyword, even if there are no parameters in the test case?
I found the answer here.
The issue has nothing to do with the robotframework, and has every thing to do with Python; Python implicitly passes the current instance of the class to method calls, but I needed to explicitly declare the parameter. This is customarily named self:
def do_nothing(self):
This test runs.

Why to pass a template parameter in Qt method

I'm trying to read some example code about implicit creation of QVariants from enum values.
About the following line of code:
QVariant::fromValue<Qt::PenStyle>(Qt::SolidLine)
I don't really understand what is the purpose of Qt::PenStyle in the above expression.
I think Qt::SolidLine is unique.
The syntax is OK?
Shouldn't it be something like:
QVariant::fromValue(Qt::SolidLine)
?
Sorry if this question seems dumb.
You can use this form:
1) QVariant::fromValue(Qt::SolidLine)
QVariant::fromValue(const T & value) is a template method. When you call a template method or function you can specify for what type of argument this method should be called. If you don't do that a compiler tries to do it for you. That is why 1) is equal to this:
2) QVariant::fromValue<Qt::PenStyle>(Qt::SolidLine)
But you can call this method for int and pass enum value (if you are not at c++11):
3) QVariant::fromValue<int>(Qt::SolidLine)
or even force creating of QPen:
4) QVariant::fromValue<QPen>(Qt::SolidLine)
EDIT:
If someone is suprised by 4 and want to know how it works: it is the same as if there was a method (actually it is created during the compilation):
QVariant::fromValue(const QPen& pen);
When you call this method with Qt::SolidLine compiler uses an implicit constructor QPen(Qt::PenStyle style) to create a new temporary QPen object and pass it as an argument to the method fromValue.

PyQt Unbound Method When Passing Argument Between Signal and Slot

I am working on a project in which I need to download and parse an XML file from a network location. I've been trying to use the QtNetwork module to accomplish this, but I'm running into a seemingly simple problem that I've spent many hours trying to solve. My code is as follows:
class NetworkAccessor(QObject):
done = False
def importXml(self):
self.manager = QNetworkAccessManager()
self.manager.finished.connect(self.fileReady(QNetworkReply))
self.manager.get(QNetworkRequest(QUrl("http://192.168.5.243/details.xml")))
def fileReady(self, response):
self.f = QTemporaryFile()
print type(response)
self.f.write(response.readAll())
self.done = True
print "Done"
When I instantiate the NetworkAccessor class and call importXml, I get the following error:
Traceback (most recent call last):
File "C:/SVN-Local/Thermal/PyQtTestTemplate.py", line 40, in updateUi
f = networkAccessor.importXml()
File "C:/SVN-Local/Thermal/PyQtTestTemplate.py", line 14, in importXml
self.connect(self.manager,SIGNAL("finished(QNetworkReply*)"),self.fileReady(QNetworkReply))
File "C:/SVN-Local/Thermal/PyQtTestTemplate.py", line 20, in fileReady
self.f.write(response.readAll())
TypeError: QIODevice.readAll(): first argument of unbound method must have type 'QIODevice'
It seems to indicate that the argument passed to the fileReady method is not instantiated. Furthermore, the print type(response) statement above indicates that response is of type PyQt4.QtNetwork.QNetworkReply.
I've tried various ways of connecting the signal to the slot including the "old fashioned" way: self.connect(self.manager,SIGNAL("finished(QNetworkReply*)"),self.fileReady("QNetworkReply")) as well as using short-circuit parameter passing with no success.
Can somebody show me the proper way to pass an instantiated instance of a QNetworkReply object from a signal to a slot?
Thanks.
Just pass it the slot, the QNetworkReply object will get passed through by the signal:
self.manager.finished.connect(self.fileReady)

Resources