PDA

View Full Version : Is it possible to use other things than widgets in Designer?



hansmbakker
14th February 2006, 14:02
Hello,

I'm busy with making a chat program with Qt. I already made some plugins for Designer (a widget ith buttons for the connection and an exit button, and a widget that forms the chatwindow, see my other thread in the designer forum for that).

I'd like it if it were possible to create a widget or QObject that handles the network part (I already created some network functions with the RakNet library before I used Qt), because I like the simplicity of connecting signals and slots in Designer.

Is it possible to make a plugin of a QObject, instead of a QWidget and place that in a form in Designer?

Any suggestions on my code are welcome, too.

network.h:

#ifndef NETWORK_H
#define NETWORK_H

#include "RakNet/BitStream.h"
#include "RakNet/RakClientInterface.h"
#include "RakNet/RakNetworkFactory.h"
#include "RakNet/RakServerInterface.h"
#include "RakNet/PacketEnumerations.h"

#ifdef _WIN32
#include "Windows.h" //sleep
#endif

class Network : public QObject
{
Q_OBJECT

public:
Network(QObject *parent=0);
~Network(void);


public slots:
void connect(void);
void disconnect(void);

void sendInt(char *outmessage);
void sendData(char *outmessage);

signals:
void connecting(bool success);
void connectionAccepted(void);
void disconnection(void);
void chatMessage(const QString &message);


private:
RakClientInterface* clientInterface;
Packet* p;

void receive(void);
void handleBitStream(Packet* a);
void handleIntStream(Packet* a);
};

#endif


network.cpp:

#include <QObject>
#include "network.h"

Network::Network(QObject *parent)
: QObject(parent)
{
clientInterface = RakNetworkFactory::GetRakClientInterface();
}

Network::~Network()
{
//shut down
if(clientInterface)
RakNetworkFactory::DestroyRakClientInterface(clien tInterface);
}

void Network::connect()
{
char ip[14]="127.0.0.1";

char port[6]="44394";

if(clientInterface->Connect(ip, atoi(port), 0, 0, 30))
{
emit connecting(true);
}
else
{
emit connecting(false);
}
}

void Network::disconnect()
{
clientInterface->Disconnect(30);
}

void Network::sendInt(char* outmessage)
{
RakNet::BitStream outstream;
outstream.Write(ID_INT);
outstream.Write(atoi(outmessage));
if(clientInterface)
clientInterface->Send(&outstream, HIGH_PRIORITY, RELIABLE_ORDERED, 0 );
outstream.Reset();
}

void Network::sendData(char* outmessage)
{
RakNet::BitStream outstream;
outstream.Write(ID_TEST);
outstream.Write((int)strlen(outmessage));
outstream.Write(outmessage, (int)strlen(outmessage));
if(clientInterface)
clientInterface->Send(&outstream, HIGH_PRIORITY, RELIABLE_ORDERED, 0 );
outstream.Reset();
}

void Network::receive()
{
if(clientInterface)
{
p = clientInterface->Receive();
}

if(p)
{
switch(p->data[0])
{
case ID_CONNECTION_REQUEST_ACCEPTED:
emit verbindingGeaccepteerd();
break;
case ID_RECEIVED_STATIC_DATA:
printf("Static data received.\n");
break;
case ID_DISCONNECTION_NOTIFICATION:
emit disconnect();
break;
case ID_TEST:
//printf("Test message coming in.\n");
handleBitStream(p);
break;
case ID_INT:
//printf("Int message coming in.\n");
handleIntStream(p);
break;
default:
printf("Packet incoming with identifier %i.\n", p->data[0]);
break;
}
if(clientInterface)
clientInterface->DeallocatePacket(p);
}
}

void Network::handleBitStream(Packet* a)
{
RakNet::BitStream bitStream((char*)a->data, a->length, false);
char* incmessage;
unsigned int messagelength=0;
bitStream.IgnoreBits(32); //sizeof(unsigned char)*8); // Ignore the packet type enum
bitStream.Read(messagelength);
//printf("%i\n",messagelength);
incmessage=new char[messagelength];
if(bitStream.Read(incmessage, messagelength))
{
emit chatMessage(QString(incmessage)); //incmessage); //printf("\tDe ander zegt: %s\n",incmessage);
}
else
{
//printf("Message error.\n");
}
bitStream.Reset();
}

void Network::handleIntStream(Packet* a)
{
RakNet::BitStream bitStream((char*)a->data, a->length, false);
int intje=0;
bitStream.IgnoreBits(sizeof(ID_INT)*8); // Ignore the packet type enum
if(bitStream.Read(intje))
{
emit chatBericht(QString(QChar(intje)));
}
else
{
//printf("Message error.\n");
}
bitStream.Reset();
}

wysota
14th February 2006, 15:26
No, Designer can only use widgets. But you can use auto-connecting features of Qt4 -- if you name a slot on_xxx_yyy(), it will be auto-connected to yyy signal of xxx object.

hansmbakker
14th February 2006, 16:28
Thank you. Comments on my code are always welcome btw