我想将数据从一种形式传递到另一种形式。我做了一些研究,我发现了如何做,但我的代码不起作用!!
这是我的代码:
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&))) ;
我没有从MainWindow类获得端口号!! 有人可以请帮助。
修改 这是我的代码
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()) ;
}*/
}
有人可以告诉我出了什么问题。这让我发疯了!
答案 0 :(得分:0)
这里有很多小问题,看看评论,这段代码似乎不是真正的代码......?发布真实的代码然后发送一些非常错误的例子会更好 - 这会让想要帮助你的人感到困惑。
无论如何,有一些事情可以帮助你:
if (!QObject::connect(...)) {printf("Connection failed!\n");}
。如果它是假的那么就是错误的。
void MainWindow::on_pushButtonclicked()
{
emit signalEmission((QString) "test");
}
最后,请显示编译和调试输出中的任何错误/警告。