私はMainWindow w
窓を持ってTestThread testThread
おり、のメンバーとしてw
。単純なことはわかっていますが、testThread.foo()
メソッドをtestThread
スレッドで実行できません(ウィンドウスレッドではありません)。つまり、QThread の動作がわかりません。
次のテスト アプリケーションの修正にご協力ください。QProgressBar *MainWindow::ui::progressBar
とQPushButton *MainWindow::ui::startButton
(簡単に書くと)があります。毎秒インクリメントする(startButton
クリックで)開始したい。TestThread::foo(int* progress)
int progress
メインウィンドウ:
MainWindow::MainWindow(QWidget *parent) : // ...
{
// ...
ui->progressBar->setRange(0, 5);
progress = 0; // int MainWindow::progress
this->connect(ui->startButton, SIGNAL(clicked()), SLOT(startFoo()));
connect(this, SIGNAL(startFooSignal(int*)), &testThread, SLOT(foo(int*)));
// TestThread MainWindow::testThread
testThread.start();
}
// ...
void MainWindow::timerEvent(QTimerEvent *event)
{
ui->progressBar->setValue(progress);
}
void MainWindow::startFoo() // this is a MainWindow SLOT
{
startTimer(100);
emit startFooSignal(&progress);
// startFooSignal(int*) is a MainWindows SIGNAL
}
テストスレッド:
void TestThread::foo(int *progress) // this is a TestThread SLOT
{
for (unsigned i = 0; i < 5; ++i) {
sleep(1);
++*progress; // increment MainWindow::progress
}
}
わかりました、これは簡単です。私は何か間違ったことをしています:)
PS動作を理解するために、最も単純な(可能な限り)例を実行したいと思いQThread
ます。
ありがとう!