mainwindow.cpp Example File | Qt 4.8 (original) (raw)

xml/streambookmarks/mainwindow.cpp

#include

#include "mainwindow.h" #include "xbelreader.h" #include "xbelwriter.h"

MainWindow::MainWindow() { QStringList labels; labels << tr("Title") << tr("Location");

treeWidget = new [QTreeWidget](qtreewidget.html);
treeWidget->header()->setResizeMode([QHeaderView](qheaderview.html)::Stretch);
treeWidget->setHeaderLabels(labels);
setCentralWidget(treeWidget);

createActions();
createMenus();

statusBar()->showMessage(tr("Ready"));

setWindowTitle(tr("QXmlStream Bookmarks"));
resize(480, 320);

}

void MainWindow::open() { #if defined(Q_OS_SYMBIAN)

[QString](qstring.html) bookmarksFolder =
        [QDesktopServices](qdesktopservices.html)::storageLocation([QDesktopServices](qdesktopservices.html)::DataLocation).left(1);
bookmarksFolder.append(":/Data/qt/saxbookmarks");
[QDir](qdir.html)::setCurrent(bookmarksFolder);

#endif QString fileName = QFileDialog::getOpenFileName(this, tr("Open Bookmark File"), QDir::currentPath(), tr("XBEL Files (*.xbel *.xml)")); if (fileName.isEmpty()) return;

treeWidget->clear();

[QFile](qfile.html) file(fileName);
if (!file.open([QFile](qfile.html)::ReadOnly | [QFile](qfile.html)::Text)) {
    [QMessageBox](qmessagebox.html)::warning(this, tr("QXmlStream Bookmarks"),
                         tr("Cannot read file %1:\n%2.")
                         .arg(fileName)
                         .arg(file.errorString()));
    return;
}

XbelReader reader(treeWidget);
if (!reader.read(&file)) {
    [QMessageBox](qmessagebox.html)::warning(this, tr("QXmlStream Bookmarks"),
                         tr("Parse error in file %1:\n\n%2")
                         .arg(fileName)
                         .arg(reader.errorString()));
} else {
    statusBar()->showMessage(tr("File loaded"), 2000);
}

}

void MainWindow::saveAs() { #if defined(Q_OS_SYMBIAN)

[QString](qstring.html) bookmarksFolder =
        [QDesktopServices](qdesktopservices.html)::storageLocation([QDesktopServices](qdesktopservices.html)::DataLocation).left(1);
bookmarksFolder.append(":/Data/qt/saxbookmarks");
[QDir](qdir.html)::setCurrent(bookmarksFolder);

#endif QString fileName = QFileDialog::getSaveFileName(this, tr("Save Bookmark File"), QDir::currentPath(), tr("XBEL Files (*.xbel *.xml)")); if (fileName.isEmpty()) return;

[QFile](qfile.html) file(fileName);
if (!file.open([QFile](qfile.html)::WriteOnly | [QFile](qfile.html)::Text)) {
    [QMessageBox](qmessagebox.html)::warning(this, tr("QXmlStream Bookmarks"),
                         tr("Cannot write file %1:\n%2.")
                         .arg(fileName)
                         .arg(file.errorString()));
    return;
}

XbelWriter writer(treeWidget);
if (writer.writeFile(&file))
    statusBar()->showMessage(tr("File saved"), 2000);

}

void MainWindow::about() { QMessageBox::about(this, tr("About QXmlStream Bookmarks"), tr("The QXmlStream Bookmarks example demonstrates how to use Qt's " "QXmlStream classes to read and write XML documents.")); }

void MainWindow::createActions() { openAct = new QAction(tr("&Open..."), this); openAct->setShortcuts(QKeySequence::Open); connect(openAct, SIGNAL(triggered()), this, SLOT(open()));

saveAsAct = new [QAction](qaction.html)(tr("&Save As..."), this);
saveAsAct->setShortcuts([QKeySequence](qkeysequence.html)::SaveAs);
connect(saveAsAct, SIGNAL(triggered()), this, SLOT(saveAs()));

exitAct = new [QAction](qaction.html)(tr("E&xit"), this);
exitAct->setShortcuts([QKeySequence](qkeysequence.html)::Quit);
connect(exitAct, SIGNAL(triggered()), this, SLOT(close()));

aboutAct = new [QAction](qaction.html)(tr("&About"), this);
connect(aboutAct, SIGNAL(triggered()), this, SLOT(about()));

aboutQtAct = new [QAction](qaction.html)(tr("About &Qt"), this);
connect(aboutQtAct, SIGNAL(triggered()), qApp, SLOT(aboutQt()));

}

void MainWindow::createMenus() { fileMenu = menuBar()->addMenu(tr("&File")); fileMenu->addAction(openAct); fileMenu->addAction(saveAsAct); fileMenu->addAction(exitAct);

menuBar()->addSeparator();

helpMenu = menuBar()->addMenu(tr("&Help"));
helpMenu->addAction(aboutAct);
helpMenu->addAction(aboutQtAct);

}

© 2016 The Qt Company Ltd. Documentation contributions included herein are the copyrights of their respective owners. The documentation provided herein is licensed under the terms of the GNU Free Documentation License version 1.3 as published by the Free Software Foundation. Qt and respective logos are trademarks of The Qt Company Ltd. in Finland and/or other countries worldwide. All other trademarks are property of their respective owners.