私は毎秒関数を実行するQTimerオブジェクトを持っています。私は変数を渡すときにQTimerが起動しない
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent)
{
// Set a Qtimer to update the OSD display every 1 second
QTimer *timer = new QTimer(this);
connect(timer, SIGNAL(timeout()), this, SLOT(print()));
timer->start(1000);
}
void MainWindow::print()
{
printf("hello world\n");
}
次なければ、タイマーが正しく実行されますが、私はプリント()に変数を渡したいです。しかし、私がこれをするとき、私は決して私のプリントステートメントを見ません。
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent)
{
// Set a Qtimer to update the OSD display every 1 second
QTimer *timer = new QTimer(this);
int val = 42;
// Now pass val to print()
connect(timer, SIGNAL(timeout()), this, SLOT(print(val)));
timer->start(1000);
}
void MainWindow::print(int val)
{
// I never see my print statement
printf("hello world, val=%d\n", val);
}
がheader.h
class MainWindow : public QMainWindow
{
Q_OBJECT
public:
explicit MainWindow(QWidget *parent = 0);
~MainWindow();
public slots:
void print(int val);
は、なぜこれが動作しませんか? QTimerを使用してにprint()を渡すにはどうすればよいですか?
迅速な回答ありがとうございます。私はそれが署名と関係があると思ったが、私は今までstdoutにエラーメッセージを気付かなかった。私はそれがすぐに私にできるように、これをマークします。 –
この答えを拡張するには、QSignalMapperというクラスがあります。これは、この目的のためにオブジェクト間に単純なマッピングを作成することを可能にします(つまり、パラメーターのないシグナルが放出されたときに実際の値をいくつかのスロット関数の引数として提供する)。 – aatwo