【问题标题】:Display text from QLineEdit in a QTextEdit already containing some text and update it in real time在已经包含一些文本的 QTextEdit 中显示来自 QLineEdit 的文本并实时更新它
【发布时间】:2015-09-15 11:16:39
【问题描述】:

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

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

名字是 + textFromQLineEdit + ,24岁 ?

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

以下是使用来自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框?

【问题讨论】:

    标签: c++ qt qtextedit qlineedit


    【解决方案1】:

    创建特殊槽:

    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;
    

    【讨论】:

    • 首先:感谢您的所有提示!第二:我尝试了您的代码,但在我的计算机中,我必须在 connect 命令中将 textBox 替换为 this 才能使其正常工作......有什么想法吗?
    • @jrojasqu 哦,是的,对不起,这是我的错字(我复制粘贴了您的代码,忘记更改所有内容)是的,您需要 this,因为您尝试使用 SmallWindow 的插槽,而不是一个 QTextEdit(QTextEdit 根本没有 pasteText 插槽)。
    【解决方案2】:

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

    小部件 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);
    }
    

    【讨论】:

      【解决方案3】:

      这是 Qt 5 中使用 C++11 的最小示例。它与 Python 中的一样简洁。如果您使用的是 Qt 5,那么您的问题应该与下面的完全一样,除了 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();
      }
      

      下面是 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"
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2019-10-05
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多