Pergunta

I am trying to connect QTextEdit to QTextBrowser, so the text browser widget outputs what is entered in text edit widget. As a signal I used textChanged(), and as a slot I used setText(QString). And these two don't have same parameters.

If I used QLineEdit instead of QTextEdit, in that case there is textChanged(QString) function which is compatible with the slot,but I need to make it work with QTextEdit. Here is the code:

#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QtWidgets>

MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
{
    QWidget * mainWidget=new QWidget(this);
    ui->setupUi(this);
    QTextEdit * mainTextEdit=new QTextEdit();
    QTextBrowser * textDisplay=new QTextBrowser();

    connect(mainTextEdit,SIGNAL( textChanged() ),
            textDisplay,SLOT( setText(QString) ) );

    QHBoxLayout * Alayout=new QHBoxLayout();
    Alayout->addWidget(mainTextEdit);
    Alayout->addWidget(textDisplay);
    mainWidget->setLayout(Alayout);
    setCentralWidget(mainWidget);
}

MainWindow::~MainWindow()
{
    delete ui;
}
Foi útil?

Solução

Thankfully, the QTextEdit and QTextBrowser are views onto a QTextDocument model. So, you can simply set the editor's document on the browser. QTextBrowser::setDocument is semantically equivalent to QAbstractItemView::setModel:

textDisplay->setDocument(mainTextEdit->document());

In Qt, there are really two basic model classes: QAbstractItemModel and QTextDocument. A QTextDocument is a model in its own model-view framework. We simply set another view onto the document that the editor operates on. The editor allows modifications to the model, the browser doesn't. It's no different from using the same model on two QListViews, etc.

A QTextEditor is a view with a default model (document). You can replace that default model with one from another view, or even with one that you yourself provide. You could have multiple editors all displaying the same QTextDocument document and allowing editing of it, in parallel. You can also have multiple browsers doing the same.

Complete example:

#include <QApplication>
#include <QTextEdit>
#include <QTextBrowser>
#include <QHBoxLayout>

int main(int argc, char *argv[])
{
   QApplication a(argc, argv);
   QWidget window;
   QHBoxLayout layout(&window);
   QTextEdit edit;
   QTextBrowser browser;
   layout.addWidget(&edit);
   layout.addWidget(&browser);
   browser.setDocument(edit.document());
   window.show();
   return a.exec();
}

Outras dicas

I would do it in the following way:

Declare the pointers to the text edit and text browser widgets as member variables in the class,

Create a slot onTextChanged() in MainWindow class that will be called as soon as the text edit is changed and setup the connection as:

connect(mainTextEdit, SIGNAL(textChanged()), this, SLOT(onTextChanged()));

Implement the onTextChanged() slot in the following way:

MainWindow::onTextChanged()
{
    QString text = mainTextEdit->toPlainText();
    textDisplay->setPlainText(text); 
}
Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top