在QTabWidget

时间:2018-05-15 15:15:57

标签: qt qtabwidget

我想读取数据表单文本文件并在tabWidget中添加数字图表(tab1) 但是在显示时,新小部件已打开,我该如何解决这个问题?

void mainWindow::readfile(){

    QFile config(":/new/prefix1/3.txt");
    config.open(QIODevice::ReadOnly);
    if(config.isOpen()){
        QTextStream stream(&config);
        while (!stream.atEnd()){
         line = stream.readLine().split('\t');
        //qDebug()<<line;
        bool allOk(true);
        bool ok;
        for (int x = 0; x <= line.count()-1 && allOk; x++) {
        val.append(line.at(x).toInt(&ok));
        allOk &= ok;
            }
        }
}
    else
        qDebug()<<"not opened";
     ui->stackedWidget->setCurrentIndex(8);
     on_ecg_destroyed();
}

void mainWindow::on_ecg_destroyed()
{
    QLineSeries *series = new QLineSeries();
        for(int y=0;y<288;y++)
            series->append(y,val[y]);

    QChart *chart = new QChart();
    chart->legend()->hide();
    chart->addSeries(series);
    chart->createDefaultAxes();
    chart->setTitle("line chart");
     QChartView *chartView = new QChartView(chart);
    chartView->setRenderHint(QPainter::Antialiasing);
    setCentralWidget(chartView);
    resize(400, 300);
    show();
}

我使用QStackWidget来定义页面,在其中一个(页面)中,我使用QTabWidget和四个标签,我想在标签1中创建一个图表。 QTabWidget位于QStackWidget索引8中, 阅读完文件后,请在第{8}页的tab1显示图表。

1 个答案:

答案 0 :(得分:2)

在您的代码中,您需要添加chartview以更正QTabWidget的标签,您可以创建布局,将chartview添加到创建的布局,然后将tab1布局设置为该新布局:

QChart *chart = new QChart();
chart->legend()->hide();
chart->addSeries(series);
chart->createDefaultAxes();
chart->setTitle("line chart");
QChartView *chartView = new QChartView(chart);
chartView->setRenderHint(QPainter::Antialiasing);
// create layout
QGridLayout layout;
layout.addWidget(chartView);
this->ui->tab1->setLayout(&layout);
// position view to chartview
this->ui->stackedWidget->setCurrentIndex(8);
this->ui->tabWidget->setCurrentIndex(this->ui->tabWidget->indexOf(this->ui->tab1));