What is the procedure to make a text being written in a QLineEdit
widget, dynamically display inside a QTextEdit
that already contains some text?
For example, let us say that a QLineEdit
asks for a name where one writes "John". Is it possible to display it in real time inside a QTextEdit
containing :
The name is + textFromQLineEdit
+ , age 24 ?
The displayed text has to dynamically take into account the changes being made to the QLineEdit
so that the user does not need to press a button or press enter to see his/her name appear.
The following is the minimal code for connecting the two widgets to each other using the signal textChanged()
from QLineEdit
and the slot setText()
from QTextEdit
(which does not allow for adding some text before and after the text from the 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 should be done to keep the text before and after the QLineEdit
text in place and updating the QTextEdit
box in real time?