Frage

I have 2 classes:

class Server : public QTcpServer
{
Q_OBJECT

public:
Server(QObject * parent = 0 , quint16 port = 1922);
void SendData(QString data);
virtual ~Server();
signals:
void RecieveMessage(QString);

private slots:
void acceptConnection();
void startRead();
void disconnected();

private:
QTcpServer *tcpServer;
QTcpSocket *client;
};

and

class ChessLanTEst : public QMainWindow
{
Q_OBJECT

public:
friend class Server;
friend class Client;
ChessLanTEst(QWidget *parent = 0);
~ChessLanTEst();

private:
Ui::ChessLanTEstClass ui;
Server *server_;
Client *client_;
private slots:
void createGame();
void ShowMessage(QString);
};

in Server class I have signal:

void RecieveMessage(QString);

in ChessLanTEst class I have slot:

   void ShowMessage(QString);

and I connected it in the ChessLanTEst constructor:

connect(server_, &Server::RecieveMessage, this, &ChessLanTEst::ShowMessage); 

but I don't receive this signal and I don't know why.

War es hilfreich?

Lösung

Based on comments, you emit in constructor of server_, before you do connect (because constructor has returned if you have valid value in server_ pointer).

Emits themselves are not queued ever, connect must have been done before emit for the slot to be called (or the call queued).

Andere Tipps

You need to use the SIGNAL and SLOT macros. Like:

connect(server_, SIGNAL(RecieveMessage(QString)), this, SLOT(ShowMessage(QString)));

If you search for the definitions of these macro's, you'll find that the arguments are actually converted to strings.

Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top