Results 1 to 2 of 2

Thread: [Newbie problem] Calling a method from another class

  1. #1
    Join Date
    Mar 2009
    Posts
    7
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default [Newbie problem] Calling a method from another class

    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
    Qt Code:
    1. #include <QtGui>
    2. #include "ui_mdichild.h"
    3. #include "clientsocket.h"
    4.  
    5. class MdiChild : public QDialog, private Ui::MdiChildDlg {
    6. Q_OBJECT
    7. Q_DISABLE_COPY(MdiChild)
    8. public:
    9. explicit MdiChild();
    10. virtual ~MdiChild();
    11. void addlog(QString text);
    12. private slots:
    13. void on_btConnect_clicked();
    14.  
    15. protected:
    16.  
    17. private:
    18. ClientSocket *clisock;
    19. };
    20.  
    21. #endif // MDICHILD_H
    To copy to clipboard, switch view to plain text mode 

    // mdichild.cpp
    Qt Code:
    1. #include "mdichild.h"
    2. #include "ui_mdichild.h"
    3.  
    4. /**** I need to have access to this method from clientsocket.cpp ****/
    5. void MdiChild::addlog(QString text){
    6. if(!cbLogEvents->isChecked()) return;
    7. QTreeWidgetItem *item=new QTreeWidgetItem(twLogEvents);
    8. QTime time=QTime().currentTime();
    9.  
    10. item->setText(0,time.toString("hh:mm:ss"));
    11. item->setText(1,text);
    12.  
    13. twLogEvents->insertTopLevelItem(0,item);
    14. }
    15.  
    16. MdiChild::MdiChild()
    17. {
    18. static int clientsCount = 1;
    19. setupUi(this);
    20. setAttribute(Qt::WA_DeleteOnClose);
    21.  
    22. setWindowTitle(tr("Client %1 - ").arg(clientsCount++));
    23.  
    24. clisock = new ClientSocket(this);
    25. }
    26.  
    27. MdiChild::~MdiChild()
    28. {
    29. }
    30.  
    31. void MdiChild::on_btConnect_clicked()
    32. {
    33. if(btConnect->text()=="Connect"){
    34. addlog("Connecting to ("+ledRemoteHostIp->text()+":"+ledRemotePort->text()+") ...");
    35. clisock->connectToHost(ledRemoteHostIp->text(),ledRemotePort->text().toInt());
    36.  
    37. btConnect->setText("Disconnect");
    38. }
    39. else{
    40. addlog("Disconnecting from ("+ledRemoteHostIp->text()+":"+ledRemotePort->text()+") ...");
    41. clisock->abort();
    42. btConnect->setText("Connect");
    43. }
    44. }
    To copy to clipboard, switch view to plain text mode 

    // clientsocket.h
    Qt Code:
    1. #ifndef CLIENTSOCKET_H
    2. #define CLIENTSOCKET_H
    3.  
    4. #include <QTcpSocket>
    5. #include <QSignalMapper>
    6. #include <QMessageBox>
    7.  
    8. class ClientSocket : public QTcpSocket
    9. {
    10. Q_OBJECT
    11. public:
    12. ClientSocket( QObject * parent = 0 );
    13. private:
    14. private slots:
    15. void OnConnected();
    16. };
    17.  
    18. #endif // CLIENTSOCKET_H
    To copy to clipboard, switch view to plain text mode 

    // clientsocket.cpp
    Qt Code:
    1. #include "clientsocket.h"
    2.  
    3. ClientSocket::ClientSocket( QObject * parent)
    4. :QTcpSocket(parent)
    5. {
    6. connect(this,SIGNAL(connected()),this,SLOT(OnConnected()));
    7. }
    8.  
    9. void ClientSocket::OnConnected(){
    10. //** I need to be able to call addlog() here **
    11. //addlog("Connected to server!");
    12. }
    To copy to clipboard, switch view to plain text mode 

  2. #2
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: [Newbie problem] Calling a method from another class

    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).
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


Similar Threads

  1. Replies: 3
    Last Post: 16th May 2007, 11:07
  2. Creating object of other class in Run() method
    By santosh.kumar in forum Qt Programming
    Replies: 2
    Last Post: 15th May 2007, 15:05
  3. Calling Recursivly loading function in Run() method of QThread
    By santosh.kumar in forum Qt Programming
    Replies: 2
    Last Post: 15th May 2007, 14:42
  4. Replies: 2
    Last Post: 27th March 2007, 12:09
  5. Replies: 4
    Last Post: 10th March 2007, 18:01

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.