简体   繁体   中英

I can't seem to print several lines of data in qt creator. Program overwrites all output except for the last line

My intended output: To print the time and number of bacteria for an exponential equation. I'm trying to print every data point up until time t, for instance if I'm finding the growth up until 50 hours in, I want to print the number of bacteria at time 0, 1, 2, ..., 49, 50. I am trying to have each output on a new line as well.

So here is my code:

void MainWindow::on_pushButtonCalc_clicked()
{
    QString s;
    double t = ui->t->text().toDouble();
    double k = ui->k->text().toDouble();
    double n0 = ui->n0->text().toDouble();

    /*double example;
    example= k;
    s = s.number(example);
    ui->textOutput->setText(s);*/

    for(int c = 0; c<t; ++c)
    {
        double nt = n0*exp(k*t);
        s = s.number(nt);
        ui->textOutput->setText(s);

    }

}

I've tried quite a few different outputs, and have also been trying to append new points to an array and print the array, but I haven't had too much luck with that either. I'm somewhat new to c++, and very new to qt.

Thank you for any suggestions.

The QTextEdit::setText function is going to replace the contents of the text edit with the parameter you pass in. Instead, you can use the append function:

for(int c = 0; c<t; ++c)
{
    double nt = n0*exp(k*t);
    s = QString::number(nt);
    ui->textOutput->append(s);
}

Note also that since QString::number is a static function, you don't need an instance to call it.

Alternately, you can create the string in your loop and then set it to the text edit using setText :

for (int c = 0; c<t; ++c)
{
    double nt = n0*exp(k*t);
    s += QString("%1 ").arg(nt);
}
ui->textOutput->setText(s);

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