Qt大牛请进,QDomDocument加载效率问题??

tantingfang 发布于 2015/08/05 16:59
阅读 217
收藏 0

最近用了Qt解析xml文件,奇怪的是QDomDocument加载同一文件,第一次加载比较快,第二次再次加载速度慢了6-7倍?有没有人碰到过类似问题??

1.main.cpp

#include<QLocale>
#include<QTranslator>
int main(int argc, char *argv[])
{
    QApplication app(argc, argv); 

    VSCDTestMainWin mainWin;
    mainWin.showMaximized();
    mainWin.show();
    return app.exec();
}

2.mainwindow.h

#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include <QtGui>
#include <QMainWindow>
#include <QAction>
#include <QListWidget>
#include <QMenu>
#include <QTextEdit>
#include <QMap>
#include <QPointer>
#include<QDomDocument>

class VSCDTestMainWin : public QMainWindow
{
    Q_OBJECT

public:
    VSCDTestMainWin();
    ~VSCDTestMainWin();
public slots:
    void openSlot();
    void closeSlot();
private:
    QDomDocument *m_doc;
   
};
#endif

3.mainwindow.cpp

#include "mainwindow.h"
#include <QAction>
#include <QApplication>
#include <QComboBox>
#include <QFile>
#include <QFileDialog>
#include <QFileInfo>
#include <QMenu>
#include <QMenuBar>
#include<QMenu>
#include<QFileDialog>
#include<QTime>
//#################MainWindow################
VSCDTestMainWin::VSCDTestMainWin()       
{   
    m_doc = new QDomDocument("test");
    QAction *act = new QAction(tr("open"),this);
    connect(act,SIGNAL(triggered(bool)),this,SLOT(openSlot()));
    QMenu *fileMenu = this->menuBar()->addMenu(tr("File"));
    fileMenu->addAction(act);

    act = new QAction(tr("close"),this);
    connect(act,SIGNAL(triggered(bool)),this,SLOT(closeSlot()));
    fileMenu->addAction(act);
}
VSCDTestMainWin::~VSCDTestMainWin()
{
    delete m_doc;  
}
void VSCDTestMainWin::openSlot()
{
    QString fileName = QFileDialog::getOpenFileName(this,tr("Select file......"),".");

    QApplication::setOverrideCursor(Qt::WaitCursor);
    m_doc->clear();

    QTime time1 = QTime::currentTime();
    QFile file(fileName);
    if (!file.open(QIODevice::ReadOnly))
        return;
    if (!m_doc->setContent(&file)) {
        file.close();
        return;
    }    
    QTime time2 = QTime::currentTime();
    std::cout<<"**********qt  step3 cost time=="<<time1.msecsTo(time2)<<",iedContent.size=="<<file.size()<<std::endl;
    file.close();
    QApplication::restoreOverrideCursor();
}
void VSCDTestMainWin::closeSlot()
{

}


加载中
返回顶部
顶部