thiforums
30th March 2009, 15:01
Hello, I have 2 classes: MdiChild and ClientSocket. I need to call the method addlog() of MdiChild inside clientsocket.cpp.
The normal thing to do is to include mdichild.h inside clientsocket.cpp. But no, it doens't work on this case (or maybe I'm missing something). Please, paste a working code for me. I already wasted too much time on this basic problem.
Read my comments.
// mdichild.h
#include <QtGui>
#include "ui_mdichild.h"
#include "clientsocket.h"
class MdiChild : public QDialog, private Ui::MdiChildDlg {
Q_OBJECT
Q_DISABLE_COPY(MdiChild)
public:
explicit MdiChild();
virtual ~MdiChild();
void addlog(QString text);
private slots:
void on_btConnect_clicked();
protected:
private:
ClientSocket *clisock;
};
#endif // MDICHILD_H
// mdichild.cpp
#include "mdichild.h"
#include "ui_mdichild.h"
/**** I need to have access to this method from clientsocket.cpp ****/
void MdiChild::addlog(QString text){
if(!cbLogEvents->isChecked()) return;
QTreeWidgetItem *item=new QTreeWidgetItem(twLogEvents);
QTime time=QTime().currentTime();
item->setText(0,time.toString("hh:mm:ss"));
item->setText(1,text);
twLogEvents->insertTopLevelItem(0,item);
}
MdiChild::MdiChild()
:QDialog()
{
static int clientsCount = 1;
setupUi(this);
setAttribute(Qt::WA_DeleteOnClose);
setWindowTitle(tr("Client %1 - ").arg(clientsCount++));
clisock = new ClientSocket(this);
}
MdiChild::~MdiChild()
{
}
void MdiChild::on_btConnect_clicked()
{
if(btConnect->text()=="Connect"){
addlog("Connecting to ("+ledRemoteHostIp->text()+":"+ledRemotePort->text()+") ...");
clisock->connectToHost(ledRemoteHostIp->text(),ledRemotePort->text().toInt());
btConnect->setText("Disconnect");
}
else{
addlog("Disconnecting from ("+ledRemoteHostIp->text()+":"+ledRemotePort->text()+") ...");
clisock->abort();
btConnect->setText("Connect");
}
}
// clientsocket.h
#ifndef CLIENTSOCKET_H
#define CLIENTSOCKET_H
#include <QTcpSocket>
#include <QSignalMapper>
#include <QMessageBox>
class ClientSocket : public QTcpSocket
{
Q_OBJECT
public:
ClientSocket( QObject * parent = 0 );
private:
private slots:
void OnConnected();
};
#endif // CLIENTSOCKET_H
// clientsocket.cpp
#include "clientsocket.h"
ClientSocket::ClientSocket( QObject * parent)
:QTcpSocket(parent)
{
connect(this,SIGNAL(connected()),this,SLOT(OnConne cted()));
}
void ClientSocket::OnConnected(){
//** I need to be able to call addlog() here **
//addlog("Connected to server!");
}
The normal thing to do is to include mdichild.h inside clientsocket.cpp. But no, it doens't work on this case (or maybe I'm missing something). Please, paste a working code for me. I already wasted too much time on this basic problem.
Read my comments.
// mdichild.h
#include <QtGui>
#include "ui_mdichild.h"
#include "clientsocket.h"
class MdiChild : public QDialog, private Ui::MdiChildDlg {
Q_OBJECT
Q_DISABLE_COPY(MdiChild)
public:
explicit MdiChild();
virtual ~MdiChild();
void addlog(QString text);
private slots:
void on_btConnect_clicked();
protected:
private:
ClientSocket *clisock;
};
#endif // MDICHILD_H
// mdichild.cpp
#include "mdichild.h"
#include "ui_mdichild.h"
/**** I need to have access to this method from clientsocket.cpp ****/
void MdiChild::addlog(QString text){
if(!cbLogEvents->isChecked()) return;
QTreeWidgetItem *item=new QTreeWidgetItem(twLogEvents);
QTime time=QTime().currentTime();
item->setText(0,time.toString("hh:mm:ss"));
item->setText(1,text);
twLogEvents->insertTopLevelItem(0,item);
}
MdiChild::MdiChild()
:QDialog()
{
static int clientsCount = 1;
setupUi(this);
setAttribute(Qt::WA_DeleteOnClose);
setWindowTitle(tr("Client %1 - ").arg(clientsCount++));
clisock = new ClientSocket(this);
}
MdiChild::~MdiChild()
{
}
void MdiChild::on_btConnect_clicked()
{
if(btConnect->text()=="Connect"){
addlog("Connecting to ("+ledRemoteHostIp->text()+":"+ledRemotePort->text()+") ...");
clisock->connectToHost(ledRemoteHostIp->text(),ledRemotePort->text().toInt());
btConnect->setText("Disconnect");
}
else{
addlog("Disconnecting from ("+ledRemoteHostIp->text()+":"+ledRemotePort->text()+") ...");
clisock->abort();
btConnect->setText("Connect");
}
}
// clientsocket.h
#ifndef CLIENTSOCKET_H
#define CLIENTSOCKET_H
#include <QTcpSocket>
#include <QSignalMapper>
#include <QMessageBox>
class ClientSocket : public QTcpSocket
{
Q_OBJECT
public:
ClientSocket( QObject * parent = 0 );
private:
private slots:
void OnConnected();
};
#endif // CLIENTSOCKET_H
// clientsocket.cpp
#include "clientsocket.h"
ClientSocket::ClientSocket( QObject * parent)
:QTcpSocket(parent)
{
connect(this,SIGNAL(connected()),this,SLOT(OnConne cted()));
}
void ClientSocket::OnConnected(){
//** I need to be able to call addlog() here **
//addlog("Connected to server!");
}