⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 mainwindow.cpp

📁 linux 下 qt 编写的阅读器实例!
💻 CPP
📖 第 1 页 / 共 2 页
字号:
/******************************************************************************** Copyright (C) 2004-2007 Trolltech ASA. All rights reserved.**** This file is part of the example classes 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://trolltech.com/products/qt/licenses/licensing/opensource/**** If you are unsure which license is appropriate for your use, please** review the following information:** http://trolltech.com/products/qt/licenses/licensing/licensingoverview** or contact the sales department at sales@trolltech.com.**** In addition, as a special exception, Trolltech gives you certain** additional rights. These rights are described in the Trolltech GPL** Exception version 1.0, which can be found at** http://www.trolltech.com/products/qt/gplexception/ and in the file** GPL_EXCEPTION.txt in this package.**** In addition, as a special exception, Trolltech, as the sole copyright** holder for Qt Designer, grants users of the Qt/Eclipse Integration** plug-in the right for the Qt/Eclipse Integration to link to** functionality provided by Qt Designer and its related libraries.**** Trolltech reserves all rights not expressly granted herein.** ** Trolltech ASA (c) 2007**** 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 <QtGui>#include "mainwindow.h"MainWindow::MainWindow(){    init();    setCurrentFile("");}MainWindow::MainWindow(const QString &fileName){    init();    loadFile(fileName);}void MainWindow::closeEvent(QCloseEvent *event){        writeSettings();        event->accept();        qApp->quit();}void MainWindow::open(){    QString fileName = QFileDialog::getOpenFileName(this,                    "QReadBook - Open File",                    "",                    "Plain Text Files (*.txt)");;    if (!fileName.isEmpty()) {        MainWindow *existing = findMainWindow(fileName);        if (existing) {            existing->show();            existing->raise();            existing->activateWindow();            return;        }        else        {			QSettings settings("Qreadbook", "Configuration");	        if (pathfilename != tr(""))	              settings.setValue(pathfilename, textEdit->verticalScrollBar()->value());			pathfilename = fileName;            loadFile(fileName);        }     }}void MainWindow::about(){   QString about = "QReadBook is a small and simple book reader programmed in C++ using the Qt4.3 toolkit.\n\nHomepage: http://code.google.com/p/qreadbook\n\nAuthor: Weiwei Fang (http://hi.baidu.com/vvfang)"; QMessageBox::information( this,"QReadBook - About", about , QMessageBox::Ok );}void MainWindow::init(){    setAttribute(Qt::WA_DeleteOnClose);    isUntitled = true;    fullscreen = false;    mousecontrol = true;    step = 2;    splitter = new QSplitter(Qt::Horizontal);    tree = new QTreeView(splitter);    dirmodel = new QDirModel();    tree->setModel(dirmodel);    tree->setColumnHidden(1, true);    tree->setColumnHidden(3, true);    splitter->addWidget(tree);    tree->hide();	timedialog = new TimeDialog(this);	pathfilename = tr("");    font = new QFont();    textEdit = new QTextEdit;    splitter->addWidget(textEdit);    splitter->setStretchFactor(1,1);    setCentralWidget(splitter);    setWindowIcon(QIcon(":/images/qreader.png"));    setMouseTracking(true);    textEdit->setAutoFillBackground(true);    textEdit->setReadOnly(true);    trayIcon = new QSystemTrayIcon(this);    trayIcon->setIcon(QIcon(":/images/qreader.png"));    createActions();    createTrayIcon();    createMenus();    createStatusBar();    readSettings();}void MainWindow::createActions(){    openAct = new QAction(tr("&Open..."), this);    openAct->setShortcut(tr("Ctrl+O"));    openAct->setStatusTip(tr("Open an existing file"));    connect(openAct, SIGNAL(triggered()), this, SLOT(open()));    closeAct = new QAction(tr("&Close"), this);    closeAct->setShortcut(QKeySequence( Qt::Key_Escape ));    closeAct->setStatusTip(tr("Close this window"));    connect(closeAct, SIGNAL(triggered()), this, SLOT(close()));    aboutAct = new QAction(tr("&About"), this);    aboutAct->setShortcut ( QKeySequence( Qt::Key_F1 ) );    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()));    fontAct = new QAction(tr("Font"), this);    fontAct->setStatusTip(tr("Font"));    fontAct->setShortcut ( QKeySequence( Qt::Key_F2 ) );    connect(fontAct, SIGNAL(triggered()), this, SLOT(fontSelectSlot()));  fontcolorAct = new QAction(tr("Font Color"), this);  fontcolorAct->setStatusTip(tr("Font Color"));  fontcolorAct->setShortcut(QKeySequence( Qt::Key_F3 ) );  connect(fontcolorAct, SIGNAL(triggered()), this, SLOT(fontColorSlot()));  bgcolorAct = new QAction(tr("Background Color"), this);  bgcolorAct->setStatusTip(tr("Background Color"));  bgcolorAct->setShortcut(QKeySequence( Qt::Key_F4 ) );  connect(bgcolorAct, SIGNAL(triggered()), this, SLOT(bgColorSlot()));  switchControlAct = new QAction("Switch Control", this);  switchControlAct->setShortcut ( QKeySequence( Qt::Key_F5) );  switchControlAct->setStatusTip("Switch mouse/autoscroll control of QReadBook");  connect(switchControlAct, SIGNAL( triggered() ), this , SLOT(swithchControlSlot() ) );  addstepAct = new QAction("AddStep", this);  addstepAct->setShortcut ( QKeySequence( Qt::Key_F6) );  connect(addstepAct, SIGNAL(triggered() ), this , SLOT(addStepSlot() ) );  minusstepAct = new QAction("MinusStep", this);  minusstepAct->setShortcut ( QKeySequence( Qt::Key_F7) );  connect(minusstepAct, SIGNAL(  triggered() ), this , SLOT(minusStepSlot() ) );  lefttreehideAct = new QAction("Directory Display", this);  lefttreehideAct->setShortcut ( QKeySequence( Qt::Key_Tab) );  connect(lefttreehideAct, SIGNAL(  triggered() ), this , SLOT(dirtreeDisplaySlot() ) );  view_fullScreenAct = new QAction("Fullscreen Mode", this);  view_fullScreenAct->setShortcut ( QKeySequence( Qt::Key_F11 ) );  view_fullScreenAct->setCheckable(true);  view_fullScreenAct->setStatusTip("Enables the fullscreen mode of QReadBook");  connect(view_fullScreenAct, SIGNAL( toggled(bool) ), this , SLOT(  fullScreenModeSlot() ) );  clockdialogAct = new QAction("Alarm Setting", this);  clockdialogAct->setShortcut( QKeySequence( Qt::Key_F8) );  connect(clockdialogAct, SIGNAL(  triggered() ), this , SLOT(opentimedialogSlot()));  timer = new QTimer(this);  connect(timer, SIGNAL(timeout()), this, SLOT(autoScrollSlot()));  connect(tree, SIGNAL(doubleClicked(const QModelIndex&)), this, SLOT(dbclickopenfileSlot(const QModelIndex&)));  minimizeAct = new QAction(tr("Tray<->Window"), this);  minimizeAct->setShortcut ( QKeySequence( Qt::Key_F12 ) );  minimizeAct->setStatusTip("Minimize QReadBook to tray");  connect(minimizeAct,SIGNAL(triggered()), this, SLOT(minitotraySlot()));  connect(trayIcon, SIGNAL(activated(QSystemTrayIcon::ActivationReason)), this, SLOT(dbclicktraySlot(QSystemTrayIcon::ActivationReason)));}void MainWindow::opentimedialogSlot(){	if (timedialog->exec() == QDialog::Accepted)	{		//printf("fuck\n");	}}void MainWindow::dbclicktraySlot(QSystemTrayIcon::ActivationReason reason){     switch (reason) {     case QSystemTrayIcon::Trigger:     case QSystemTrayIcon::DoubleClick:         minitotraySlot();         break;     case QSystemTrayIcon::MiddleClick:         break;     default:         ;     }}void MainWindow::minitotraySlot(){    if(isHidden())    {           show();           trayIcon->hide();    }    else    {           hide();           trayIcon->show();    }}void MainWindow::createTrayIcon(){    trayIconMenu = new QMenu(this);    trayIconMenu->addAction(minimizeAct);    trayIconMenu->addAction(closeAct);    trayIconMenu->addSeparator();    trayIconMenu->addAction(aboutAct);           trayIcon->setContextMenu(trayIconMenu);}void MainWindow::dbclickopenfileSlot(const QModelIndex&){    QString  file;    QString  pathfile;    QModelIndex fileIndex, parentIndex;    fileIndex = tree->currentIndex();    file = tree->currentIndex().data(Qt::DisplayRole).toString();    pathfile = file;    parentIndex = tree->currentIndex().parent();    while (parentIndex != tree->rootIndex())    {          if (parentIndex.data(Qt::DisplayRole).toString() != "/")             pathfile.prepend("/");          pathfile.prepend(parentIndex.data(Qt::DisplayRole).toString());          parentIndex = parentIndex.parent();    }    if (!dirmodel->isDir(fileIndex) && file.right(4)==".txt")

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -