在已经包含一些文本的 QTextEdit 中显示来自 QLineEdit 的文本并实时更新

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

使在 QLineEdit 小部件中写入的文本动态显示在已经包含一些文本的 QTextEdit 中的过程是什么?

例如,让我们说 QLineEdit 要求一个人写 "John" 的名字。是否可以在包含 :

QTextEdit 中实时显示它

名字是 + textFromQLineEdit + , 年龄24 ?

显示的文本必须动态考虑对 QLineEdit 所做的更改,以便用户无需按按钮或按回车键即可看到 his/her 名称出现。

以下是使用来自 QLineEdit 的信号 textChanged() 和来自 QTextEdit 的插槽 setText() 将两个小部件相互连接的最小代码(它确实不允许在 之前和之后添加一些文本 来自 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"

如何保持 QLineEdit 文本前后的文本并实时更新 QTextEdit 框?

创建特殊插槽:

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

并且不要使用textChanged()信号,因为你只需要一个用户接受的名字,所以你需要QLineEdit::editingFinished()(或者可能QLineEdit::returnPressed(),这取决于你的需要)

connect(nameLine,SIGNAL(editingFinished(QString)),this,SLOT(pasteText(QString)));

你也不需要QWidget::connect,因为你把这段代码写在QObject子类里面,所以没有必要。

还有这些行:

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

应该是:

nameLine = new QLineEdit;
textBox = new QTextEdit;

为文本更新创建一个自己的插槽。我认为您的代码中也有一些错误。

小部件 nameLine 和 textBox 已在 SmallWindow.h 中定义。您可能希望按照以下方式在 SmallWindow.cpp 中创建它们:

nameLine = new QLineEdit;
textBox = new QTextEdit;

此外,GroupBox 组未设置为任何布局。也许您想再创建一个布局并将小部件设置在那里?

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

如果您为文本更新创建自己的插槽,您可以在那里更改文本框的文本内容:

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

这是 Qt 5 中的一个最小示例,使用 C++11。它与 Python 中的一样简洁。如果您使用的是 Qt 5,那么除了 connect 语句之外,您的问题应该与下面的完全一样。当谈到 Qt 时,这就是 "minimal" 的意思。避免那些不会增加问题的绒毛和样板。在这样一个简单的例子中,window 不需要单独的 class。

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

下面是 Qt 4 中的相同内容 - 请注意没有任何手动内存管理。这就是你的问题理想情况下应该如何寻找 Qt 4,当然没有插槽的实现。您可以使用 connectSlotsByName 或明确的 connect,当然 - 这只是风格问题。

#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"