Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. can not call function of different class
Forum Updated to NodeBB v4.3 + New Features

can not call function of different class

Scheduled Pinned Locked Moved Unsolved General and Desktop
24 Posts 6 Posters 4.1k Views 2 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • J JadeN001

    connection.h

    #ifndef CONNECTION_H
    #define CONNECTION_H
    
    #include <QDialog>
    #include<QTcpSocket>
    #include"mainwindow.h"
    #include"formclient.h"
    namespace Ui {
    class connection;
    }
    class MainWindow;
    class connection : public QDialog
    {
        Q_OBJECT
    
    public:
        explicit connection(QWidget *parent = 0);
        ~connection();
        QTcpSocket *m_pclientsocket;
        void send_MsgToServer(QString msg);
    
    private slots:
        void Read();
        void displayError(QAbstractSocket::SocketError socketerror);
        void on_pushButton_connect_clicked();
    
        void on_pushButton_send_clicked();
    
    private:
        Ui::connection *ui;
        MainWindow *m;
    };
    
    #endif // CONNECTION_H
    
    

    connection.cpp

    #include "connection.h"
    #include "ui_connection.h"
    #include<QMessageBox>
    connection::connection(QWidget *parent) :
        QDialog(parent),
        ui(new Ui::connection)
    {
        ui->setupUi(this);
    
    }
    
    void connection::send_MsgToServer(QString msg)
    {
        qDebug()<<"in sendmsgserver"<<m_pclientsocket;
        m_pclientsocket->write(msg.toStdString().c_str());
        qDebug()<<"message sended "<<msg;
    }
    
    

    formclient.h

    #ifndef FORMCLIENT_H
    #define FORMCLIENT_H
    
    #include <QWidget>
    #include"mydelegate.h"
    #include"mainwindow.h"
    #include"connection.h"
    #include<QString>
    namespace Ui {
    class FormClient;
    }
    class mydelegate;
    class connection;
    class FormClient : public QWidget
    {
        Q_OBJECT
    
    public:
        explicit FormClient( QString name,QWidget *parent = 0);
        ~FormClient();
    signals:
        void sendmsg(QString msg);
    public slots:
        void on_pushButton_send_clicked();
    
    private:
        Ui::FormClient *ui;
        mydelegate *delegate;
        connection *mw;
        QString C_name;
    };
    
    #endif // FORMCLIENT_H
    
    

    formclient.cpp

    FormClient::FormClient(QString name, QWidget *parent) :
        QWidget(parent),
        ui(new Ui::FormClient)
    {
        ui->setupUi(this);
        delegate=new mydelegate(this);
        mw=new connection(this);
        C_name=name;
        ui->label_clientname->setText(C_name);
    }
    
    FormClient::~FormClient()
    {
        delete ui;
    }
    
    void FormClient::on_pushButton_send_clicked()
    {
        QString s = ui->textEdit_message->toPlainText().trimmed();
     
        QString send=C_name+":"+s;
           qDebug()<<s<<" "<<send;
          mw->send_MsgToServer(send);  "<- not working"
    
    }
    

    mw->send_MsgToServer(send); is not getting executed. It is may be issue of object "mw".But i dont know what exactly it is.

    K Offline
    K Offline
    koahnig
    wrote on last edited by
    #2

    @JadeN001

    Did you do a rerun of qmake and a rebuild?

    Your project might be ouit of sync.

    Otherwise the best is touse the debugger and set a break point there.

    Possibly you should have received a warning for the connect not possible.

    Vote the answer(s) that helped you to solve your issue(s)

    J 1 Reply Last reply
    1
    • K koahnig

      @JadeN001

      Did you do a rerun of qmake and a rebuild?

      Your project might be ouit of sync.

      Otherwise the best is touse the debugger and set a break point there.

      Possibly you should have received a warning for the connect not possible.

      J Offline
      J Offline
      JadeN001
      wrote on last edited by JadeN001
      #3

      @koahnig program runs properly. But when function send_MsgToServer called.It stopped working.

      JonBJ 1 Reply Last reply
      0
      • J JadeN001

        @koahnig program runs properly. But when function send_MsgToServer called.It stopped working.

        JonBJ Online
        JonBJ Online
        JonB
        wrote on last edited by
        #4

        @JadeN001
        You have several qDebug() messages, including 2 in send_MsgToServer(). Do you think it would help us if you told us which ones produced any output (or state that none did), or would you like us to guess?

        J 1 Reply Last reply
        0
        • JonBJ JonB

          @JadeN001
          You have several qDebug() messages, including 2 in send_MsgToServer(). Do you think it would help us if you told us which ones produced any output (or state that none did), or would you like us to guess?

          J Offline
          J Offline
          JadeN001
          wrote on last edited by
          #5

          @JonB none of the qDebug() of send_MsgToServer() function works. I mean send_MsgToServer() is not getting called.
          And formclient() is called from delegate's create editor().Is there amy connection with issue?

          JonBJ K 2 Replies Last reply
          1
          • J JadeN001

            @JonB none of the qDebug() of send_MsgToServer() function works. I mean send_MsgToServer() is not getting called.
            And formclient() is called from delegate's create editor().Is there amy connection with issue?

            JonBJ Online
            JonBJ Online
            JonB
            wrote on last edited by
            #6

            @JadeN001
            OK, so you do get the qDebug() in on_pushButton_send_clicked() but not the first one even in send_MsgToServer(), right?

            J 1 Reply Last reply
            1
            • J JadeN001

              @JonB none of the qDebug() of send_MsgToServer() function works. I mean send_MsgToServer() is not getting called.
              And formclient() is called from delegate's create editor().Is there amy connection with issue?

              K Offline
              K Offline
              koahnig
              wrote on last edited by
              #7

              @JadeN001

              If you are trying to do an auto-connect through Qt, there might be the parameter missing [see here](lhttps://doc.qt.io/qt-5/qabstractbutton.html#clicked .

              Therefore, my hint that you might have received a warning on that.

              Also when a program crashes under certain circumstances it is helpful to use the debugger.

              Vote the answer(s) that helped you to solve your issue(s)

              1 Reply Last reply
              2
              • JonBJ JonB

                @JadeN001
                OK, so you do get the qDebug() in on_pushButton_send_clicked() but not the first one even in send_MsgToServer(), right?

                J Offline
                J Offline
                JadeN001
                wrote on last edited by
                #8

                @JonB yeah. because send_MsgToServer() is getting called.

                JonBJ 1 Reply Last reply
                0
                • J JadeN001

                  @JonB yeah. because send_MsgToServer() is getting called.

                  JonBJ Online
                  JonBJ Online
                  JonB
                  wrote on last edited by
                  #9

                  @JadeN001
                  Do you have access to and know how to use a debugger, because that's what I would use?

                  1 Reply Last reply
                  1
                  • J Offline
                    J Offline
                    JadeN001
                    wrote on last edited by
                    #10

                    @koahnig @JonB I Don't have that much idea about debugger. Is there any alternative solution ?

                    jsulmJ JonBJ 2 Replies Last reply
                    0
                    • J JadeN001

                      @koahnig @JonB I Don't have that much idea about debugger. Is there any alternative solution ?

                      jsulmJ Offline
                      jsulmJ Offline
                      jsulm
                      Lifetime Qt Champion
                      wrote on last edited by
                      #11

                      @JadeN001 You should really learn to use a debugger. A debugger is one of the most important tools a programmer should use :-)

                      https://forum.qt.io/topic/113070/qt-code-of-conduct

                      1 Reply Last reply
                      4
                      • J JadeN001

                        @koahnig @JonB I Don't have that much idea about debugger. Is there any alternative solution ?

                        JonBJ Online
                        JonBJ Online
                        JonB
                        wrote on last edited by JonB
                        #12

                        @JadeN001
                        As @jsulm has just said, learning to use a debugger will in the long-run be the best time you spent! Just do it soon.

                        In the meantime, in on_pushButton_send_clicked() just before you try to call mw->send_MsgToServer(send); I'd put in some more qDebug() statements which show some of the properties of your connection object via mw->.... You're looking to just verify that mw is indeed still pointing at some valid connection, just in case.....

                        J 1 Reply Last reply
                        4
                        • JonBJ JonB

                          @JadeN001
                          As @jsulm has just said, learning to use a debugger will in the long-run be the best time you spent! Just do it soon.

                          In the meantime, in on_pushButton_send_clicked() just before you try to call mw->send_MsgToServer(send); I'd put in some more qDebug() statements which show some of the properties of your connection object via mw->.... You're looking to just verify that mw is indeed still pointing at some valid connection, just in case.....

                          J Offline
                          J Offline
                          JadeN001
                          wrote on last edited by
                          #13

                          @JonB @jsulm yeah I really appreciate your suggestion. I will start.
                          I have checked Value of object "mw" by qDebug(). and It returns proper object value.
                          something like this :

                          object :  connection(0x201c0350, name="connection")
                          

                          I thought that might be the issue is ,the object was not being generated properly.But i was wrong.

                          JonBJ 1 Reply Last reply
                          0
                          • J JadeN001

                            @JonB @jsulm yeah I really appreciate your suggestion. I will start.
                            I have checked Value of object "mw" by qDebug(). and It returns proper object value.
                            something like this :

                            object :  connection(0x201c0350, name="connection")
                            

                            I thought that might be the issue is ,the object was not being generated properly.But i was wrong.

                            JonBJ Online
                            JonBJ Online
                            JonB
                            wrote on last edited by JonB
                            #14

                            @JadeN001
                            Assuming it really is as you say, and you really have rebuilt your code etc., and you are compiling for/viewing debug, and it's not crashing, it's going to be pretty hard to answer how the line mw->send_MsgToServer(send) could simply "not get executed", without a debugger....

                            One last thing I can think of, just in case. Your first line in send_MsgToServer() is:

                            qDebug()<<"in sendmsgserver"<<m_pclientsocket;

                            Just in case there's some problem serializing m_pclientsocket, temporarily change it to plain:

                            qDebug()<<"in sendmsgserver"

                            (I don't think there actually will have been an issue because I think you're only trying to print out the pointer here, but it's worth a try...)

                            J 1 Reply Last reply
                            0
                            • JonBJ JonB

                              @JadeN001
                              Assuming it really is as you say, and you really have rebuilt your code etc., and you are compiling for/viewing debug, and it's not crashing, it's going to be pretty hard to answer how the line mw->send_MsgToServer(send) could simply "not get executed", without a debugger....

                              One last thing I can think of, just in case. Your first line in send_MsgToServer() is:

                              qDebug()<<"in sendmsgserver"<<m_pclientsocket;

                              Just in case there's some problem serializing m_pclientsocket, temporarily change it to plain:

                              qDebug()<<"in sendmsgserver"

                              (I don't think there actually will have been an issue because I think you're only trying to print out the pointer here, but it's worth a try...)

                              J Offline
                              J Offline
                              JadeN001
                              wrote on last edited by
                              #15

                              @JonB yeah u r right. Here is problem in m_pClientsocket. Thank you for direct me. now, I am trying to solve socket problem

                              1 Reply Last reply
                              0
                              • J Offline
                                J Offline
                                JadeN001
                                wrote on last edited by JadeN001
                                #16

                                I have define m_pClientsocket in constructor of class
                                but it failed to access in send_MsgToServer().
                                because ,If I define in constuctor then when formclient class define the object on connection class

                                FormClient::FormClient(QString name, QWidget *parent) :
                                    QWidget(parent),
                                    ui(new Ui::FormClient)
                                {
                                    ui->setupUi(this);
                                    delegate=new mydelegate(this);
                                    mw=new connection(this);
                                 
                                }
                                

                                At that time value of socket is getting changed.
                                So the changes it bu moving the

                                    m_pclientsocket= new QTcpSocket(this);
                                
                                

                                in to another main function.

                                 void connection::on_pushButton_connect_clicked()
                                {
                                    m_pclientsocket= new QTcpSocket(this);
                                
                                    ui->textEdit_Sendname->clear();
                                   m_pclientsocket->connectToHost(ui->textEdit_IP->toPlainText(),quint16(ui->textEdit_Port->toPlainText().toInt()));
                                   if(m_pclientsocket->waitForConnected(1000)){
                                   qDebug()<<"connected"<<m_pclientsocket;
                                   ui->label_connection->setText("Connected to Server");
                                   }
                                }
                                

                                send_MsgToServer() can not access it . where i am wrong.? what could be solution?

                                1 Reply Last reply
                                0
                                • J Offline
                                  J Offline
                                  JadeN001
                                  wrote on last edited by JadeN001
                                  #17

                                  In send_MsgToServer(). the ans of

                                  void connection::send_MsgToServer(QString msg)
                                  { 
                                  qDebug()<<"in sendmsgserver"<<m_pclientsocket;
                                      m_pclientsocket->write(msg.toStdString().c_str());
                                      qDebug()<<"message sended "<<msg;
                                  }
                                  
                                  in sendmsgserver QApplication(0x22fe28)
                                  QIODevice::write (QApplication): device not open
                                  

                                  what it is indicating? How can be value of socket be like QApplication.

                                  1 Reply Last reply
                                  1
                                  • SGaistS Offline
                                    SGaistS Offline
                                    SGaist
                                    Lifetime Qt Champion
                                    wrote on last edited by
                                    #18

                                    Hi,

                                    Why are you creating a new QTcpSocket each time you call on_pushButton_connect_clicked ?

                                    Interested in AI ? www.idiap.ch
                                    Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

                                    aha_1980A J 2 Replies Last reply
                                    1
                                    • SGaistS SGaist

                                      Hi,

                                      Why are you creating a new QTcpSocket each time you call on_pushButton_connect_clicked ?

                                      aha_1980A Offline
                                      aha_1980A Offline
                                      aha_1980
                                      Lifetime Qt Champion
                                      wrote on last edited by
                                      #19

                                      And why are you using waitForConnected?

                                      this cannot work. use signals&slots!

                                      regards

                                      Qt has to stay free or it will die.

                                      J 1 Reply Last reply
                                      1
                                      • SGaistS SGaist

                                        Hi,

                                        Why are you creating a new QTcpSocket each time you call on_pushButton_connect_clicked ?

                                        J Offline
                                        J Offline
                                        JadeN001
                                        wrote on last edited by
                                        #20

                                        @SGaist because the Problem i am facing :
                                        if I called new QTcpSocket from constructor,then

                                        FormClient::FormClient(QString name, QWidget *parent) :
                                            QWidget(parent),
                                            ui(new Ui::FormClient)
                                        {
                                            ui->setupUi(this);
                                            delegate=new mydelegate(this);
                                            mw=new connection(this);
                                         
                                        }
                                        

                                        this class create QTcpSocket evety time when mw=new connection(this); called.
                                        because of that the void connection::send_MsgToServer(QString msg)
                                        get different value of QTcpsocket.

                                        1 Reply Last reply
                                        0
                                        • aha_1980A aha_1980

                                          And why are you using waitForConnected?

                                          this cannot work. use signals&slots!

                                          regards

                                          J Offline
                                          J Offline
                                          JadeN001
                                          wrote on last edited by
                                          #21

                                          @aha_1980 yeah i will correct it.but my problem is still there.

                                          1 Reply Last reply
                                          0

                                          • Login

                                          • Login or register to search.
                                          • First post
                                            Last post
                                          0
                                          • Categories
                                          • Recent
                                          • Tags
                                          • Popular
                                          • Users
                                          • Groups
                                          • Search
                                          • Get Qt Extensions
                                          • Unsolved