PDA

View Full Version : QSignalMapper



Ali Reza
4th November 2012, 08:51
hi all

i want to make a multi-stream http proxy. i faced to a problem.
my problem is about "look up host"
i used QHostInfo to do this.(function LookupHost(...))
as we know this function emit a signal that have one parameter (QHostInfo &)
but i want connect each stream look up to it's own function
in other words i want this slot:

void LookUpResult(QHostInfo @hi , int id)
that id is the identifier of a specific stream.
how to implement this??
please help

thanks in advance.

Ginsengelf
5th November 2012, 08:11
Hi, I don't think you can do that with QSignalMapper.
As a (little bitugly) work-around you can connect all signals to one slot and use sender() to check which stream sent the signal.

Ginsengelf

Ali Reza
5th November 2012, 08:48
hi Ginsengelf
thanks for replying

please explain more. i do not understand what your mean !

thanks again...

Ali Reza
10th November 2012, 15:43
help me!!!
i do not take my answer.
please offer me a solution... or a new approach or ...

regards

amleto
10th November 2012, 16:24
hi all

i want to make a multi-stream http proxy. i faced to a problem.
my problem is about "look up host"
i used QHostInfo to do this.(function LookupHost(...))
as we know this function emit a signal that have one parameter (QHostInfo &)
but i want connect each stream look up to it's own function
in other words i want this slot:

void LookUpResult(QHostInfo @hi , int id)
that id is the identifier of a specific stream.
how to implement this??
please help

thanks in advance.

just copy the example in the docs

qsignalmapper

Ali Reza
10th November 2012, 16:32
this link get me 404 not found !!!

amleto
10th November 2012, 21:06
changed it.

not sure exactly what you're trying to do, though. There needs to be something that knows the id to use, but you dont show where that is in your code

Ali Reza
11th November 2012, 14:53
how to implement with QSignalMapper ?? (if possible)

amleto
11th November 2012, 19:31
Why do you want

void LookUpResult(QHostInfo hi , int id)

when id = hi.lookupId()?*

*I believe this is true but I'm not certain.

Ali Reza
14th November 2012, 14:50
i want to implement a multi stream HTTP proxy that accept many connections from the browser.
my proxy must look up IP address of the host that specified by each TCP stream (for each http request from browser)
thus i must look up the ip for each stream concurrently
i used QHostInfo ( method LookUpHost() ).
now i want connect the result of this function to my customized slot:

void LookUpResult(QHostInfo @hi , int id)
how ???

thanks

amleto
14th November 2012, 18:00
well, you were given the id earlier when you called lookupHost. so save 'id', and when you receive HostInfo you will be ready to call that slot.

Ali Reza
17th November 2012, 15:33
please explain to me by coding. i do not understand your mean.
thanks

amleto
17th November 2012, 16:33
tell me, what is the difference between

hostinfo.lookupId() and your 'int id' in void LookUpResult(QHostInfo @hi , int id)??

I say there is no difference!

Therefore you don't need


void LookUpResult(QHostInfo @hi , int id)
because you can use


void LookUpResult(QHostInfo hi)
{
int id = hi.lookupId();
// ...
}

Ali Reza
18th November 2012, 09:20
sorry for my bad English.

my 'id' is a identifier of a stream that established by a internet browser.
i don't know the hostinfo.lookuoid() is what !
please tell me the above function what returns.

thanks

Added after 5 minutes:

thanks dear amleto
i understand what you say
my problem solved .

thanks very very much...

amleto
18th November 2012, 10:37
sorry, finally I think I understand your problem. I think what I suggested above might not work as you want.



class MyMapper : QObject
{
// Q_OBJECt etc

public:
MyMapper(int id) : id_(id) {}


public slots:
void myResultLookup(QHostInfo info)
{
//...
resultLookup(info, id_);
deleteLater(); // this object no longer needed?
}

private:
int id_;

};

void YourMethod()
{

int streamId = ...;
QString hostname = ...;
MyMapper* mapper = new MyMapper(streamId);
QHostInfo::lookupHost(hostname, mapper, SLOT(myResultLookup(QHostInfo)));
}

Ali Reza
19th November 2012, 08:54
thanks again ...
solved !

Added after 4 minutes:

hey amleto
excuse me, another problem
is there any way to IP caching in Qt Creator ?

amleto
19th November 2012, 23:36
not sure. probably time for a new thread! :)

Ali Reza
25th November 2012, 16:09
Hey amleto
*** NOT SOLVED ***
your snipped code about Host look up is wrong.
in definition of myResultLookup from Mapper class ,you call resultLookup. but this function is member of another class!!
what can i do?

regards

amleto
26th November 2012, 03:30
My code isn't wrong, it's just not complete. That function comes from your code. you figure out how to pass it in ;)

Ali Reza
26th November 2012, 08:33
excuse me !
i did not understand
1.you define a new class that named "MyMapper" that inherits "QObject". but this definition get me the following error:
" 'QObject' is an inaccessible base of 'MyMapper' ".
2.you call function "resultLookup(info, id_);" in line 13 in your code. but this function is not accessible here (this function is member of another class)

plz tell me how your code is right ?

thanks alot dear amleto !

amleto
26th November 2012, 08:49
what do you think I meant when I wrote

//Q_OBJECT etc


in the class code?

It means you have to fill in some bits for yourself! Now, please go and have a try for yourself!

Ali Reza
26th November 2012, 15:51
excuse me

what about 2'nd problem? (call a function that is member of other class)

my mapper class is :


class LookUpResultHandler : QObject
{
Q_OBJECT;
public:
LookUpResultHandler(int); //constructor

private:
int id;

public slots:
void lookupresult(QHostInfo);

};


and in a cpp file i wrote :


#include "lookupresulthandler.h"

LookUpResultHandler::LookUpResultHandler(int id):id(id)

void LookUpResultHandler::lookupresult(QHostInfo hi)
{
HostLookUpResult(hi,id); //this function is not accessible here because this is member of another class (my main class)
}



and in my main cpp file i wrote :


LookUpResultHandler *lrh = new LookUpResultHandler(id);
QHostInfo::lookupHost(rp.getHost(),lrh,SLOT(lookup result(QHostInfo)));


just according your approach !
BUT give me ERROR !!!
why?????

wysota
26th November 2012, 16:08
BUT give me ERROR !!!
why?????

You answered this question yourself -- because it is not accessible as it is member of another class. Adjust your code accordingly. It's a basic programming issue and you should be able to solve it with basic programming skills.

Ali Reza
26th November 2012, 16:36
hi Mr Wysota

i knew this code is wrong but Mr amleto say this is correct.
i'm wait to amleto to relpy me

thanks Mr wysota

amleto
26th November 2012, 16:41
you already know that method is from another class. So I leave up to you to figure out how to solve this issue.

by the way,


LookUpResultHandler::LookUpResultHandler(int id):id(id)
That is not a valid constructor.

wysota
26th November 2012, 16:51
i knew this code is wrong but Mr amleto say this is correct.
It is correct (as far as the logic is concerned, at least). It is just different than your actual code. Usually this is called "pseudo-code". If you never heard that term before, search for it using your favourite web search engine.

Ali Reza
26th November 2012, 16:53
i add a QObject *ReturnHandle to second class. by the following:


class LookUpResultHandler : QObject
{
Q_OBJECT;
public:
LookUpResultHandler(QObject *,int);

private:
int id;
QObject *ReturnHandle;

public slots:
void lookupresult(QHostInfo);

};


and in my main cpp file i wrote:


LookUpResultHandler *lrh = new LookUpResultHandler(this,id);
QHostInfo::lookupHost(rp.getHost(),lrh,SLOT(lookup result(QHostInfo)));

and for definition of lookupresult, i wrote:


LookUpResultHandler::LookUpResultHandler(QObject *rh,int id):id(id) , ReturnHandle(rh){}

void LookUpResultHandler::lookupresult(QHostInfo hi)
{
ReturnHandle->HostLookUpResult(hi,id);
}

but give me error on second line in my main cpp
the error is: 'QObject' is inaccessible base of 'LookupResultHandler'
my problem is this ! what can i do ??
if needed i attach my project ?

amleto
26th November 2012, 16:56
my first try would be this:

class LookUpResultHandler : public QObject

For future reference, if you are going to say 'line two gives me an error', it is probably somewhat helpful if you actually show line 2

Ali Reza
26th November 2012, 16:59
now i tried by "public : QObject"

but give the error:
the 'QObject' has no member that named 'HostLookUpResult'

amleto
26th November 2012, 17:03
have you looked at QObject docs? Please do. If you look at the docs and find a method named HostLookUpResult, then you may think about writing a bug report to Qt. Otherwise you may want to think about why your code is wrong, and what the ccompiler is telling you.

Ali Reza
26th November 2012, 17:06
dear amleto plz tell me how i call that function ?:)

wysota
26th November 2012, 20:18
dear amleto plz tell me how i call that function ?:)

Ali Reza, please tell me, how much experience with C++ do you have? As it seems you have completely none. All the "problems" here are related more to C++ than to Qt, the fact that we're talking about Qt classes here is completely irrelevant. Maybe you should read some book on C++ programming and spend a week or two on practising use of the language? It would really save you a lot of time.

Ali Reza
27th November 2012, 07:53
you are right Mr wysota
i am newbie to C++ and Qt. because i am a student with 16 years old. and you are instructor and master.
thanks for your advice

anda_skoa
27th November 2012, 19:06
1.you define a new class that named "MyMapper" that inherits "QObject". but this definition get me the following error:
" 'QObject' is an inaccessible base of 'MyMapper' ".

That's because of the private inheritance. Just dervice publically from QObject

Cheers,
_

amleto
28th November 2012, 01:48
well done on reading the thread

Ali Reza
30th November 2012, 09:12
finally i solved my problem by signal&slot mechanism .
thanks Mr amleto