简体   繁体   中英

How can I display of 2 different QTextEdit values into a QLabel

Im trying to simply display of 2 different QTextEdit value into a QLabel . I have tried for a single QTextEdit but couldn't display the value of both QTextEdit .

void MainWindow::on_pushButton_clicked()
{    
  ui->label_az->setText(ui->textEdit_ra1->toPlainText());
  ui->label_az->setText(ui->textEdit_ra2->toPlainText());
}

It doesn't display the QTextEdit values when I click on pushbutton . Thank you in advance

Just to summarize our comments into a single post: QLabel::setText replaces the content of the label, so you have to create the whole string before and set it once. Code below will do it:

void MainWindow::on_pushButton_clicked()
{
  ui->label_az->setText(
    ui->textEdit_ra1->toPlainText() +
    " " + // use here the separator you find more convenient
    ui->textEdit_ra2->toPlainText());
}

The second setText() call replaces the label's text. You want to combine both texts into a single label text, like this:

label->setText(text_1->toPlainText() + "\n" + text_2->toPlainText());

Here's a complete example program, to give context:

#include <QWidget>
#include <QBoxLayout>
#include <QTextEdit>
#include <QPushButton>
#include <QLabel>
#include <QApplication>

#include <memory>

int main(int argc, char **argv)
{
    QApplication app{argc, argv};

    const auto w = std::make_unique<QWidget>();
    const auto window = w.get();
    const auto layout = new QVBoxLayout(window);
    const auto text_1 = new QTextEdit(window);
    layout->addWidget(text_1);
    const auto text_2 = new QTextEdit(window);
    layout->addWidget(text_2);
    const auto button = new QPushButton("Push Me!", window);
    layout->addWidget(button);
    const auto label = new QLabel(window);
    layout->addWidget(label);

    QObject::connect(button, &QPushButton::pressed,
                     label, [=]() { label->setText(text_1->toPlainText() + "\n" + text_2->toPlainText()); });

    window->show();
    return app.exec();
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM