Archie Gertsman
Archie Gertsman

Reputation: 1661

C++ Qt: Connect a TextEdit with a Label

In a Qt GUI I'm trying to connect a TextEdit with a label so that when the user types something, the label updates it's text. Here is what I've tried:

void MainWindow ::updatelabel()
{
    ui->label->setText("Hello");
}

void MainWindow::changeTextColor()
{
    QString textEdit = ui->textEdit->toPlainText();
    QString label = ui->label->text();
    connect(textEdit, SIGNAL(textChanged()), label, SLOT(updateLabel()));
}

This gives me an error though:

error: no matching function for call to 'MainWindow::connect(QString&, const char*, QString&, const char*)'
     connect(textEdit, SIGNAL(textChanged()), label, SLOT(updateLabel()));
                                                                        ^

What am I doing wrong and how can I fix it? Thanks!

Upvotes: 1

Views: 2786

Answers (2)

hyde
hyde

Reputation: 62848

You have a few problems in your code. Here's changed code with comments explaining it:

// make sure updateLabel is declared under slots: tag in .h file
void MainWindow ::updatelabel()
{
    // set label text to be the text in the text edit when this slot is called
    ui->label->setText(ui->textEdit->toPlainText());
}

// this is a very suspicious method. Where do you call it from?
// I changed its name to better indicate what it does.
void MainWindow::initializeUpdatingLabel()
{
    //QString textEdit = ui->textEdit->toPlainText(); // not used
    //QString label = ui->label->text(); // not used

    // when ever textChanged is emitted, call our updatelabel slot
    connect(ui->textEdit, SIGNAL(textChanged()), 
            this, SLOT(updatelabel())); // updateLabel or updatelabel??!
}

A practical hint: when ever you use SIGNAL and SLOT macros, let Qt Creator to autocomplete them. If you type them by hand, and make a typo, you don't get a compile time error, instead there will be a runtime warning print about having no a matching signal/slot.

Or, assuming you are using Qt5 and C++11 capable compiler, you can use the new connect syntax, which will give you compiler error if you get it wrong. First add line CONFIG += C++11 to the .pro file, and then do the connect like this:

void MainWindow::initializeUpdatingLabel()
{
    connect(ui->textEdit, &QTextEdit::textChanged, 
            this, &MainWindow::updatelabel);
}

Now if you for example actually have no updateLabel method, you get compile time error, which is much nicer than runtime message which you might not even notice. You could also replace the whole updatelabel method with a lambda, but that goes out of the scope of this question/answer.

Upvotes: 3

Harvey
Harvey

Reputation: 5821

You're connecting to the wrong textEdit and label variables in that method:

-    connect(textEdit, SIGNAL(textChanged()), label, SLOT(updateLabel()));
+    connect(ui->textEdit, SIGNAL(textChanged()), this, SLOT(updateLabel()));

A QString is not a widget with signals and slots. You want the actual widget from ui, ui->textEdit, and this for the current class which contains updateLabel().

Edits: fix mistakes I made because I answered while tired.

Upvotes: 1

Related Questions