Display text from QLineEdit to QTextEdit already containing some text and updating it in real time
What is the procedure for creating text in a widget QLineEdit
dynamically rendered internally QTextEdit
that already contains some text?
For example, let's say a is QLineEdit
asking for a name that says "John". Is it possible to display it in real time inside QTextEdit
containing:
Name + textFromQLineEdit
+, age 24?
The displayed text should dynamically reflect the changes made to it QLineEdit
, so the user doesn't have to click a button or hit the enter key to see his / her name.
Below is the minimal code for connecting two widgets to each other using a signal textChanged()
from QLineEdit
and a slot setText()
from QTextEdit
(which does not allow text to be added before and after text from QLineEdit
):
#include <QLineEdit>
#include <QVBoxLayout>
#include <QGroupBox>
#include <QTextEdit>
#include <QApplication>
class SmallWindow : public QWidget
{
Q_OBJECT
public:
SmallWindow();
private:
QLineEdit *nameLine;
QTextEdit *textBox;
};
SmallWindow::SmallWindow() : QWidget()
{
setFixedSize(300,250);
QLineEdit *nameLine = new QLineEdit;
QTextEdit *textBox = new QTextEdit;
QWidget::connect(nameLine,SIGNAL(textChanged(QString)),textBox,SLOT(setText(QString)));
QVBoxLayout *layout = new QVBoxLayout;
layout->addWidget(nameLine);
layout->addWidget(textBox);
QGroupBox *group = new QGroupBox(this);
group->setLayout(layout);
}
int main(int argc, char *argv[])
{
QApplication app(argc, argv);
SmallWindow window;
window.show();
app.exec();
}
#include "main.moc"
What do you need to do to keep the text before and after the text QLineEdit
and update the field QTextEdit
in real time?
source to share
Create a custom slot:
void SmallWindow::pasteText(const QString& str)
{
textBox->setText(QString("The name is %1 , age 24").arg(str));
}
and don't use signal textChanged()
because you only need one user accepted name, so you need QLineEdit::editingFinished()
(or maybe QLineEdit::returnPressed()
it depends on your needs)
connect(nameLine,SIGNAL(editingFinished(QString)),this,SLOT(pasteText(QString)));
Also you don't need it QWidget::connect
, because you write this code inside a subclass QObject
, so you don't need it.
Also these lines:
QLineEdit *nameLine = new QLineEdit;
QTextEdit *textBox = new QTextEdit;
it should be:
nameLine = new QLineEdit;
textBox = new QTextEdit;
source to share
Create your own text update slot. I think you also have some bugs in your code.
The nameLine and textBox widgets are already defined in SmallWindow.h. You probably want to create them in SmallWindow.cpp like this:
nameLine = new QLineEdit;
textBox = new QTextEdit;
Also the GroupBox is not set to any layouts. Perhaps you want to create another layout and set the widget there?
QVBoxLayout *mainlayout = new QVBoxLayout;
mainlayout->addWidget(group);
this->setLayout(mainlayout);
If you create your own text update slot, you can simply change the text content of the textbox:
SmallWindow.h
#ifndef SMALLWINDOW_H
#define SMALLWINDOW_H
#include <QLineEdit>
#include <QVBoxLayout>
#include <QGroupBox>
#include <QTextEdit>
class SmallWindow : public QWidget
{
Q_OBJECT
public:
SmallWindow();
private slots:
updateLineEditText(QString name);
private:
QLineEdit *nameLine;
QTextEdit *textBox;
};
#endif // SMALLWINDOW_H
SmallWindow.cpp
#include "SmallWindow.h"
SmallWindow::SmallWindow() : QWidget()
{
setFixedSize(300,250);
nameLine = new QLineEdit;
textBox = new QTextEdit;
connect(nameLine,SIGNAL(textChanged(QString)),this,
SLOT(updateLineEditText(QString)));
QVBoxLayout *layout = new QVBoxLayout;
layout->addWidget(nameLine);
layout->addWidget(textBox);
QGroupBox *group = new QGroupBox(this);
group->setLayout(layout);
QVBoxLayout *mainlayout = new QVBoxLayout;
mainlayout->addWidget(group);
this->setLayout(mainlayout);
}
void MainWindow::updateTextEdit(QString name) {
QString textEditString("The name is ");
textEditString.append(name);
textEditString.append(", age 24 ?");
textBox->setText(textEditString);
}
source to share
This is a minimal example in Qt 5 using C ++ 11. It's about as concise as it is in Python. If you are using Qt 5 your question should look exactly like below except for the instruction connect
. This is what "minimal" means when it comes to Qt. Avoid fluff and a pattern that doesn't add a problem. There is no need to have a separate class for the window in such a simple example.
#include <QVBoxLayout>
#include <QLineEdit>
#include <QTextEdit>
#include <QApplication>
int main(int argc, char *argv[])
{
QApplication app(argc, argv);
QWidget window;
QVBoxLayout layout(&window);
QLineEdit name;
QTextEdit text;
layout.addWidget(&name);
layout.addWidget(&text);
QObject::connect(&name, &QLineEdit::textChanged, [&](const QString & name){
text.setPlainText(QString("The name is %1, age 24.").arg(name));
});
window.show();
return app.exec();
}
Ditto in Qt 4 below - note the lack of manual memory management. This is how your question should ideally have been looking for Qt 4, no slot implementation of course. You can use connectSlotsByName
or explicit connect
, of course - it's just a matter of style.
#include <QVBoxLayout>
#include <QLineEdit>
#include <QTextEdit>
#include <QApplication>
class Window : public QWidget {
Q_OBJECT
QVBoxLayout m_layout; // not a pointer!
QLineEdit m_name; // not a pointer, must come after the layout!
QTextEdit m_text;
Q_SLOT void on_name_textChanged(const QString & name) {
m_text.setPlainText(QString("The name is %1, age 24.").arg(name));
}
public:
Window() : m_layout(this) {
m_layout.addWidget(&m_name);
m_layout.addWidget(&m_text);
m_name.setObjectName("name");
QMetaObject::connectSlotsByName(this);
}
};
int main(int argc, char *argv[])
{
QApplication app(argc, argv);
Window window;
window.show();
return app.exec();
}
#include "main.moc"
source to share