阅读多篇文章Qt

时间:2014-02-07 06:00:51

标签: qt

我正在做关于显示许多文本的Qt项目。详细说明,在第一次文本显示后,它将关闭然后显示下一个文件。我的问题是只显示最后一个文件。所有链接资源路径都是正确的。请帮我解决一下。提前致谢

#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QFile>
#include <QTextStream>
#include <QMessageBox>
#include <QString>
#include <QStackedWidget>
#include <QTextBrowser>
#include <QStringList>

MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
{
    ui->setupUi(this);

    QStringList L;
    L << ":/sample.txt" << ":/idp.txt";

    foreach (QString str, L){
        QFile file(str);
        if (!file.open(QIODevice::ReadOnly))
            QMessageBox::information(0,"error file path", file.errorString());

        QString name = file.fileName();
        QStringList parts = name.split("/");
        QString lastBit = parts.at(parts.size()-1);
        statusBar()->showMessage(lastBit);

        QTextStream out(&file);
        QString txt = out.readAll();
        QStackedWidget  *temp = new QStackedWidget();
        QTextBrowser *textbrs = new QTextBrowser();
        textbrs->setText(txt);
        temp->addWidget(textbrs);
        setCentralWidget(temp);
        file.close();
    }
}

MainWindow::~MainWindow()
{
    delete ui;
}

2 个答案:

答案 0 :(得分:0)

每次你将用旧的QStackedWidget替换 setCentralWidget(temp);
在Qt帮助文档中使用QStackedWidget的示例如下:

QWidget *firstPageWidget = new QWidget;
QWidget *secondPageWidget = new QWidget;
QWidget *thirdPageWidget = new QWidget;

QStackedWidget *stackedWidget = new QStackedWidget;
stackedWidget->addWidget(firstPageWidget);
stackedWidget->addWidget(secondPageWidget);
stackedWidget->addWidget(thirdPageWidget);

QVBoxLayout *layout = new QVBoxLayout;
layout->addWidget(stackedWidget);
setLayout(layout);

因此,您应该在addWidget()的QStackedWidget中添加多个QTextBrowser,并使用setCentralWiget()一次。
希望对你有所帮助。

答案 1 :(得分:0)

这样的事情:

#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QFile>
#include <QTextStream>
#include <QMessageBox>
#include <QString>
#include <QStackedWidget>
#include <QTextBrowser>
#include <QStringList>

MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
{
    ui->setupUi(this);                                 
    L << ":/sample.txt" << ":/idp.txt";    //Class member of Type QStringList
    textbrs = new QTextBrowser();          //Class member of Type QTextBrowser*
    ui->centralWidget->layout()->addWidget(textbrs);
    timer = new QTimer();                  //Class member of Type QTimer*
    timer->setInterval(5000);
    connect(timer,SIGNAL(timeout()),this,SLOT(slotFileAction()));
    timer->start();

}

MainWindow::~MainWindow()
{
    delete ui;
}

void MainWindow::slotFileAction()
{
   static int count = 0;
   if(count >= L.size())
       timer->stop();
   QString str = L.at(count);
   count++;
   QFile file(str);
    if (!file.open(QIODevice::ReadOnly))
        QMessageBox::information(0,"error file path", file.errorString());

    QString name = file.fileName();
    QStringList parts = name.split("/");
    QString lastBit = parts.at(parts.size()-1);
    statusBar()->showMessage(lastBit);

    QTextStream out(&file);
    QString txt = out.readAll();

    textbrs->setText(txt);        
    file.close();
}