PyQt Unbound Method When Passing Argument Between Signal and Slot - qt

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)

Related

error using frequncycounter library in arduino mega

dear
i wrote this code to calculate wind speed from anemometer
below the error message
windspeedcode2:15: error: 'FreqCount' is not a class, namespace, or
enumeration
FreqCount::f_comp= 8; // Set compensation to 12
^
windspeedcode2:16: error: 'FreqCount' is not a class, namespace, or
enumeration
FreqCount::start(100); // Start counting with gatetime of
100ms
^
windspeedcode2:17: error: 'FreqCounter' has not been declared
while (FreqCounter::f_ready == 0) // wait until counter
ready
^
windspeedcode2:19: error: 'FreqCount' is not a class, namespace, or
enumeration
freq=FreqCount::f_freq;//read frequency value
^
exit status 1 'FreqCount' is not a class, namespace, or enumeration
This report would have more information with "Show verbose output
during compilation" option enabled in File -> Preferences.
:: is the scope resolution operator for defining the same function outside the class
. is the dot operator used to call a member function (or member variable) on a object.
An example would be (assuming a instance named FreqCount exist in you library header): FreqCount.f_comp= 8;
Only if f_comp is a static class member variable can it be accessed is you called it: FreqCount::f_comp= 8; But that is unlikely for a library.
So what is in your header and where does the library come from?
thank you for your help the problem has been solved by downloading another library
as shown in picture

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);

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.

Connecting QTableView signal in PySide, wrong parameters?

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'.

using an exists statement in R

I have made a progress bar using the winProgressBar method in R. What I want to do is if someone instantiates my program while it is doing all of its processing, I want the current progress bar to close. I tried using a statement that says
if(exists(progressBar)) {
close(progressBar);
}
but I get an error from the console that says
Error in exists(progressBar) : object 'progressBar' not found
I know that it will not exist during the first iteration of my program, but there is no reason that I can find that would make an if statement cause the program to crash.
If you read the help for exists you will see the following under Arguments
x a variable name (given as a character string).
So
exists('progressBar')
will return TRUE or FALSE.

Resources