【この質問に関連】
qt シグナルとスロットがどのように機能するかを理解するために、このコードを書きました。行動を説明し、自分の結論が正しいかどうかを教えてくれる人が必要です。
私のプログラム:
connectionhandler.h
#ifndef CONNECTIONHANDLER_H
#define CONNECTIONHANDLER_H
#include <QTcpServer>
class ConnectionHandler : public QObject
{
Q_OBJECT
public:
ConnectionHandler();
public slots:
void newConn();
private:
QTcpServer *server;
};
#endif // CONNECTIONHANDLER_H
connectionhandler.cpp
#include "connectionhandler.h"
#include <QTextStream>
ConnectionHandler::ConnectionHandler() {
server = new QTcpServer;
server->listen(QHostAddress::LocalHost, 8080);
QObject::connect(server, SIGNAL(newConnection()),this, SLOT(newConn()));
}
void ConnectionHandler::newConn() {
QTextStream out(stdout);
out << "new kanneksan!\n";
out.flush();
}
main.cpp
#include <QCoreApplication>
#include "connectionhandler.h"
int main(int argc, char* argv[]) {
QCoreApplication app(argc,argv);
ConnectionHandler handler;
return app.exec();
}
このプログラムを実行すると、新しい接続を探す無限ループに陥ります。
Observation:
を呼び出さないとapp.exec()
、プログラムはすぐに戻ります (そうあるべきです)。
Question:
なぜ?
Question:
スロットをキュー接続として接続した場合、スロット呼び出しはいつ実行されますか?
Question:
ある種の無限ループの場合app.exec()
、信号はどのようnewConnection()
に放出されるのでしょうか?
Big Question:
彼らの「2番目のスレッド」はここに関係していますか? (いいえ、そして驚くほどエレガントな説明を期待しています:))
ありがとう、
ジュニア
PS: このネストされた括弧症候群を持っている人は他にいますか? 「(.. :))」または「(.. (..))」のように?