我是 QT(占碑)的新手。我有一个非常简单的 QTJambi (4.5.2) 应用程序。我试图弄清楚为什么这在线程中不起作用(DoResponseReady 从未调用过)。在 threaded=true/false 之间切换静态变量以重现。
import com.trolltech.qt.core.QByteArray;
import com.trolltech.qt.core.QUrl;
import com.trolltech.qt.gui.QApplication;
import com.trolltech.qt.gui.QPushButton;
import com.trolltech.qt.network.QNetworkAccessManager;
import com.trolltech.qt.network.QNetworkReply;
import com.trolltech.qt.network.QNetworkRequest;
public class Main {
static boolean threaded = false;
static Main main;
public static void main(String[] args) {
QApplication.initialize(args);
main = new Main();
main.doit();
}
void DoResponseReady(QNetworkReply reply) {
System.out.println("Response Ready");
}
void testnetwork_thread()
{
new Thread(new Runnable() {
@Override
public void run() {
testnetwork();
}
}).start();
}
void testnetwork()
{
QNetworkAccessManager qnam = new QNetworkAccessManager();
QNetworkRequest req = new QNetworkRequest(new QUrl("http://junkurl.neverwillwork.ok"));
QByteArray data = new QByteArray();
qnam.finished.connect(main, "DoResponseReady(QNetworkReply)");
qnam.post(req, data);
}
void NetworkTest()
{
if (threaded)
testnetwork_thread();
else
testnetwork();
}
public void doit() {
QPushButton quit = new QPushButton("Test");
quit.clicked.connect(this, "NetworkTest()");
quit.show();
QApplication.exec();
}
}