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

喜欢而已 提交于 2020-02-28 07:26:06

问题


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?


回答1:


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;



回答2:


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);
}



回答3:


This is a minimal example in Qt 5, using C++11. It is about as concise as it would be in Python. If you are using Qt 5, then your question should have looked exactly as it does below, save for the connect statement. This is what "minimal" means when it comes to Qt. Avoid the fluff and boilerplate that doesn't add to the problem. There's 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();
}

The same in Qt 4 is below - note the absence of any manual memory management. That's how your question ideally should have looked for Qt 4, without the slot's implementation of course. You can use the connectSlotsByName or an explicit connect, of course - that'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"


来源:https://stackoverflow.com/questions/31108116/display-text-from-qlineedit-in-a-qtextedit-already-containing-some-text-and-upda

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!