I'm trying to use QLocalServer as an ipc solution. The version of qt is 4.6
This is my main.cpp:
int main(int argc, const char*argv[]) {
QServer test();
while (true) {
}
}
This is my QServer class:
class QServer : public QObject
{
Q_OBJECT
public :
QServer ();
virtual ~QServer();
private :
QLocalServer* m_server;
QLocalSocket* m_connection;
private slots:
void socket_new_connection();
};
QServer::QServer()
{
m_server = new QLocalServer(this);
if (!m_server->listen("DLSERVER")) {
qDebug() << "Testing";
qDebug() << "Not able to start the server";
qDebug() << m_server->errorString();
qDebug() << "Server is " << m_server->isListening();
}
connect(m_server, SIGNAL(newConnection()),
this, SLOT(socket_new_connection()));
}
void
QServer::socket_new_connection()
{
m_connection = m_server->nextPendingConnection();
connect(clientConnection, SIGNAL(readyRead()),
this, SLOT(newData(clientConnection)));
}
This all compiles, however at runtime, when I try to connect newConnection(), I get a QSocketNotifier: Can only be used with threads started with QThread error.
I have tried wrapping this whole thing in a QThread, but I still got the same error.
Can anybody explain what I'm doing wrong or why there's even a thread involved?
The error message is misleading. You need a Qt event loop in order to use QSocketNotifier. The appropriate way to do that in your application is to create a QApplication (or if you don't want any graphical stuff, a QCoreApplication). Your main should look like:
int main(int argc, char** argv)
{
QCoreApplication app(argc, argv);
QServer test();
app.exec();
return 0;
}
QCoreApplication::exec() starts the event loop (which replaces your while (true) {}
loop).
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With