PDA

View Full Version : [Newbie problem] Calling a method from another class



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!");
}

wysota
30th March 2009, 16:07
Including a class definition inside some other file will not make an object of that class visible. You need to have access to a pointer to this exact object. This is basic programming knowledge... We can't paste a working snippet as you need to write some code outside those two classes to be able to communicate them. Either use a global variable or pass a pointer to one object to the other object through some method of yours or use a singleton class or make the signal-slot connection from a scope that knows both objects and can access their pointers (like the place where you create both objects).