mainwindow.cpp Example File
itemviews/simpledommodel/mainwindow.cpp
#include "dommodel.h"
#include "mainwindow.h"
#include <QDomDocument>
#include <QTreeView>
#include <QMenuBar>
#include <QFileDialog>
MainWindow::MainWindow() : QMainWindow(), model(0)
{
fileMenu = menuBar()->addMenu(tr("&File"));
fileMenu->addAction(tr("&Open..."), this, &MainWindow::openFile, QKeySequence::Open);
fileMenu->addAction(tr("E&xit"), this, &QWidget::close, QKeySequence::Quit);
model = new DomModel(QDomDocument(), this);
view = new QTreeView(this);
view->setModel(model);
setCentralWidget(view);
setWindowTitle(tr("Simple DOM Model"));
}
void MainWindow::openFile()
{
QString filePath = QFileDialog::getOpenFileName(this, tr("Open File"),
xmlPath, tr("XML files (*.xml);;HTML files (*.html);;"
"SVG files (*.svg);;User Interface files (*.ui)"));
if (!filePath.isEmpty()) {
QFile file(filePath);
if (file.open(QIODevice::ReadOnly)) {
QDomDocument document;
if (document.setContent(&file)) {
DomModel *newModel = new DomModel(document, this);
view->setModel(newModel);
delete model;
model = newModel;
xmlPath = filePath;
}
file.close();
}
}
}