QTextEdit 中的 Qt textChange()
Qt textChange() in QTextEdit
我写了这段代码,但是当我在 TextEdit 中更改文本时,没有任何反应。我做错了什么 ?我尝试过使用 this->update() 和 widget->update() 函数,但没有成功...
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");
}
首先你应该在 .h 文件中将 changeCaption
函数定义为一个槽:
private slots:
void changeCaption();
第二个 textChanged
信号有一个 QString
参数。还要更正连接语句中插槽名称的拼写错误:
connect(edit, SIGNAL(textChanged(QString)), this, SLOT(changeCaption()));
最好使用Qt5语法,因为它有助于在编译时检测到此类错误并简化代码:
connect( edit, &QLineEdit::textChanged, this, &MainWindow::changeCaption );
我写了这段代码,但是当我在 TextEdit 中更改文本时,没有任何反应。我做错了什么 ?我尝试过使用 this->update() 和 widget->update() 函数,但没有成功...
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");
}
首先你应该在 .h 文件中将 changeCaption
函数定义为一个槽:
private slots:
void changeCaption();
第二个 textChanged
信号有一个 QString
参数。还要更正连接语句中插槽名称的拼写错误:
connect(edit, SIGNAL(textChanged(QString)), this, SLOT(changeCaption()));
最好使用Qt5语法,因为它有助于在编译时检测到此类错误并简化代码:
connect( edit, &QLineEdit::textChanged, this, &MainWindow::changeCaption );