1

I want to pass data from one form to another. I did some research and I found out how to do it, but my code doesn't work!!
Here is my code:

MainWindow.h

signals:
    void signalEmission(const String &port) ;

MainWindow.cpp

void MainWindow::on_pushButtonclicked()
{

   emit signalEmission(nameArea->toPlainText()) ;

}

class.h

private slots:
  void mySlot(const QString &port) ;

class.cpp

void class::mySlot(const QString &port)
{
   messages->append(port) ;
}

main.cpp

  MainWindow h ;
    class c;

 QObject::connect(&h,SIGNAL(signalEmission(const QString&)),&c,SLOT(research(const QString&))) ;

I don't get the port number from the MainWindow class!! Can someone please help.


EDIT: Here is my code

history.h

#ifndef HISTORY_H
#define HISTORY_H

#include <QMainWindow>
#include <ui_history.h>
#include <QTcpSocket>

class History : public QMainWindow, private Ui::History
{
    Q_OBJECT

public:
    explicit History();

private slots:
    void on_searchButton_clicked();
signals:
        void signalEmission( QString port) ;

};

#endif // HISTORY_H

history.cpp

#include "history.h"
#include "ui_history.h"
#include "client.h"
#include "ui_client.h"
#include <QDebug>
#include <QtNetwork>

History::History() {

    setupUi(this);
    resize(500,200);
    move(300,300) ;

}


void History::on_searchButton_clicked()
{

   emit this->signalEmission(receiverPort->toPlainText()) ;

   this->close() ;
}

client.h

#ifndef HEADER_CLIENT
#define HEADER_CLIENT

#include <QMainWindow>
#include <QtWidgets>
#include <QtNetwork>
#include <QSqlDatabase>
#include "history.h"
#include "ui_client.h"

class client : public QMainWindow, private Ui::client
{
    Q_OBJECT

    public:
    explicit client();

    public slots:
        void on_connexionButton_clicked();
        void on_sendButton_clicked();
        void receivedData();
        void connectClient();
        void disconnect();
        void socketError(QAbstractSocket::SocketError erreur);

        void on_disconnexionButton_clicked();
        void on_connectedClientsButton_clicked();
        void on_refreshButton_clicked();

        void on_historyButton_clicked();
        void research( QString port) ;




private slots:
        void on_clearButton_clicked();

        void on_connectedList_pressed();

private:
        QTcpSocket *socket; // Représente le serveur
        quint16 messageLength;

        History *his;
        QSqlDatabase db ;
        QStringList *cc ;
        QStringListModel *ccl ;
        QString selectedTexts ;


};

#endif

client.cpp

#include "client.h"
#include "history.h"
#include "ui_history.h"
#include "ui_client.h"
#include <QMainWindow>
#include <QSqlDatabase>
#include <QSqlError>
#include <QSqlQuery>
#include <QDebug>

client::client()
{
      setupUi(this);

      cc = new QStringList();
      ccl = new QStringListModel() ;


      socket = new QTcpSocket(this);
      connect(socket, SIGNAL(readyRead()), this, SLOT(receivedData()));
      connect(socket, SIGNAL(connected()), this, SLOT(connectClient()));
      connect(socket, SIGNAL(disconnected()), this, SLOT(disconnect()));
      connect(socket, SIGNAL(error(QAbstractSocket::SocketError)), this, SLOT(socketError(QAbstractSocket::SocketError)));

      messageLength = 0;

      disconnexionButton->setEnabled(false);
      connectedClientsButton->setEnabled(false);
      sendButton->setEnabled(false);
      clearButton->setEnabled(false);
      refreshButton->setEnabled(false);
      historyButton->setEnabled(false);
}

void client::research(QString port){


    qDebug() << port ;


    //messagesList->cleanHistory() ;
   /* QSqlQuery qry(db);
    qry.prepare("SELECT * FROM conversations where sender=? and receiver=?");
    qry.addBindValue(socket->localPort()) ;
    qry.addBindValue(port) ;
    if(!qry.exec()){
       qDebug() << "Problem with Select conversation" ;
    }
    else{
        qDebug() << "Select conversation query executed successfully" ;
    }

    messagesList->append(tr("<strong>Historique des conversations:</strong>")) ;
   while(qry.next())
      {
       messagesList->append(qry.value(2).toString()+"  "+qry.value(3).toString()) ;
      }*/


}

Can someone please tell me what's wrong. It's driving me crazy!!

GitaarLAB
  • 14,536
  • 11
  • 60
  • 80
  • 4
    What is `String` in signal `void signalEmission(const String &port)`? Shouldn't it be `QString`? – Lahiru Chandima Sep 01 '15 at 02:36
  • 1
    You have to connect to the `SLOT(mySlot(const QString &))` instead of `SLOT(research(const QString &))`. – vahancho Sep 01 '15 at 06:47
  • yes, I already got them correct in my code. This is not the problem. I don't get any errors, The slot mySlot isn't executed when the signal signalEmission is emitted from the MainWindow.cpp. – Douae Haddad Sep 01 '15 at 10:22
  • Do you get any errors during execution, like `Object::connect: No such signal...` ? (in Application Output, though doesn't affect execution). Also I notice your slot is named different in your declaration (`mySlot`) and in the `connect` (`research`)- so since this is not true code the bug may be in the code you are not showing... or you are just connecting wrong things. – Thalia Sep 01 '15 at 16:05

1 Answers1

0

There are so many little issues here, looking at the comments, this code does not appear to be the real code anyway...? it is better to post the real code then to send some very buggy example - it will confuse people trying to help you.

Anyway, a few things to try to help you:

  1. Is your class "class" really called "class"?, this is a key word already, so I would rename that, I have no idea what the compiler will make of that :o
  2. Make sure all the function signatures are identical for your slots and signals, i.e. they all take "const QString &".
  3. Check the connection return value is true (ok) and not false (failed): if (!QObject::connect(...)) {printf("Connection failed!\n");}. If its false then something is wrong.
  4. Try a simple start-point, i.e. make a slot and signal that take a QString such that: void MainWindow::on_pushButtonclicked() { emit signalEmission((QString) "test"); }

finally, please show any errors/warnings from compilation and from debug output.

code_fodder
  • 15,263
  • 17
  • 90
  • 167
  • Thanks for your advice. The data I want to pass through signal is actually a port number that I get from a QTextEdit with QTextEditObjectName->toPlainText(). Is this a (cont Qstring &) type ? – Douae Haddad Sep 02 '15 at 08:19
  • @DouaeHaddad Well, its just a QString, the function is `QString toPlainText() const`, the const in this case means it is a const function and does not modify any member variables, but the return type is just a QString. What I meant was try to send a test string first just to get your mechanism working, then start passing the real values afterwards once you get it to work. – code_fodder Sep 02 '15 at 08:58
  • I tried to pass a string, but I didn't get a thing. I tried to see what the connect function returns in main, I found that it returns true, it means the connection doesn't fail, I don't know why I can't see the string!! I use qDebug() to display. – Douae Haddad Sep 02 '15 at 20:02
  • I think you are going to have to post up more code (the real/fixed code). Need to see the connection, the slot and signal definitions and where the emit is called. Also make sure the emit is called (i.e. debug it out) – code_fodder Sep 02 '15 at 20:35
  • How can I make sure if the emit is called? – Douae Haddad Sep 02 '15 at 20:37
  • I mean just stick a debug next to it so you know that code is called. – code_fodder Sep 02 '15 at 20:50
  • Yes I already did it. I think the problem is in the signal emission. I also tried to change emit to Q_EMIT, it doesn't work either. I am going to post the whole code. – Douae Haddad Sep 02 '15 at 21:07