在 QTcpServer 的子类中重载 Qt 函数
我有一个 QTcpServer 的子类:
.h-file:
#ifndef GEOLISTENER_H
#define GEOLISTENER_H
#include <QTcpServer>
class GeoListener : public QTcpServer
{
Q_OBJECT
public:
explicit GeoListener(QObject *parent = 0);
bool listen(void);
signals:
public slots:
void handleConnection(void);
};
#endif // GEOLISTENER_H
.cpp-文件:
#include "geolistener.h"
#include <QDebug>
GeoListener::GeoListener(QObject *parent) :
QTcpServer(parent)
{
QObject::connect(this, SIGNAL(newConnection()),this, SLOT(handleConnection()));
}
bool GeoListener::listen(void)
{
bool ret;
ret = this->listen(QHostAddress::Any, 9871); //This function isn't found!
/* If something is to be done right after listen starts */
return ret;
}
void GeoListener::handleConnection(void) {
qDebug() << "got connection";
}
Qt-Framework 的基类具有此功能:
bool QTcpServer::listen ( const QHostAddress & address = QHostAddress::Any, quint16 port = 0 )
我用listen() 函数重载了它。如果我这样做,我就无法调用上面的函数 - 在我看来这应该可行。为什么它不起作用?有什么想法吗?
I have a subclass of QTcpServer:
.h-file:
#ifndef GEOLISTENER_H
#define GEOLISTENER_H
#include <QTcpServer>
class GeoListener : public QTcpServer
{
Q_OBJECT
public:
explicit GeoListener(QObject *parent = 0);
bool listen(void);
signals:
public slots:
void handleConnection(void);
};
#endif // GEOLISTENER_H
.cpp-file:
#include "geolistener.h"
#include <QDebug>
GeoListener::GeoListener(QObject *parent) :
QTcpServer(parent)
{
QObject::connect(this, SIGNAL(newConnection()),this, SLOT(handleConnection()));
}
bool GeoListener::listen(void)
{
bool ret;
ret = this->listen(QHostAddress::Any, 9871); //This function isn't found!
/* If something is to be done right after listen starts */
return ret;
}
void GeoListener::handleConnection(void) {
qDebug() << "got connection";
}
The base class of the Qt-Framework has this function:
bool QTcpServer::listen ( const QHostAddress & address = QHostAddress::Any, quint16 port = 0 )
I overloaded it with a listen()-function. If I do that I can't call the function above - in my opinion this should work. Why isn't it working? Any ideas?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
首先,请注意:那些 QT 类是为组合而设计的,而不是为继承而设计的。
无论如何,这里的问题是您的
listen()
函数隐藏了基础的listen()
。您的问题可以通过以下方式解决:
First of all, just a remark: those QT classes are designed for composition, not for inheritance.
Anyway, the problem here is that your
listen()
function is hiding the base'slisten()
.Your problem is resolved by:
因为名称
listen
隐藏了基础的同名函数。在你的类的定义中你可以写using QTcpServer::listen;
因此基础的监听将能够参与重载决策Because the name
listen
hides the base's function with the same name. In the definition of your class you can writeusing QTcpServer::listen;
and thus the base's listen will be able to participate in overload resolution