Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/152.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 在已经包含某些文本的QTextEdit中显示QLineEdit中的文本,并实时更新_C++_Qt_Qtextedit_Qlineedit - Fatal编程技术网

C++ 在已经包含某些文本的QTextEdit中显示QLineEdit中的文本,并实时更新

C++ 在已经包含某些文本的QTextEdit中显示QLineEdit中的文本,并实时更新,c++,qt,qtextedit,qlineedit,C++,Qt,Qtextedit,Qlineedit,要使正在QLineEdit小部件中编写的文本动态显示在已包含某些文本的QTextEdit小部件中,需要执行哪些步骤 例如,假设一个QLineEdit请求输入一个写有“John”的名字。是否可以在包含以下内容的QTextEdit中实时显示它: 姓名是+textFromQLineEdit+,24岁 显示的文本必须动态考虑对QLineEdit所做的更改,以便用户无需按下按钮或按enter键即可看到其姓名 以下是使用来自QLineEdit的信号textChanged()和来自qtexedit的插槽set

要使正在
QLineEdit
小部件中编写的文本动态显示在已包含某些文本的
QTextEdit
小部件中,需要执行哪些步骤

例如,假设一个
QLineEdit
请求输入一个写有“John”的名字。是否可以在包含以下内容的
QTextEdit
中实时显示它:

姓名是+
textFromQLineEdit
+,24岁

显示的文本必须动态考虑对
QLineEdit
所做的更改,以便用户无需按下按钮或按enter键即可看到其姓名

以下是使用来自
QLineEdit
的信号
textChanged()
和来自
qtexedit
的插槽
setText()
将两个小部件相互连接的最低代码(这不允许在来自
QLineEdit
的文本前后添加一些文本):

#包括
#包括
#包括
#包括
#包括
类SmallWindow:publicqwidget
{
Q_对象
公众:
小窗口();
私人:
QLineEdit*名称行;
QTextEdit*文本框;
};
SmallWindow::SmallWindow():QWidget()
{
设置固定大小(300250);
QLineEdit*nameLine=新的QLineEdit;
QTextEdit*textBox=新的QTextEdit;
QWidget::connect(名称行、信号(textChanged(QString))、textBox、SLOT(setText(QString));
QVBoxLayout*布局=新的QVBoxLayout;
布局->添加小部件(名称行);
布局->添加小部件(文本框);
QGroupBox*组=新的QGroupBox(此);
组->设置布局(布局);
}
int main(int argc,char*argv[])
{
QApplication应用程序(argc、argv);
小窗;
window.show();
app.exec();
}
#包括“main.moc”
如何在
QLineEdit
文本前后保留文本,并实时更新
qtexedit
框?

创建特殊插槽:

void SmallWindow::pasteText(const QString& str)
{
    textBox->setText(QString("The name is %1 , age 24").arg(str)); 
}
不要使用
textChanged()
信号,因为您只需要用户接受的一个名称,所以您需要
QLineEdit::editingFinished()
(或者
QLineEdit::returnPressed()
,这取决于您的需要)

另外,您不需要
QWidget::connect
,因为您在
QObject
子类中编写了此代码,所以它不是必需的

还有以下几行:

QLineEdit *nameLine = new QLineEdit;
QTextEdit *textBox = new QTextEdit;
应该是:

nameLine = new QLineEdit;
textBox = new QTextEdit;

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

小部件名称行和文本框已经在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

这是Qt5中使用C++11的一个最小示例。它与Python一样简洁。如果您使用的是Qt5,那么您的问题应该与下面的问题完全相同,只需使用
connect
语句即可。这就是Qt中“最小”的含义。避免那些不会增加问题的陈词滥调。在这样一个简单的示例中,不需要为窗口使用单独的类

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

第一:谢谢你的提示!第二:我试过你的代码,但在我的电脑里,我必须用
连接
命令中的
这个
来替换
文本框
。。。知道为什么吗?@jrojasqu哦,是的,对不起,是我的打字错误(我复制粘贴了你的代码,忘记了更改所有东西)是的,你需要
这个
,因为你试图使用SmallWindow的插槽,而不是QTextEdit(QTextEdit根本没有粘贴文本插槽)。
#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
#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);
}
#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();
}
#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"