From: Richard W.M. Jones <"Richard W.M. Jones "> Date: Mon, 2 Feb 2009 15:21:51 +0000 (+0000) Subject: Example Qt application. X-Git-Url: http://git.annexia.org/?a=commitdiff_plain;h=845fd34e6e39a4ab2e574f38ac20225ce34cf68a;hp=be15d9d51e251a8dcd4fec88db7a289a37072aa0;p=fedora-mingw.git Example Qt application. --- diff --git a/qt-win/application.qrc b/qt-win/application.qrc new file mode 100644 index 0000000..986ac3b --- /dev/null +++ b/qt-win/application.qrc @@ -0,0 +1,10 @@ + + + copy.png + cut.png + new.png + open.png + paste.png + save.png + + \ No newline at end of file diff --git a/qt-win/copy.png b/qt-win/copy.png new file mode 100644 index 0000000..9d12fc2 Binary files /dev/null and b/qt-win/copy.png differ diff --git a/qt-win/cut.png b/qt-win/cut.png new file mode 100644 index 0000000..2fb9af4 Binary files /dev/null and b/qt-win/cut.png differ diff --git a/qt-win/example.pro b/qt-win/example.pro new file mode 100644 index 0000000..2b0975d --- /dev/null +++ b/qt-win/example.pro @@ -0,0 +1,16 @@ +# Simple application example from: +# http://doc.trolltech.com/4.0/mainwindows-application.html +# +# Process this file with +# QMAKESPEC=fedora-win32-cross qmake-qt4 -win32 example.pro +# and then run 'make'. + +TEMPLATE = app +TARGET = example + +CONFIG += qt warn_on + +HEADERS = mainwindow.h +SOURCES = mainwindow.cpp main.cpp + +RESOURCES += application.qrc diff --git a/qt-win/main.cpp b/qt-win/main.cpp new file mode 100644 index 0000000..4024c69 --- /dev/null +++ b/qt-win/main.cpp @@ -0,0 +1,36 @@ +/**************************************************************************** + ** + ** Copyright (C) 2004-2005 Trolltech AS. All rights reserved. + ** + ** This file is part of the documentation of the Qt Toolkit. + ** + ** This file may be used under the terms of the GNU General Public +** License version 2.0 as published by the Free Software Foundation +** and appearing in the file LICENSE.GPL included in the packaging of +** this file. Please review the following information to ensure GNU +** General Public Licensing requirements will be met: +** http://www.trolltech.com/products/qt/opensource.html +** +** If you are unsure which license is appropriate for your use, please +** review the following information: +** http://www.trolltech.com/products/qt/licensing.html or contact the +** sales department at sales@trolltech.com. + ** + ** This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE + ** WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. + ** + ****************************************************************************/ + + #include + + #include "mainwindow.h" + + int main(int argc, char *argv[]) + { + Q_INIT_RESOURCE(application); + + QApplication app(argc, argv); + MainWindow mainWin; + mainWin.show(); + return app.exec(); + } diff --git a/qt-win/mainwindow.cpp b/qt-win/mainwindow.cpp new file mode 100644 index 0000000..faa104f --- /dev/null +++ b/qt-win/mainwindow.cpp @@ -0,0 +1,296 @@ +/**************************************************************************** + ** + ** Copyright (C) 2004-2005 Trolltech AS. All rights reserved. + ** + ** This file is part of the documentation of the Qt Toolkit. + ** + ** This file may be used under the terms of the GNU General Public +** License version 2.0 as published by the Free Software Foundation +** and appearing in the file LICENSE.GPL included in the packaging of +** this file. Please review the following information to ensure GNU +** General Public Licensing requirements will be met: +** http://www.trolltech.com/products/qt/opensource.html +** +** If you are unsure which license is appropriate for your use, please +** review the following information: +** http://www.trolltech.com/products/qt/licensing.html or contact the +** sales department at sales@trolltech.com. + ** + ** This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE + ** WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. + ** + ****************************************************************************/ + + #include + + #include "mainwindow.h" + + MainWindow::MainWindow() + { + textEdit = new QTextEdit; + setCentralWidget(textEdit); + + createActions(); + createMenus(); + createToolBars(); + createStatusBar(); + + readSettings(); + + connect(textEdit->document(), SIGNAL(contentsChanged()), + this, SLOT(documentWasModified())); + + setCurrentFile(""); + } + + void MainWindow::closeEvent(QCloseEvent *event) + { + if (maybeSave()) { + writeSettings(); + event->accept(); + } else { + event->ignore(); + } + } + + void MainWindow::newFile() + { + if (maybeSave()) { + textEdit->clear(); + setCurrentFile(""); + } + } + + void MainWindow::open() + { + if (maybeSave()) { + QString fileName = QFileDialog::getOpenFileName(this); + if (!fileName.isEmpty()) + loadFile(fileName); + } + } + + bool MainWindow::save() + { + if (curFile.isEmpty()) { + return saveAs(); + } else { + return saveFile(curFile); + } + } + + bool MainWindow::saveAs() + { + QString fileName = QFileDialog::getSaveFileName(this); + if (fileName.isEmpty()) + return false; + + return saveFile(fileName); + } + + void MainWindow::about() + { + QMessageBox::about(this, tr("About Application"), + tr("The Application example demonstrates how to " + "write modern GUI applications using Qt, with a menu bar, " + "toolbars, and a status bar.")); + } + + void MainWindow::documentWasModified() + { + setWindowModified(true); + } + + void MainWindow::createActions() + { + newAct = new QAction(QIcon(":/images/new.png"), tr("&New"), this); + newAct->setShortcut(tr("Ctrl+N")); + newAct->setStatusTip(tr("Create a new file")); + connect(newAct, SIGNAL(triggered()), this, SLOT(newFile())); + + openAct = new QAction(QIcon(":/images/open.png"), tr("&Open..."), this); + openAct->setShortcut(tr("Ctrl+O")); + openAct->setStatusTip(tr("Open an existing file")); + connect(openAct, SIGNAL(triggered()), this, SLOT(open())); + + saveAct = new QAction(QIcon(":/images/save.png"), tr("&Save"), this); + saveAct->setShortcut(tr("Ctrl+S")); + saveAct->setStatusTip(tr("Save the document to disk")); + connect(saveAct, SIGNAL(triggered()), this, SLOT(save())); + + saveAsAct = new QAction(tr("Save &As..."), this); + saveAsAct->setStatusTip(tr("Save the document under a new name")); + connect(saveAsAct, SIGNAL(triggered()), this, SLOT(saveAs())); + + exitAct = new QAction(tr("E&xit"), this); + exitAct->setShortcut(tr("Ctrl+Q")); + exitAct->setStatusTip(tr("Exit the application")); + connect(exitAct, SIGNAL(triggered()), this, SLOT(close())); + + cutAct = new QAction(QIcon(":/images/cut.png"), tr("Cu&t"), this); + cutAct->setShortcut(tr("Ctrl+X")); + cutAct->setStatusTip(tr("Cut the current selection's contents to the " + "clipboard")); + connect(cutAct, SIGNAL(triggered()), textEdit, SLOT(cut())); + + copyAct = new QAction(QIcon(":/images/copy.png"), tr("&Copy"), this); + copyAct->setShortcut(tr("Ctrl+C")); + copyAct->setStatusTip(tr("Copy the current selection's contents to the " + "clipboard")); + connect(copyAct, SIGNAL(triggered()), textEdit, SLOT(copy())); + + pasteAct = new QAction(QIcon(":/images/paste.png"), tr("&Paste"), this); + pasteAct->setShortcut(tr("Ctrl+V")); + pasteAct->setStatusTip(tr("Paste the clipboard's contents into the current " + "selection")); + connect(pasteAct, SIGNAL(triggered()), textEdit, SLOT(paste())); + + aboutAct = new QAction(tr("&About"), this); + aboutAct->setStatusTip(tr("Show the application's About box")); + connect(aboutAct, SIGNAL(triggered()), this, SLOT(about())); + + aboutQtAct = new QAction(tr("About &Qt"), this); + aboutQtAct->setStatusTip(tr("Show the Qt library's About box")); + connect(aboutQtAct, SIGNAL(triggered()), qApp, SLOT(aboutQt())); + + cutAct->setEnabled(false); + copyAct->setEnabled(false); + connect(textEdit, SIGNAL(copyAvailable(bool)), + cutAct, SLOT(setEnabled(bool))); + connect(textEdit, SIGNAL(copyAvailable(bool)), + copyAct, SLOT(setEnabled(bool))); + } + + void MainWindow::createMenus() + { + fileMenu = menuBar()->addMenu(tr("&File")); + fileMenu->addAction(newAct); + fileMenu->addAction(openAct); + fileMenu->addAction(saveAct); + fileMenu->addAction(saveAsAct); + fileMenu->addSeparator(); + fileMenu->addAction(exitAct); + + editMenu = menuBar()->addMenu(tr("&Edit")); + editMenu->addAction(cutAct); + editMenu->addAction(copyAct); + editMenu->addAction(pasteAct); + + menuBar()->addSeparator(); + + helpMenu = menuBar()->addMenu(tr("&Help")); + helpMenu->addAction(aboutAct); + helpMenu->addAction(aboutQtAct); + } + + void MainWindow::createToolBars() + { + fileToolBar = addToolBar(tr("File")); + fileToolBar->addAction(newAct); + fileToolBar->addAction(openAct); + fileToolBar->addAction(saveAct); + + editToolBar = addToolBar(tr("Edit")); + editToolBar->addAction(cutAct); + editToolBar->addAction(copyAct); + editToolBar->addAction(pasteAct); + } + + void MainWindow::createStatusBar() + { + statusBar()->showMessage(tr("Ready")); + } + + void MainWindow::readSettings() + { + QSettings settings("Trolltech", "Application Example"); + QPoint pos = settings.value("pos", QPoint(200, 200)).toPoint(); + QSize size = settings.value("size", QSize(400, 400)).toSize(); + resize(size); + move(pos); + } + + void MainWindow::writeSettings() + { + QSettings settings("Trolltech", "Application Example"); + settings.setValue("pos", pos()); + settings.setValue("size", size()); + } + + bool MainWindow::maybeSave() + { + if (textEdit->document()->isModified()) { + int ret = QMessageBox::warning(this, tr("Application"), + tr("The document has been modified.\n" + "Do you want to save your changes?"), + QMessageBox::Yes | QMessageBox::Default, + QMessageBox::No, + QMessageBox::Cancel | QMessageBox::Escape); + if (ret == QMessageBox::Yes) + return save(); + else if (ret == QMessageBox::Cancel) + return false; + } + return true; + } + + void MainWindow::loadFile(const QString &fileName) + { + QFile file(fileName); + if (!file.open(QFile::ReadOnly | QFile::Text)) { + QMessageBox::warning(this, tr("Application"), + tr("Cannot read file %1:\n%2.") + .arg(fileName) + .arg(file.errorString())); + return; + } + + QTextStream in(&file); + QApplication::setOverrideCursor(Qt::WaitCursor); + textEdit->setPlainText(in.readAll()); + QApplication::restoreOverrideCursor(); + + setCurrentFile(fileName); + statusBar()->showMessage(tr("File loaded"), 2000); + } + + bool MainWindow::saveFile(const QString &fileName) + { + QFile file(fileName); + if (!file.open(QFile::WriteOnly | QFile::Text)) { + QMessageBox::warning(this, tr("Application"), + tr("Cannot write file %1:\n%2.") + .arg(fileName) + .arg(file.errorString())); + return false; + } + + QTextStream out(&file); + QApplication::setOverrideCursor(Qt::WaitCursor); + out << textEdit->toPlainText(); + QApplication::restoreOverrideCursor(); + + setCurrentFile(fileName); + statusBar()->showMessage(tr("File saved"), 2000); + return true; + } + + void MainWindow::setCurrentFile(const QString &fileName) + { + curFile = fileName; + textEdit->document()->setModified(false); + setWindowModified(false); + + QString shownName; + if (curFile.isEmpty()) + shownName = "untitled.txt"; + else + shownName = strippedName(curFile); + + setWindowTitle(tr("%1[*] - %2").arg(shownName).arg(tr("Application"))); + } + + QString MainWindow::strippedName(const QString &fullFileName) + { + return QFileInfo(fullFileName).fileName(); + } diff --git a/qt-win/mainwindow.h b/qt-win/mainwindow.h new file mode 100644 index 0000000..4fef93d --- /dev/null +++ b/qt-win/mainwindow.h @@ -0,0 +1,84 @@ +/**************************************************************************** + ** + ** Copyright (C) 2004-2005 Trolltech AS. All rights reserved. + ** + ** This file is part of the documentation of the Qt Toolkit. + ** + ** This file may be used under the terms of the GNU General Public +** License version 2.0 as published by the Free Software Foundation +** and appearing in the file LICENSE.GPL included in the packaging of +** this file. Please review the following information to ensure GNU +** General Public Licensing requirements will be met: +** http://www.trolltech.com/products/qt/opensource.html +** +** If you are unsure which license is appropriate for your use, please +** review the following information: +** http://www.trolltech.com/products/qt/licensing.html or contact the +** sales department at sales@trolltech.com. + ** + ** This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE + ** WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. + ** + ****************************************************************************/ + + #ifndef MAINWINDOW_H + #define MAINWINDOW_H + + #include + + class QAction; + class QMenu; + class QTextEdit; + + class MainWindow : public QMainWindow + { + Q_OBJECT + + public: + MainWindow(); + + protected: + void closeEvent(QCloseEvent *event); + + private slots: + void newFile(); + void open(); + bool save(); + bool saveAs(); + void about(); + void documentWasModified(); + + private: + void createActions(); + void createMenus(); + void createToolBars(); + void createStatusBar(); + void readSettings(); + void writeSettings(); + bool maybeSave(); + void loadFile(const QString &fileName); + bool saveFile(const QString &fileName); + void setCurrentFile(const QString &fileName); + QString strippedName(const QString &fullFileName); + + QTextEdit *textEdit; + QString curFile; + + QMenu *fileMenu; + QMenu *editMenu; + QMenu *helpMenu; + QToolBar *fileToolBar; + QToolBar *editToolBar; + QAction *newAct; + QAction *openAct; + QAction *saveAct; + QAction *saveAsAct; + QAction *exitAct; + QAction *cutAct; + QAction *copyAct; + QAction *pasteAct; + QAction *aboutAct; + QAction *aboutQtAct; + }; + + #endif diff --git a/qt-win/new.png b/qt-win/new.png new file mode 100644 index 0000000..5777e26 Binary files /dev/null and b/qt-win/new.png differ diff --git a/qt-win/open.png b/qt-win/open.png new file mode 100644 index 0000000..a847203 Binary files /dev/null and b/qt-win/open.png differ diff --git a/qt-win/paste.png b/qt-win/paste.png new file mode 100644 index 0000000..8108bb8 Binary files /dev/null and b/qt-win/paste.png differ diff --git a/qt-win/save.png b/qt-win/save.png new file mode 100644 index 0000000..8e2b5fe Binary files /dev/null and b/qt-win/save.png differ