From 845fd34e6e39a4ab2e574f38ac20225ce34cf68a Mon Sep 17 00:00:00 2001 From: "Richard W.M. Jones" Date: Thu, 1 Jan 1970 00:00:00 +0000 Subject: [PATCH] Example Qt application. --- qt-win/application.qrc | 10 ++ qt-win/copy.png | Bin 0 -> 431 bytes qt-win/cut.png | Bin 0 -> 635 bytes qt-win/example.pro | 16 +++ qt-win/main.cpp | 36 ++++++ qt-win/mainwindow.cpp | 296 +++++++++++++++++++++++++++++++++++++++++++++++++ qt-win/mainwindow.h | 84 ++++++++++++++ qt-win/new.png | Bin 0 -> 464 bytes qt-win/open.png | Bin 0 -> 607 bytes qt-win/paste.png | Bin 0 -> 511 bytes qt-win/save.png | Bin 0 -> 503 bytes 11 files changed, 442 insertions(+) create mode 100644 qt-win/application.qrc create mode 100644 qt-win/copy.png create mode 100644 qt-win/cut.png create mode 100644 qt-win/example.pro create mode 100644 qt-win/main.cpp create mode 100644 qt-win/mainwindow.cpp create mode 100644 qt-win/mainwindow.h create mode 100644 qt-win/new.png create mode 100644 qt-win/open.png create mode 100644 qt-win/paste.png create mode 100644 qt-win/save.png 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 0000000000000000000000000000000000000000..9d12fc200f06717919679e1f624d4adb39ba73f6 GIT binary patch literal 431 zcmV;g0Z{&lP)6otPef(q(VQSc3fBDx>nV3@b^ZF~yd>dKA36`^i~ zf(osry|Wn8QCnv`5PB1moWqw}_|XWEH^!7FsoPgP&qHgCQVOM1THmkLYPI__02FLt zjNx!Nuv{(ysMqWGzE2cIG#U*Y$9Xn?OmI9NUpm`)sZ`1&uoB<*Wj>#a<2diIkQ=w( z1gFypr4*G)1<&(x)8z3;8ECE9@AoN>-EN2Lx+taoO~Y=StBQzy&gmAlp&V@x)|eZ5|0`k|bhv+naXc*L_b|9`-q{27>|BYL&%e zk**cUkuipTzyAOa;BvVz9*>z$rvTf$vj7iKt->&r$z&qK;ZWM`wg8{6F8J@)9swVA Z^EU#7&n0vVlN$g4002ovPDHLkV1lczy;uMM literal 0 HcmV?d00001 diff --git a/qt-win/cut.png b/qt-win/cut.png new file mode 100644 index 0000000000000000000000000000000000000000..2fb9af4c8da08b8ba5c626c49fdbd5291fcafc53 GIT binary patch literal 635 zcmV->0)+jEP)9S(;j5C{mM{YxANOyQmG{6 za#<>silozNQB_s;_xELUb5lk}M!H4*zy{E4HmTKW6bc0_77I?NlT0Rq-EL=aaFDUF zF><-wiN9L`JRT2`NQ8VokH_P|=kpN=1W2V)D2hTb80WUUGCiQ z%IF1TSw-HAB6qC_FlPZI7K^d8w1lQcNcTvGN+V1v`_G5 z4IWqEejNsY7b`0(I2;b9rlwe5U+3W90JqzX&1R$BZj($VnSYxkbP1*x;A06M48Z#q zSO76ilT0QvKGvud%!eFR#GuGf>WfVZb9n2atBVEuBtB zI-QPmiO1tI>Ou<7kHxy=MNKsFmUs@ zkrGy`_1;N0o_+{hTTuHB)t^4pfwjKII7UQuBH8Q6hap6>^xFO}=7#9C-kbS2KL9un VC~OMI>LLID002ovPDHLkV1iae4VVA` literal 0 HcmV?d00001 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 0000000000000000000000000000000000000000..5777e26d6908fc2b9e2cd84a87fb00bc1218f450 GIT binary patch literal 464 zcmV;>0WbcEP)ws%p!_3f#r=lki~?|B~hCnW*- z2Pe&-^0UfxI*pBn`umhG9rMrBW%WX_^d& z!xD%W0m>k=Ni1RVqq z1$7wo2b7&Ug*!#ip*jc^gsxj@ z^S~SaNA&|$TkQA!2^t3OR|m?SzQZlHI#A&NgFpzFXsoNT9l?4F-1rsr0YihQPKK|Z z@1e7;5udM&==%k3jy<8br-k0`78+`Ip^c&G_$86a7Q#R$Ab>MIU^o;Cjb1rhkJbjQ z4MrQZS({Qxa``MPD=BV2TBfbzC@#jou=VH4Hesbltc`fpJ&xo zD5=rfaO=(l!p(;P{6GZ2%VkNYmN_(Vfhx0zbP{Ar8YR`*TgG6m#Td=K$5Q~K8wLR6 zavmZh36z(SjSIBWXk)O(Vy!`Ijg*pipT81MiU&XjpbQ`qc}7nt!2i~&PzUF+X8xDE{Sm(Krl{Q%PV9hhENr5OMK002ovPDHLkV1jle2VnpJ literal 0 HcmV?d00001 diff --git a/qt-win/paste.png b/qt-win/paste.png new file mode 100644 index 0000000000000000000000000000000000000000..8108bb87e28f850809c115ebf049b57e2f09b56a GIT binary patch literal 511 zcmVmPP9NJCAR#c=rtlc)HJl{PF%&erbM|^?KcR@}JQKINA{I9EZ`@ zH<%_&r&AO~K?s2m!e0l^qS5G!9l(4E$B*vUs;Xk!HpAf%fLJVs<2Wo93*zxOhGF>D zeSkOL1*B3bbX^Zs-t0qw*&L3Kwt&fG!eB5!2;m0{AxI<=luD&A;BE#d0SCISlgVTr z?%;L?XX~C*6ov74Ouyd;pja$28jWyWmt-UG-#W-(qB_VweF>WY3cv~002ovPDHLkV1ji} B<3a!c literal 0 HcmV?d00001 diff --git a/qt-win/save.png b/qt-win/save.png new file mode 100644 index 0000000000000000000000000000000000000000..8e2b5fef76b9cc20658257c7c56182edfa5e2a58 GIT binary patch literal 503 zcmV<1wH9OL=gn>1z+f)-uxm( z&mQ~;<|as+euP$NDN2q3MYO2dke!xLW7clkd1l5#-03bxLG*!#S$5|C`~T+!{!={% z++Xx6Fa1&@9!~slQoebECEfTs@MmDIERIg^PE% zeCZseQfZ)b4r7dOYb~$qiePkt&5ha>fWVtLOVs}0#nxIxB-dJN)OLs6PUyq=1lC$+ z&s_80sjGtb0S+g#!Ym*)(fSX7tb6L3iUG71M9lBD zV0f|AAP5}WN@0x6LH3dZ_WP3FY98RML2DsWeZG8daDJ``97bJxwa(Tj$JoS4W@b+R z3NV>#o0^1$g?Vn?e#pvl6=Mu>9Mjo-%hzv7?hw;=@c8|^x(D==KFwy6W5-Vsh9O}X z((CmYA0Go43V5>hu~A)q`4kbu&Q3_X-R8#iIie^+DMcK|B+1?&t+5_3LcX2>cZ