Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Display text from QLineEdit in a QTextEdit already containing some text and update it in real time

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?

like image 672
Meclassic Avatar asked Jan 08 '23 01:01

Meclassic


2 Answers

Create special slot:

void SmallWindow::pasteText(const QString& str)
{
    textBox->setText(QString("The name is %1 , age 24").arg(str)); 
}

and don't use textChanged() signal because you need only one name accepted by user, 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 QWidget::connect, because you write this code inside QObject subclass, so it is not necessary.

Also these lines:

QLineEdit *nameLine = new QLineEdit;
QTextEdit *textBox = new QTextEdit;

should be:

nameLine = new QLineEdit;
textBox = new QTextEdit;
like image 113
Kosovan Avatar answered Jan 10 '23 15:01

Kosovan


Create an own slot for the text update. I think that you have also some errors in your code.

Widgets nameLine and textBox are already defined in the SmallWindow.h. You probably want to create them in SmallWindow.cpp following way:

nameLine = new QLineEdit;
textBox = new QTextEdit;

Also GroupBox group is not set to any layouts. Perhaps you want to create one layout more and set the widget there?

QVBoxLayout *mainlayout = new QVBoxLayout;
mainlayout->addWidget(group);   
this->setLayout(mainlayout);

If you create an own slot for the text update, you can just change text content of the textBox there:

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:
 void 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 SmallWindow::updateLineEditText(QString name) {
    QString textEditString("The name is ");
    textEditString.append(name);
    textEditString.append(", age 24 ?");
    textBox->setText(textEditString);
}
like image 37
Petri Pyöriä Avatar answered Jan 10 '23 14:01

Petri Pyöriä