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

📄 3.9 实例:一个简单的文本浏览的例子.txt

📁 qt的源代码
💻 TXT
字号:
#ifndef MAINWINDOW_H
 #define MAINWINDOW_H
 #include <QAssistantClient>
#include <QMainWindow>
 #include <QTextEdit>
class MainWindow : public QMainWindow
{
     Q_OBJECT
 public:
     MainWindow();
 private slots:
     void about();    
     void assistant();
 void open();
 protected:
void closeEvent(QCloseEvent *event);
 private:
     void createActions();
     void createMenus();
void initializeAssistant();
 QAssistantClient *assistantClient;
     QTextEdit *textViewer;
     QMenu *fileMenu;
     QMenu *helpMenu;
     QAction *assistantAct;
     QAction *clearAct;
     QAction *openAct;
     QAction *exitAct;
     QAction *aboutAct;
     QAction *aboutQtAct;
 };
 #endif
#ifndef FINDFILEDIALOG_H
 #define FINDFILEDIALOG_H
 #include <QAssistantClient>
 #include <QDialog>
 class QComboBox;
 class QDialogButtonBox;
 class QLabel;
 class QTextEdit;
 class QToolButton;
 class QTreeWidget;
 class QTreeWidgetItem;
 class FindFileDialog : public QDialog
 {
     Q_OBJECT
 public:
     FindFileDialog(QTextEdit *editor, QAssistantClient *assistant,
                    QWidget *parent = 0);
 private slots:
     void browse();
     void help();
     void openFile(QTreeWidgetItem *item = 0);
     void update();
 private:
     void findFiles();
     void showFiles(const QStringList &files);
     void createButtons();
     void createComboBoxes();
     void createFilesTree();
     void createLabels();
     void createLayout();
QAssistantClient *currentAssistantClient;
     QTextEdit *currentEditor;
     QTreeWidget *foundFilesTree;
     QComboBox *directoryComboBox;
     QComboBox *fileNameComboBox;
     QLabel *directoryLabel;
     QLabel *fileNameLabel;
     QDialogButtonBox *buttonBox;
     QToolButton *browseButton;
 };
 #endif
 #include <QtGui>
 #include "mainwindow.h"
 #include "findfiledialog.h"
 MainWindow::MainWindow()
 {
     textViewer = new QTextEdit;
     textViewer->setReadOnly(true);
     QFile file("documentation/intro.html");
     if (file.open(QIODevice::ReadOnly))
         textViewer->setHtml(file.readAll());
     setCentralWidget(textViewer);
     createActions();
     createMenus();
     initializeAssistant()
;
setWindowTitle(tr("Simple Text Viewer"));
     resize(750, 400);
 }
 void MainWindow::closeEvent(QCloseEvent *)
 {
     if (assistantClient)
         assistantClient->closeAssistant();
 }
 void MainWindow::about()
 {
     QMessageBox::about(this, tr("About Simple Text Viewer"),
                          tr("This example demonstrates how to use\n" \
                             "Qt Assistant as help system for your\n" \
                             "own application."));
 }
void MainWindow::assistant()
 {
     assistantClient->showPage(QLibraryInfo::location(QLibraryInfo::ExamplesPath) +
                                               QDir::separator() +                                            "assistant/simpletextviewer/documentation/index.html");
 }
 void MainWindow::open()
 {
     FindFileDialog dialog(textViewer, assistantClient);
     dialog.exec();
}
void MainWindow::createActions()
 {
     assistantAct = new QAction(tr("Help Contents"), this);
assistantAct->setShortcut(tr("F1"));
     connect(assistantAct, SIGNAL(triggered()), this, SLOT(assistant()));
     openAct = new QAction(tr("&Open..."), this);
     openAct->setShortcut(tr("Ctrl+O"));
     connect(openAct, SIGNAL(triggered()), this, SLOT(open()));
     clearAct = new QAction(tr("&Clear"), this);
     clearAct->setShortcut(tr("Ctrl+C"));
     connect(clearAct, SIGNAL(triggered()), textViewer, SLOT(clear()));
     exitAct = new QAction(tr("E&xit"), this);
     exitAct->setShortcut(tr("Ctrl+Q"));
     connect(exitAct, SIGNAL(triggered()), this, SLOT(close()));
     aboutAct = new QAction(tr("&About"), this);
     connect(aboutAct, SIGNAL(triggered()), this, SLOT(about()));
     aboutQtAct = new QAction(tr("About &Qt"), this);
     connect(aboutQtAct, SIGNAL(triggered()), QApp, SLOT(aboutQt()));
 }
 void MainWindow::createMenus()
 {
     fileMenu = new QMenu(tr("&File"), this);
     fileMenu->addAction(openAct);
     fileMenu->addAction(clearAct);
     fileMenu->addSeparator();
     fileMenu->addAction(exitAct);
     helpMenu = new QMenu(tr("&Help"), this);
     helpMenu->addAction(assistantAct);
     helpMenu->addSeparator();
     helpMenu->addAction(aboutAct);
     helpMenu->addAction(aboutQtAct);
     menuBar()->addMenu(fileMenu);
     menuBar()->addMenu(helpMenu);
 }
void MainWindow::initializeAssistant()
 {
     assistantClient=new QAssistantClient(QLibraryInfo::location(QLibraryInfo::BinariesPath), this);
     QStringList arguments;
     arguments << "-profile" << QString("documentation") + QDir::separator() + QString("simpletextviewer.adp");
assistantClient->setArguments(arguments);
 }
 #include <QtGui>
 #include "findfiledialog.h"
FindFileDialog::FindFileDialog(QTextEdit *editor, QAssistantClient *assistant,
                                QWidget *parent)
     : QDialog(parent)
 {
currentAssistantClient = assistant;
     currentEditor = editor;
     createButtons();
     createComboBoxes();
     createFilesTree();
     createLabels();
     createLayout();
     directoryComboBox->addItem(QDir::toNativeSeparators(QDir::currentPath()));
     fileNameComboBox->addItem("*");
     findFiles();
     setWindowTitle(tr("Find File"));
 }
 void FindFileDialog::browse()
 {
     QString currentDirectory = directoryComboBox->currentText();
     QString newDirectory = QFileDialog::getExistingDirectory(this,
                                tr("Select Directory"), currentDirectory);
     if (!newDirectory.isEmpty()) {
         directoryComboBox->addItem(QDir::toNativeSeparators(newDirectory));
         directoryComboBox->setCurrentIndex(directoryComboBox->count() - 1);
         update();
     }
 }
void FindFileDialog::help()
 {
  currentAssistantClient->showPage(QLibraryInfo::location(QLibraryInfo::ExamplesPath) +
       QDir::separator() +  "assistant/simpletextviewer/documentation/filedialog.html");
QAssistantClient::showPage()
*/
 }
 void FindFileDialog::openFile(QTreeWidgetItem *item)
 {
     if (!item) {
         item = foundFilesTree->currentItem();
         if (!item)
             return;
     }
     QString fileName = item->text(0);
     QString path = directoryComboBox->currentText() + QDir::separator();
     QFile file(path + fileName);
     if (file.open(QIODevice::ReadOnly)) {
         QString data(file.readAll());
         if (fileName.endsWith(".html"))
             currentEditor->setHtml(data);
         else
             currentEditor->setPlainText(data);
     }
     close();
 }
 void FindFileDialog::update()
 {
     findFiles();
     buttonBox->button(QDialogButtonBox::Open)->setEnabled(
             foundFilesTree->topLevelItemCount() > 0);
 }
 void FindFileDialog::findFiles()
 {
     QRegExp filePattern(fileNameComboBox->currentText() + "*");
     filePattern.setPatternSyntax(QRegExp::Wildcard);
     QDir directory(directoryComboBox->currentText());
     QStringList allFiles = directory.entryList(QDir::Files | QDir::NoSymLinks);
     QStringList matchingFiles;
     foreach (QString file, allFiles) {
         if (filePattern.exactMatch(file))
             matchingFiles << file;
     }
     showFiles(matchingFiles);
 }
 void FindFileDialog::showFiles(const QStringList &files)
 {
foundFilesTree->clear();
     for (int i = 0; i < files.count(); ++i) {
         QTreeWidgetItem *item = new QTreeWidgetItem(foundFilesTree);
         item->setText(0, files[i]);
}
     if (files.count() > 0)
         foundFilesTree->setCurrentItem(foundFilesTree->topLevelItem(0));
 }
 void FindFileDialog::createButtons()
 {
browseButton = new QToolButton;
     browseButton->setText(tr("..."));
     connect(browseButton, SIGNAL(clicked()), this, SLOT(browse()));
     buttonBox = new QDialogButtonBox(QDialogButtonBox::Open
                                      | QDialogButtonBox::Cancel
                                      | QDialogButtonBox::Help);
connect(buttonBox, SIGNAL(accepted()), this, SLOT(openFile()));
     connect(buttonBox, SIGNAL(rejected()), this, SLOT(reject()));
     connect(buttonBox, SIGNAL(helpRequested()), this, SLOT(help()));
 }
 void FindFileDialog::createComboBoxes()
 {
     directoryComboBox = new QComboBox;
     fileNameComboBox = new QComboBox;
     fileNameComboBox->setEditable(true);
     fileNameComboBox->setSizePolicy(QSizePolicy::Expanding,
                                     QSizePolicy::Preferred);

     directoryComboBox->setMinimumContentsLength(30);
     directoryComboBox->setSizeAdjustPolicy(
             QComboBox::AdjustToMinimumContentsLength);
     directoryComboBox->setSizePolicy(QSizePolicy::Expanding,
                                      QSizePolicy::Preferred);
     connect(fileNameComboBox, SIGNAL(editTextChanged(const QString &)),
             this, SLOT(update()));
connect(directoryComboBox, SIGNAL(currentIndexChanged(const QString &)),
             this, SLOT(update()));
 }
 void FindFileDialog::createFilesTree()
 {
     foundFilesTree = new QTreeWidget;
     foundFilesTree->setColumnCount(1);
     foundFilesTree->setHeaderLabels(QStringList(tr("Matching Files")));
     foundFilesTree->setRootIsDecorated(false);
     foundFilesTree->setSelectionMode(QAbstractItemView::SingleSelection);
     connect(foundFilesTree, SIGNAL(itemActivated(QTreeWidgetItem *, int)),
             this, SLOT(openFile(QTreeWidgetItem *)));
 }
 void FindFileDialog::createLabels()
 {
     directoryLabel = new QLabel(tr("Search in:"));
     fileNameLabel = new QLabel(tr("File name (including wildcards):"));
 }
 void FindFileDialog::createLayout()
 {
     QHBoxLayout *fileLayout = new QHBoxLayout;
 fileLayout->addWidget(fileNameLabel);
     fileLayout->addWidget(fileNameComboBox);
     QHBoxLayout *directoryLayout = new QHBoxLayout;
     directoryLayout->addWidget(directoryLabel);
     directoryLayout->addWidget(directoryComboBox);
     directoryLayout->addWidget(browseButton);
     QVBoxLayout *mainLayout = new QVBoxLayout;
     mainLayout->addLayout(fileLayout);
     mainLayout->addLayout(directoryLayout);
     mainLayout->addWidget(foundFilesTree);
     mainLayout->addStretch();
     mainLayout->addWidget(buttonBox);
     setLayout(mainLayout);
 }
 #include <QApplication>
 #include "mainwindow.h"
 int main(int argc, char *argv[])
 {
     QApplication app(argc, argv);
     MainWindow window;
     window.show();
     return app.exec();
}

⌨️ 快捷键说明

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