1

クラス Label のテキストを別のクラスから変更しようとしています。Label を含むクラス MainWindow があります。

ラベルの値を変更したい Bot クラスもあります。

シグナルとスロットを作成しようとしていますが、どこから始めればよいかわかりません。

次のようにシグナルとスロットを作成しました。

//in mainwindow.h
signals:
void changeTextSignal();

private slots:
void changeText();

//in mainwindow.cpp
void MainWindow::changeText(){
this->label->setText("FooBar");
}

しかし、ラベルのテキストを別のクラスから変更できるように信号を接続する方法がわかりません。

4

1 に答える 1

5

Qtシグナルスロット メカニズムを読んでください。あなたの理解が正しければ、ラベル テキストを変更する必要があることを Bot から MainWindow に通知しようとしています。これがあなたのやり方です...

//bot.h
class Bot
{
    Q_OBJECT;
    //other stuff here
signals:
    void textChanged(QString);
public:
    void someFunctionThatChangesText(const QString& newtext)
    {
        emit textChanged(newtext);
    }
}

//mainwindow.cpp
MainWindow::MainWindow
{
    //do other stuff
    this->label = new QLabel("Original Text");
    mybot = new Bot;   //mybot is a Bot* member of MainWindow in this example
    connect(mybot, SIGNAL(textChanged(QString)), this->label, SLOT(setText(QString)));
}

void MainWindow::hello()
{
    mybot->someFunctionThatChangesText("Hello World!");
}
于 2013-07-25T18:48:28.720 に答える