我有一个QTcpServer的子类
.h-文件:
#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()-function重载了它。如果我这样做了,我就不能调用上面的函数--在我看来,这应该是可行的。为什么它不起作用?有什么想法吗?
发布于 2010-11-23 18:55:20
首先,请注意:这些QT类是为组合而设计的,而不是为继承而设计的。
无论如何,这里的问题是您的listen()函数隐藏了基础的listen()。
您的问题可通过以下方式解决:
static_cast<QTcpServer*>(this)->listen(QHostAddress::Any, 9871);发布于 2010-11-23 19:02:03
因为名称listen隐藏了同名的基函数。在类的定义中,您可以编写using QTcpServer::listen;,因此基类的侦听将能够参与重载解析
https://stackoverflow.com/questions/4255222
复制相似问题