You are not logged in.

Dear visitor, welcome to QtForum.org. If this is your first visit here, please read the Help. It explains in detail how this page works. To use all features of this page, you should consider registering. Please use the registration form, to register here or read more information about the registration process. If you are already registered, please login here.

1

Monday, April 25th 2005, 1:25pm

Controlapp issue (QSocketDevice and Thread)

I'm trying to develop a GUI that connects to multiple servers.
I assume i need to use QThread and QSocketDevice, but I'm wondering how I could do this.

In the GUI there will be buttons that will execute some commands on all the servers, and I'm trying to find out a way to do this. I was hoping you guys with experience had some good advice on how I should be doing this :-)

- mss

Posts: 2,162

Location: Graz, Austria

Occupation: Student

  • Send private message

2

Monday, April 25th 2005, 4:31pm

You don't necessarily have to use QThread+QSocketDevice.

If your data transfer needs are not huge, you can use QSocket, which works through the event loop

Cheers,
_
Qt/KDE Developer
Debian User

3

Monday, April 25th 2005, 6:24pm

Quoted

Originally posted by anda_skoa
You don't necessarily have to use QThread+QSocketDevice.

If your data transfer needs are not huge, you can use QSocket, which works through the event loop

Cheers,
_


Are you sure of this?
I thought that when connecting to multiple servers, I needed to use Threads to control them all. Maybe I've been misinformed?

Its like 1-2kb data max being transfered I guess.

Posts: 2,162

Location: Graz, Austria

Occupation: Student

  • Send private message

4

Monday, April 25th 2005, 9:55pm

QSocket is an eventloop operated class, like QTimer or QProcess.
This means none of its methods is blocking, they return and you get notfied about results by signals.

In case of QSocket you can connect to many servers and each QSocket instance will signal state changed and data availability, etc

Cheers,
_
Qt/KDE Developer
Debian User

5

Monday, April 25th 2005, 10:10pm

Really? Then it seems I misunderstood the documentation. This might seem as a really bad question, but when I want to disconnect from 1 certain server (from a list), is there a way for me to do so? And maybe send a command to all the servers I am connected to?

Or is it then easier to use QSocketDevice and Threads?

A lot of questions here, thank you for being patient with me :)

6

Monday, April 25th 2005, 10:39pm

Sounds to me like you could just use an array of QSocket objects. When you want to disconnect from one server, just call the close() function on that one particular object. If you want to send a command to all the servers, then iterate through the array and send the command to each object.

7

Tuesday, April 26th 2005, 9:35am

Ok, I've done this in java, then i just did:

ClientThread t[] = new ClientThread[10]; (if i wanted to connect to ten servers)

Will it be the same in qT?

Posts: 2,162

Location: Graz, Austria

Occupation: Student

  • Send private message

8

Tuesday, April 26th 2005, 11:39am

Quoted

Originally posted by mss
Really?

Yes, trust me, I have been using QSocket for years :)

Quoted

This might seem as a really bad question, but when I want to disconnect from 1 certain server (from a list), is there a way for me to do so?

You have one QSocket instance for each connection, so if you just call methods on one of them, it will only affect one connection.

Quoted


And maybe send a command to all the servers I am connected to?

You can either loop through the list or create a subclass of QSocket, add a slot for command sending and connect a custom signal to the slot on each socket.
This way Qt make the iteration for you.

However alternative 1 is usually the better choice :)

Quoted


Or is it then easier to use QSocketDevice and Threads?


If you are used to working with threads, it might be. The advantage of QSocket is that you stay single thread and don't have to worry about inter-thread synchronization.

Cheers,
_
Qt/KDE Developer
Debian User

9

Tuesday, April 26th 2005, 12:10pm

I am not used to work with Threads at all, I just have been coding some examples and stuff like that.
Now I'm using QT in a big project at school, so I need to get a good overview on how it will work. :)

How can I loop through the list? Could you show me an example ? :-)

---

Another thing, I've got a couple of classes now. 1 class for the GUI, and 1 class for the connections. And I'm trying to update the GUI-status window with slots from the Connection-class. But it seems I don't really get how the slots work.

In the GUI-constructor I instantiate the connection-class, and connect the signals/slots to that, but should I then do the same in the connection class? (Instantiate the GUI-class). It doesnt seem to be
working right now anyways.

Very frustrating :-)

Posts: 2,162

Location: Graz, Austria

Occupation: Student

  • Send private message

10

Tuesday, April 26th 2005, 3:19pm

Quoted

Originally posted by mss
How can I loop through the list? Could you show me an example ? :-)


If you use a QPtrList you can just use the at() method.

But you could also use other data containers, for example QDict and a mapping from server name to QSocket.

Quoted


In the GUI-constructor I instantiate the connection-class, and connect the signals/slots to that, but should I then do the same in the connection class? (Instantiate the GUI-class).

One way should be enough.

Do you get any warning on the commanline?

Cheers,
_
Qt/KDE Developer
Debian User

11

Tuesday, April 26th 2005, 3:53pm

I don't get any warnings at all.

Heres the code:

Source code

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
// GUI header:
public slots:
    void socketClosed();
    void updateView(QString data);
    void socketConnected();
    
// GUI Class
    Client *cl = new Client();
    connect(cl, SIGNAL(dataRcvd(QString)), this, SLOT(updateView(QString)));
    connect(cl, SIGNAL(sckClosed()), this, SLOT(socketClosed()));
    connect(cl, SIGNAL(sckConnected()), this, SLOT(socketConnected()));

void Gui::socketConnected()
{
    printf("Signal Emitted?"); // Doesnt happen
}

//Client header
signals:
    void dataRcvd(QString data);
    void sckClosed();
    void sckConnected();

//Client Class:
void Client::test()
{
printf("Testing testing"); // This gets printed
emit sckConnected(); // this does not trigger anything in the GUI class.
}


Isn't this the correct way to do it?

---

I'll have a look at the QPtrList class, thanks!

Posts: 2,162

Location: Graz, Austria

Occupation: Student

  • Send private message

12

Tuesday, April 26th 2005, 8:35pm

Yes, looks correct.

Do both headers have the Q_OBJECT macro?

Cheers,
_
Qt/KDE Developer
Debian User

13

Tuesday, April 26th 2005, 11:21pm

The headers starts like this:

Source code

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
// First the Gui header
class Gui : public QVBox
{
    Q_OBJECT
public:
    Gui(QWidget *parent = 0, const char *name = 0);
    ~Gui();
(....)
};

//and then the Client header:
class Client : public QVBox
{
    Q_OBJECT
public:
    Client();
    ~Client();
(...)
}


I must be doing something wrong, I just can't seem to understand what. :-(

14

Wednesday, April 27th 2005, 10:49am

Ok, i got it working now.
The problem was that the signal was emitted before i had connected it to a slot.

15

Wednesday, April 27th 2005, 1:33pm

RE: Controlapp issue (QSocketDevice and Thread)

Ok, sorry to disturb you all again, but now I have a new problem.
It seems that the signals won't be emitted when I'm using the QPtrList.

When I'm connecting to a new IP, I'm doing this:

Source code

1
2
3
4
5
void Gui::doConnect()
{
    list.append(new Client(ip, port, "ServerName"));
    infoText->append( tr("Connecting to:" +qleIP->text()) );
}


It connects to every server I try to connect to, and it also sends data to all the servers. And i know that the server(s) responds the info back to the client, but the signals won't get emitted anyways.

Anyone know what might be wrong? ( I'm connecting the signals in the Clients constructor class. )

Posts: 2,162

Location: Graz, Austria

Occupation: Student

  • Send private message

16

Wednesday, April 27th 2005, 3:08pm

How does the client object access the GUI object?
Do the connects work? (return value of connect should be true)

Cheers,
_
Qt/KDE Developer
Debian User

17

Thursday, April 28th 2005, 12:30pm

I connected the signals and slots to a dummy instance of the client class, so of course it wouldn't work :)

I fixed it by connecting signals and slots for every instance of the client class i made. Thanks for all your help :)