[英]How to access a member function of a cpp file of different class from another class
我是QT和C ++的新手。 我陷入一个问题,我有两个不同的类,并且我想从B.cpp访问A.cpp变量。 使用调试消息,我已经看到该代码会命中setter函数,但它从未设置Textbrowser的值。
这是main.cpp的代码。
#include <QApplication>
#include "window.h"
#include "socket.h"
int main(int argc, char **argv)
{
QApplication app (argc, argv);
Window *window = new Window();
Socket *socket = new Socket();
window->setStyleSheet("background-color: rgb(226, 226, 226);");
window->showFullScreen();
return app.exec();
}
这是window.h
#ifndef WINDOW_H
#define WINDOW_H
#include <QWidget>
class QPushButton;
class QTextBrowser;
class QString;
class Window : public QWidget
{
Q_OBJECT
public:
explicit Window(QWidget *parent = 0);
QPushButton *helloButton;
QPushButton *exitButton;
QPushButton *resetButton;
QTextBrowser *clientMsgWindow;
public slots:
void reset();
void setClientWindow(QString str);
};
#endif // WINDOW_H
这是Window.cpp
#include "window.h"
#include <QPushButton>
#include <QTextBrowser>
#include <QDebug>
#include <QString>
Window::Window(QWidget *parent) : QWidget(parent)
{
/****************** Hello BUTTON ********************/
helloButton = new QPushButton(this);
helloButton->setIconSize(QSize(145, 145));
helloButton->setGeometry(15, 160, 145, 145);
helloButton->setText("Hello World");
/******************reset BUTTON ********************/
resetButton = new QPushButton(this);
resetButton->setIconSize(QSize(145, 145));
resetButton->setGeometry(15, 160, 145, 145);
resetButton->setText("Click to Reset");
/************* EXIT BUTTON *********************/
exitButton = new QPushButton(this);
exitButton->setIcon(QIcon(":/new/prefix1/images/exit.png"));
exitButton->setIconSize(QSize(145, 145));
exitButton->setGeometry(635, 10, 145, 145);
//exitButton->setStyleSheet("background-color: rgb(236, 236, 236);");
// Signal and slot for EXIT button
qDebug() << connect(exitButton, SIGNAL (clicked()), this, SLOT (close()));
/*************** TEXT BROWSER *********************/
clientMsgWindow = new QTextBrowser(this);
clientMsgWindow->setMinimumSize(QSize(0,0));
clientMsgWindow->setMaximumSize(QSize(10000,10000));
clientMsgWindow->setGeometry(175, 50, 440, 420);
clientMSgWindow->setStyleSheet("background-color: rgb(236, 236, 236);");
}
void Window::setClientWindow(QString Str)
{
qDebug() << "Hit in Set client window to set Text";
qDebug() << Str;
clientMsgWindow->setText("This is the message from client");
clientMsgWindow->setText(Str);
qDebug() << "Setting text done";
}
/**** Slot to reset the text browser **********/
void Window::reset()
{
qDebug() << "Process in Reset Window";
clientMsgWindow->clear();
}
这是socket.h
#ifndef SOCKET_H
#define SOCKET_H
#include <QObject>
#include <QDebug>
#include <QTcpServer>
#include <QTcpSocket>
class QTextBrowser;
class Socket : public QObject
{
Q_OBJECT
public:
explicit Socket(QObject *parent = 0);
void setWindow(Window *w);
signals:
public slots:
void newConnection();
private:
QTcpServer *server;
Window *window;
};
#endif // SOCKET_H
这是socket.cpp
#include "socket.h"
#include "window.h"
#include <QWidget>
#include <QString>
Socket::Socket(QObject *parent):
QObject(parent)
{
server = new QTcpServer(this);
connect(server, SIGNAL(newConnection()), this, SLOT(newConnection()));
if(!server->listen(QHostAddress::QHostAddress("192.168.2.1"), 2793)){
qDebug() << "SERVER NOT STARTED";
}
else{
qDebug() << "SERVER STARTED";
}
}
void Socket::setWindow(Window *w)
{
this->window = w;
}
void Socket::newConnection()
{
QString clientMsg ;
QTcpSocket *socket= server->nextPendingConnection();
socket->write("Server Running on 192.168.2.1");
socket->flush();
socket->waitForBytesWritten();
// Recieve the data from Client
socket->waitForReadyRead();
qDebug() << (clientMsg = socket->readAll());
// Set the Textbrowser clientMsgWindow
this->window->setClientWindow(clientMsg);
}
每次客户端发送消息时,GUI都会终止。
您应该有一个实例,而不是为每个单独的连接创建一个新的Window实例。 您可以使用setWindow()方法使其成为单例,或使其成为Socket的成员,如下所示:
#ifndef SOCKET_H
#define SOCKET_H
#include <QObject>
#include <QDebug>
#include <QTcpServer>
#include <QTcpSocket>
class QTextBrowser;
class Socket : public QObject
{
Q_OBJECT
public:
explicit Socket(QObject *parent = 0);
void setWindow(Window *w);
signals:
public slots:
void newConnection();
private:
QTcpServer *server;
Window *window;
};
#endif // SOCKET_H
您甚至可以将window参数添加到构造函数中,以便您必须始终对其进行设置。
您的设计似乎有缺陷。 你打电话时
Window* pWindow = new Window();
pWindow->setclientWindow(clientMsg);
在newConnection
方法中,您正在创建一个新的窗口对象,并在那里设置文本。 但是,这与您在main
方法中创建的窗口对象不同,因此显然您看不到那里的文本。 实际上,在newConnection
方法中创建的对象会永远丢失,因为您不会将指针保存在任何地方,这也将导致内存泄漏,因为您无法清理它。 您必须通过存储指向另一个对象的某种方式来连接两个对象。
Object::connect: No such slot Socket::Window::setClientWindow()
此错误表明您的信号槽参数不兼容,因为setClientWindow
采用了QString
参数,并且发出了空信号readReady
。
编辑:在您发表评论之后,我认为您需要了解信号/插槽的基本概念。
您的连接应类似于:
QObject::connect(socket, SIGNAL(readyRead()), window, SLOT(setClientWindow()))
但是您没有可将套接字连接到的窗口对象。 您需要重构代码,因为它有很多设计问题。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.