Krzysztof Majewski
Krzysztof Majewski

Reputation: 2534

Qt textChange() in QTextEdit

I wrote this code but when I change text inside TextEdit nothing happens. What did i do wrong ? I have tried using this->update() and widget->update() functions but it didn't work...

mainwindow.h:

#ifndef MAINWINDOW_H
#define MAINWINDOW_H

#include <QMainWindow>
#include <QTextEdit>
#include <QPushButton>
#include <QWidget>
#include <QVBoxLayout>

class MainWindow : public QMainWindow
{
    Q_OBJECT

    QTextEdit *edit;
    QPushButton *pb;
    QWidget *widget;
    QVBoxLayout *layout;

    void changeCaption();

public:
    MainWindow(QWidget *parent = 0);
    ~MainWindow();
};

#endif // MAINWINDOW_H

mainwindow.cpp:

#include "mainwindow.h"

MainWindow::MainWindow(QWidget *parent)
    : QMainWindow(parent)
{
    edit = new QTextEdit;
    pb = new QPushButton("HEHE");
    widget = new QWidget;
    layout = new QVBoxLayout(widget);
    layout->addWidget(edit);
    layout->addWidget(pb);
    this->setCentralWidget(widget);

    connect(edit, SIGNAL(textChanged()), this, SLOT(chngeCaption));
}

MainWindow::~MainWindow()
{

}

void MainWindow::changeCaption(){
    pb->setText("CHANGED");
}

Upvotes: 2

Views: 5537

Answers (2)

Dmitry Sazonov
Dmitry Sazonov

Reputation: 8994

It is better to use Qt5 syntax, because it helps to detect such errors in compile time and simplify code:

connect( edit, &QLineEdit::textChanged, this, &MainWindow::changeCaption );

Upvotes: 3

Nejat
Nejat

Reputation: 32635

First you should define changeCaption function as a slot in .h file :

private slots:
    void changeCaption();

Second textChanged signal has a QString argument. Also correct the typo of slot name in the connect statement:

connect(edit, SIGNAL(textChanged(QString)), this, SLOT(changeCaption()));

Upvotes: 3

Related Questions