免费99精品国产自在现线观看_人妻少妇精品视频区性色_丝袜 屁股 在线 国产_无码视频在线免费观看

Qt開發(fā)-應(yīng)用程序的打包與發(fā)布(qt程序打包發(fā)布安裝)

一、調(diào)試與發(fā)布

1、發(fā)布應(yīng)用程序時(shí)的候選者

(1)、調(diào)試版(debug):開發(fā)階段生成可執(zhí)行程序

(2)、發(fā)布版(release):最終產(chǎn)品的可執(zhí)行程序

2、調(diào)試版的可執(zhí)行程序

(1)、包含調(diào)試相關(guān)的信息,體積巨大

(2)、執(zhí)行速度慢,支持?jǐn)帱c(diǎn)調(diào)試

3、發(fā)布版的可執(zhí)行程序

(1)、無任何冗余信息,體積小巧

(2)、執(zhí)行速度快,無法映射到源碼調(diào)試

二、程序的庫(kù)依賴

1、可執(zhí)行程序的正常運(yùn)行需要外部庫(kù)的支持

因此:發(fā)布程序時(shí)必須保證所有依賴庫(kù)都存在

Qt開發(fā)-應(yīng)用程序的打包與發(fā)布(qt程序打包發(fā)布安裝)

2、Windows可以用Depends工具查看程序的依賴庫(kù)

Qt開發(fā)-應(yīng)用程序的打包與發(fā)布(qt程序打包發(fā)布安裝)

3、Linux可以用ldd命令查看程序的依賴庫(kù)

(1)、ldd是Linux系統(tǒng)中的一個(gè)腳本程序

Qt開發(fā)-應(yīng)用程序的打包與發(fā)布(qt程序打包發(fā)布安裝)

三、程序的環(huán)境依賴

1、應(yīng)用程序?qū)τ趫?zhí)行程序環(huán)境可能還存在依賴關(guān)系

Qt開發(fā)-應(yīng)用程序的打包與發(fā)布(qt程序打包發(fā)布安裝)

2、在客戶機(jī)上部署程序的執(zhí)行環(huán)境

(1)、方案一

A、用戶根據(jù)提供的手冊(cè)自行部署環(huán)境(不可?。?/span>

(2)、方案二

A、開發(fā)一個(gè)專用的部署(安裝)程序
B、部署(安裝)程序運(yùn)行后自動(dòng)部署執(zhí)行環(huán)境

3、部署(安裝)程序的開發(fā)

Qt開發(fā)-應(yīng)用程序的打包與發(fā)布(qt程序打包發(fā)布安裝)

4、InstallShield的簡(jiǎn)介

Qt開發(fā)-應(yīng)用程序的打包與發(fā)布(qt程序打包發(fā)布安裝)

5、Inno Setup簡(jiǎn)介

Qt開發(fā)-應(yīng)用程序的打包與發(fā)布(qt程序打包發(fā)布安裝)

6、NSIS簡(jiǎn)介

Qt開發(fā)-應(yīng)用程序的打包與發(fā)布(qt程序打包發(fā)布安裝)

7、LinuxQt程序發(fā)布簡(jiǎn)介

(1)、方法一:

A、通過ldd命令確定程序的庫(kù)依賴

B、通過shell腳本開發(fā)部署程序

(2)、方法二

根據(jù)具體發(fā)行版開發(fā)專用部署程序(dep、rpm

四、文本編輯器項(xiàng)目完整程序

關(guān)于對(duì)話框AboutDialog .h

#ifndef ABOUTDIALOG_H#define ABOUTDIALOG_H#include <QWidget>#include <QLabel>#include <QPlainTextEdit>#include <QPushButton>#include <QDialog>class AboutDialog : public QDialog{ Q_OBJECTprotected: QLabel m_label; QPushButton m_closeButton; QPlainTextEdit m_plainTextEdit;public: explicit AboutDialog(QWidget *parent = 0); };#endif // ABOUTDIALOG_H

關(guān)于對(duì)話框AboutDialog .cpp

#include "AboutDialog.h"#include <QPalette>AboutDialog::AboutDialog(QWidget *parent) : QDialog(parent), m_label(this), m_closeButton(this), m_plainTextEdit(this){ QPixmap pm(":/Res/pic/Logo.PNG"); pm = pm.scaled(120, 120, Qt::KeepAspectRatio); m_label.setPixmap(pm); m_label.move(20, 20); m_label.resize(120, 120); QPalette p = m_plainTextEdit.palette(); p.setColor(QPalette::Active, QPalette::Base, palette().color(QPalette::Active, QPalette::Background)); p.setColor(QPalette::Inactive, QPalette::Base, palette().color(QPalette::Inactive, QPalette::Background)); m_plainTextEdit.move(200, 30); m_plainTextEdit.resize(180, 130); m_plainTextEdit.setPalette(p); m_plainTextEdit.setFrameStyle(QFrame::NoFrame); m_plainTextEdit.setReadOnly(true); m_plainTextEdit.insertPlainText("NotePad ProjectnnPlatform: Qt 4.7.4nnVersion: 1.0.0nnCopyright: LGC"); m_closeButton.setText("Close"); m_closeButton.move(273, 175); m_closeButton.resize(100, 30); setFixedSize(390, 230); setWindowTitle("About NotePad"); connect(&m_closeButton, SIGNAL(clicked()), this, SLOT(close()));}

配置文件AppConfig.h

#ifndef APPCONFIG_H#define APPCONFIG_H#include <QObject>#include <QFont>#include <QSize>#include <QPoint>class AppConfig : public QObject{ Q_OBJECT QFont m_editFont; bool m_isToolBarVisible; bool m_isStatusVisible; bool m_isAutoWrap; QSize m_mainWindowSize; QPoint m_mainWindowPosition; bool m_isValid; bool restore();public: explicit AppConfig(QObject *parent = 0); explicit AppConfig(QFont editFont, bool isToolBarVisible, bool isStatusVisible,bool isAutoWrap, QSize mainWindowSize, QPoint mainWindowPosition, QObject *parent = 0); QFont editFont(); bool isToolBarVisible(); bool isStatusBarVisible(); bool isAutoWrap(); bool isValid(); QSize mainWindowSize(); QPoint mainWindowPosition(); bool store();};#endif // APPCONFIG_H

配置文件AppConfig.cpp

#include "AppConfig.h"#include <QFile>#include <QDataStream>#include <QApplication>#include <QIODevice>//此構(gòu)造函數(shù)用來讀取AppConfig::AppConfig(QObject *parent) : QObject(parent){ m_isValid = restore();}//此構(gòu)造函數(shù)用來寫入AppConfig::AppConfig(QFont editFont, bool isToolBarVisible, bool isStatusVisible,bool isAutoWrap, QSize mainWindowSize, QPoint mainWindowPosition, QObject *parent){ m_editFont = editFont; m_isToolBarVisible = isToolBarVisible; m_isStatusVisible = isStatusVisible; m_isAutoWrap = isAutoWrap; m_mainWindowSize = mainWindowSize; m_mainWindowPosition = mainWindowPosition; m_isValid = true;}QFont AppConfig::editFont(){ return m_editFont;}bool AppConfig::isToolBarVisible(){ return m_isToolBarVisible;}bool AppConfig::isStatusBarVisible(){ return m_isStatusVisible;}bool AppConfig::isAutoWrap(){ return m_isAutoWrap;}bool AppConfig::isValid(){ return m_isValid;}bool AppConfig::restore(){ bool ret = true; QFile file(QApplication::applicationDirPath() "/app.config"); if(file.open(QIODevice::ReadOnly)) { QDataStream in(&file); in.setVersion(QDataStream::Qt_4_7); in >> m_editFont; in >> m_isToolBarVisible; in >> m_isStatusVisible; in >> m_isAutoWrap; in >> m_mainWindowSize; in >> m_mainWindowPosition; file.close(); } else { ret = false; } return ret;}bool AppConfig::store(){ bool ret = true; QFile file(QApplication::applicationDirPath() "/app.config"); if(file.open(QIODevice::WriteOnly)) { QDataStream out(&file); out.setVersion(QDataStream::Qt_4_7); out << m_editFont; out << m_isToolBarVisible; out << m_isStatusVisible; out << m_isAutoWrap; out << m_mainWindowSize; out << m_mainWindowPosition; file.close(); } else { ret = false; } return ret;}QSize AppConfig::mainWindowSize(){ return m_mainWindowSize;}QPoint AppConfig::mainWindowPosition(){ return m_mainWindowPosition;}

查找對(duì)話框FindDialog .h

#ifndef FINDDIALOG_H#define FINDDIALOG_H#include <QDialog>#include <QPushButton>#include <QLabel>#include <QLineEdit>#include <QRadioButton>#include <QCheckBox>#include <QHBoxLayout>#include <QGroupBox>#include <QGridLayout>#include <QPlainTextEdit>#include <QPointer>class FindDialog : public QDialog{ Q_OBJECTprotected: QGridLayout m_gLayout;//注意要把聲明放在組件之前,否則程序崩潰 QGroupBox m_gBox; //因?yàn)橥粋€(gè)布局管理器里面的父組件相同,組合的對(duì)象根據(jù)聲明的順序來構(gòu)造,所有放在這里 QHBoxLayout m_hLayout; QLabel m_findLabel; QLineEdit m_findLineEdit; QPushButton m_findButton; QCheckBox m_checkBox; QRadioButton m_forwardButton; QRadioButton m_backwardButton; QPushButton m_cancleButton; QPointer<QPlainTextEdit> m_plainTextEdit; void initControl();protected slots: void onFindButtonClicked(); void onCancleButtonClicked();public: explicit FindDialog(QWidget *parent = 0, QPlainTextEdit* pText = NULL); void setPlainTextEdit(QPlainTextEdit* pText); QPlainTextEdit* getPlainTextEdit(); bool event(QEvent* e);};#endif // FINDDIALOG_H

查找對(duì)話框FindDialog.cpp

#include "FindDialog.h"#include <QEvent>#include <QString>#include <QTextCursor>#include <QMessageBox>FindDialog::FindDialog(QWidget *parent, QPlainTextEdit* pText) : QDialog(parent, Qt::WindowCloseButtonHint | Qt::Drawer){ initControl(); connect(&m_findButton, SIGNAL(clicked()), this, SLOT(onFindButtonClicked())); connect(&m_cancleButton, SIGNAL(clicked()), this, SLOT(onCancleButtonClicked())); setPlainTextEdit(pText); setWindowTitle("查找"); setLayout(&m_gLayout);}void FindDialog::initControl(){ m_findLabel.setText("查找內(nèi)容: "); // m_findButton.setEnabled(false); m_findButton.setText("查找下一個(gè)"); m_checkBox.setText("區(qū)分大小寫"); m_forwardButton.setText("向上"); m_backwardButton.setChecked(true); m_backwardButton.setText("向下"); m_cancleButton.setText("取消"); m_gBox.setTitle("方向"); m_hLayout.addWidget(&m_forwardButton); m_hLayout.addWidget(&m_backwardButton); m_gBox.setLayout(&m_hLayout); m_gLayout.setSpacing(10); m_gLayout.addWidget(&m_findLabel, 0, 0); m_gLayout.addWidget(&m_findLineEdit, 0, 1); m_gLayout.addWidget(&m_findButton, 0, 2); m_gLayout.addWidget(&m_checkBox, 1, 0); m_gLayout.addWidget(&m_gBox, 1, 1); m_gLayout.addWidget(&m_cancleButton, 1, 2);}void FindDialog::setPlainTextEdit(QPlainTextEdit* pText){ m_plainTextEdit = pText;}QPlainTextEdit* FindDialog::getPlainTextEdit(){ return m_plainTextEdit;}void FindDialog::onFindButtonClicked(){ QString target = m_findLineEdit.text(); if((m_plainTextEdit != NULL) && (target != "")) { QString text = m_plainTextEdit->toPlainText(); QTextCursor c = m_plainTextEdit->textCursor(); int index = -1; if(m_backwardButton.isChecked()) { index = text.indexOf(target, c.position(), m_checkBox.isChecked()? Qt::CaseSensitive : Qt::CaseInsensitive); if(index >=0 ) { c.setPosition(index); c.setPosition(index target.length(), QTextCursor::KeepAnchor); m_plainTextEdit->setTextCursor(c);//將光標(biāo)信息設(shè)置到文本框里面才能看見效果 } } if(m_forwardButton.isChecked()) { index = text.lastIndexOf(target, c.position() - text.length() - 1, m_checkBox.isChecked()? Qt::CaseSensitive : Qt::CaseInsensitive);//注意下標(biāo)為負(fù)數(shù) if(index >=0 ) { c.setPosition(index target.length()); c.setPosition(index, QTextCursor::KeepAnchor);//注意后往前找到后設(shè)置光標(biāo)的方法 m_plainTextEdit->setTextCursor(c); } } if(index < 0) { QMessageBox dlg(this); dlg.setText("找不到" target); dlg.setStandardButtons(QMessageBox::Ok); dlg.setIcon(QMessageBox::Information); dlg.setWindowTitle("查找"); dlg.exec(); } }}void FindDialog::onCancleButtonClicked(){ close();}bool FindDialog::event(QEvent* e){ if(e->type()==QEvent::Close) { hide(); return true; } return QDialog::event(e);}

替換對(duì)話框ReplaceDialog .h

#ifndef REPLACEDIALOG_H#define REPLACEDIALOG_H#include "FindDialog.h"class ReplaceDialog : public FindDialog{ Q_OBJECT QPushButton m_replaceButton; QPushButton m_replaceAllButton; QLabel m_replaceAsLabel; QLineEdit m_replaceEdit; void initControl();protected slots: void onReplaceButtonClicked(); void onReplaceAllButtonClicked();public: explicit ReplaceDialog(QWidget *parent = 0, QPlainTextEdit* pText = NULL); };#endif // REPLACEDIALOG_H

替換對(duì)話框ReplaceDialog.cpp

#include "ReplaceDialog.h"ReplaceDialog::ReplaceDialog(QWidget *parent, QPlainTextEdit* pText) : FindDialog(parent, pText){ initControl(); connect(&m_replaceButton, SIGNAL(clicked()), this, SLOT(onReplaceButtonClicked())); connect(&m_replaceAllButton, SIGNAL(clicked()), this, SLOT(onReplaceAllButtonClicked())); setWindowTitle("替換"); setLayout(&m_gLayout);}void ReplaceDialog::initControl(){ m_replaceButton.setText("替換"); m_replaceAllButton.setText("全部替換"); m_replaceAsLabel.setText("替換為: "); m_gLayout.removeWidget(&m_checkBox); m_gLayout.removeWidget(&m_gBox); m_gLayout.removeWidget(&m_cancleButton); m_gLayout.addWidget(&m_replaceAsLabel, 1, 0); m_gLayout.addWidget(&m_replaceEdit, 1, 1); m_gLayout.addWidget(&m_replaceButton, 1, 2); m_gLayout.addWidget(&m_replaceAllButton, 2, 2); m_gLayout.addWidget(&m_checkBox, 3, 0); m_gLayout.addWidget(&m_gBox, 3, 1); m_gLayout.addWidget(&m_cancleButton, 3, 2);}void ReplaceDialog::onReplaceButtonClicked(){ QString target = m_findLineEdit.text(); QString to = m_replaceEdit.text(); if(m_plainTextEdit != NULL && target !="" && to != "") { QString text = m_plainTextEdit->toPlainText(); QString selText = m_plainTextEdit->textCursor().selectedText(); if(selText == target) { m_plainTextEdit->insertPlainText(to); } onFindButtonClicked(); }}void ReplaceDialog::onReplaceAllButtonClicked(){ QString target = m_findLineEdit.text(); QString to = m_replaceEdit.text(); if(m_plainTextEdit != NULL && target !="" && to != "") { QString text = m_plainTextEdit->toPlainText(); text.replace(target, to, m_checkBox.isChecked()? Qt::CaseSensitive : Qt::CaseInsensitive); m_plainTextEdit->clear(); m_plainTextEdit->setPlainText(text); }}

主窗口頭文件MainWindow .h

#ifndef MAINWINDOW_H#define MAINWINDOW_H#include <QMenuBar>#include <QMenu>#include <QAction>#include <QString>#include <QtGui/QMainWindow>#include <QToolBar>#include <QIcon>#include <QSize>#include <QStatusBar>#include <QLabel>#include <QPlainTextEdit>#include <QFileDialog>#include <QAction>#include <QSharedPointer>#include "FindDialog.h"#include "ReplaceDialog.h"#include "AboutDialog.h"#include "AppConfig.h"class MainWindow : public QMainWindow{ Q_OBJECTprivate: QPlainTextEdit mainEdit; QLabel statusLabel; QString m_filePath;//記得在構(gòu)造函數(shù)里初始化 bool m_isTextChanged;//構(gòu)造函數(shù)里初始化為false QSharedPointer<FindDialog> m_findDialogPoiter; QSharedPointer<ReplaceDialog> m_ReplaceDialogPoiter; MainWindow(QWidget *parent = 0); MainWindow(const MainWindow& obj); MainWindow* operator = (const MainWindow& obj); bool construct(); bool initMenuBar();//菜單欄 bool initToolBar();//工具欄 bool initStatusBar();//狀態(tài)欄 bool initinitMainEditor();//編輯窗口 bool initFileMenu(QMenuBar* mb);//文件菜單 bool initEditMenu(QMenuBar* mb);//編輯菜單 bool initFormatMenu(QMenuBar* mb);//格式菜單 bool initViewMenu(QMenuBar* mb);//視圖菜單 bool initHelpMenu(QMenuBar* mb);//幫助菜單 bool initFileToolItem(QToolBar* tb);//工具選項(xiàng) bool initEditToolItem(QToolBar* tb); bool initFormatToolItem(QToolBar* tb); bool initViewToolItem(QToolBar* tb); bool makeAction(QAction*& action,QMenu* menu, QString text, int key);//菜單項(xiàng) bool makeAction(QAction*& action,QToolBar* tb, QString tip, QString icon); QString showFileDialog(QFileDialog::AcceptMode mode, QString title);//文件對(duì)話框 void showErrorMessage(QString message);//錯(cuò)誤消息對(duì)話框 int showQuesstionMessage(QString message);//問題消息對(duì)話框 QString saveCurrentData(QString path = ""); void preEditorChanged(); void openFileToEdit(QString path); QAction* findToolBarAction(QString text); QAction* findMenuBarAction(QString text);private slots: void onFileNew(); void onFileOpen(); void onFlieSave(); void onFileSaveAs(); void onFilePrint(); void onTextChanged(); void onCursorPositionChanged(); void onFileDelete(); void onFileExit(); void onEditFind(); void onEditReplce(); void onEditGoto(); void onViewStatusBar(); void onViewToolBar(); void onHelpAbout(); void onFontDialog(); void onHelpFindHelp(); void onFormatWrap(); void onCopyAvailable(bool available); void onUndoAvailable(bool available); void onRedoAvailable(bool available);protected: void closeEvent(QCloseEvent *e);//重寫關(guān)閉窗口的事件處理函數(shù) void dragEnterEvent(QDragEnterEvent* e); void dropEvent(QDropEvent* e);public: static MainWindow* NewInstance(); QToolBar* toolBar(); void openFile(QString path); ~MainWindow();};#endif // MAINWINDOW_H

主窗口界面實(shí)現(xiàn)文件MainWindowUI.cpp

#include "MainWindow.h"#include <QDebug>#include <QPalette>MainWindow::MainWindow(QWidget *parent) : m_findDialogPoiter(new FindDialog(this, &mainEdit)), m_ReplaceDialogPoiter(new ReplaceDialog(this, &mainEdit)){ m_filePath = ""; m_isTextChanged = false; setWindowTitle("NotePad-[New]"); resize(800, 500); setAcceptDrops(true);}bool MainWindow::construct(){ bool ret = true; AppConfig config; ret = ret && initMenuBar(); ret = ret && initToolBar(); ret = ret && initStatusBar(); ret = ret && initinitMainEditor(); if(config.isValid()) { mainEdit.setFont(config.editFont()); //不自動(dòng)換行 if(!config.isAutoWrap()) { mainEdit.setLineWrapMode(QPlainTextEdit::NoWrap); findMenuBarAction("換行")->setChecked(false); findToolBarAction("換行")->setChecked(false); } //隱藏工具欄 if(!config.isToolBarVisible()) { toolBar()->setVisible(false); findMenuBarAction("工具欄")->setChecked(false); findToolBarAction("工具欄")->setChecked(false); } //隱藏狀態(tài)欄 if(!config.isStatusBarVisible()) { statusBar()->setVisible(false); findMenuBarAction("狀態(tài)欄")->setChecked(false); findToolBarAction("狀態(tài)欄")->setChecked(false); } move(config.mainWindowPosition()); resize(config.mainWindowSize()); } return ret;}MainWindow* MainWindow::NewInstance(){ MainWindow* ret = new MainWindow(); if((ret==NULL) || (!ret->construct())) { delete ret; ret = NULL; } return ret;}bool MainWindow::initMenuBar()//菜單欄{ bool ret = true; QMenuBar* mb = menuBar ();//一定要注意是menuBar(),這是普通成員函數(shù),不是構(gòu)造函數(shù) ret = ret && initFileMenu(mb);//傳一個(gè)參數(shù)是為了在initFileMenu()函數(shù)將menu加入菜單欄 ret = ret && initEditMenu(mb); ret = ret && initFormatMenu(mb); ret = ret && initViewMenu(mb); ret = ret && initHelpMenu(mb); return ret;}bool MainWindow::initToolBar()//工具欄{ bool ret = true; QToolBar* tb = addToolBar("工具欄"); //tb->setMovable(false); //tb->setFloatable(false); tb->setIconSize(QSize(16,16)); ret = ret && initFileToolItem(tb); tb->addSeparator(); ret = ret && initEditToolItem(tb); tb->addSeparator(); ret = ret && initFormatToolItem(tb); tb->addSeparator(); ret = ret && initViewToolItem(tb); return ret;}bool MainWindow::initStatusBar()//狀態(tài)欄{ bool ret = true; QStatusBar* sb = statusBar(); QLabel* label = new QLabel("Made By LGC"); if(label != NULL) { statusLabel.setMinimumWidth(200); statusLabel.setAlignment(Qt::AlignHCenter); statusLabel.setText("行:1 列:1"); label->setMinimumWidth(200); label->setAlignment(Qt::AlignHCenter); sb->addPermanentWidget(new QLabel());//單純加入分隔符 sb->addPermanentWidget(&statusLabel); sb->addPermanentWidget(label); } else { ret = false; } return ret;}bool MainWindow::initinitMainEditor()//編輯窗口{ bool ret = true; mainEdit.setParent(this); setCentralWidget(&mainEdit); connect(&mainEdit, SIGNAL(textChanged()), this, SLOT(onTextChanged())); connect(&mainEdit, SIGNAL(copyAvailable(bool)), this, SLOT(onCopyAvailable(bool))); connect(&mainEdit, SIGNAL(undoAvailable(bool)), this, SLOT(onUndoAvailable(bool))); connect(&mainEdit, SIGNAL(redoAvailable(bool)), this, SLOT(onRedoAvailable(bool))); connect(&mainEdit, SIGNAL(cursorPositionChanged()), this, SLOT(onCursorPositionChanged())); QPalette pmainEdit = mainEdit.palette(); pmainEdit.setColor(QPalette::Inactive, QPalette::Highlight, pmainEdit.color(QPalette::Active, QPalette::Highlight)); pmainEdit.setColor(QPalette::Inactive, QPalette::HighlightedText, pmainEdit.color(QPalette::Active, QPalette::HighlightedText)); mainEdit.setPalette(pmainEdit); return ret;}/************************************************文件菜單********************************************************/bool MainWindow::initFileMenu(QMenuBar* mb){ bool ret = true; QMenu* menu = new QMenu("文件(&F)", mb);//創(chuàng)建文件菜單,(&F)是為了可以Alt F打開 ret = (menu != NULL); if(ret) { QAction* action = NULL; //New ret = ret && makeAction(action, menu, "新建(&N)",Qt::CTRL Qt::Key_N); if(ret) { connect(action, SIGNAL(triggered()), this, SLOT(onFileNew())); menu->addAction(action); } menu->addSeparator(); //Open ret = ret && makeAction(action, menu,"打開(&O)...",Qt::CTRL Qt::Key_O); if(ret) { connect(action, SIGNAL(triggered()), this, SLOT(onFileOpen())); menu->addAction(action); } menu->addSeparator(); //Save ret = ret && makeAction(action, menu,"保存(&S)",Qt::CTRL Qt::Key_S); if(ret) { connect(action, SIGNAL(triggered()), this ,SLOT(onFlieSave())); menu->addAction(action); } menu->addSeparator(); //Save As ret = ret && makeAction(action, menu, "另存為(&A)...",0); if(ret) { connect(action, SIGNAL(triggered()), this, SLOT(onFileSaveAs())); menu->addAction(action); } menu->addSeparator(); //print ret = ret && makeAction(action, menu, "打印(&P)...",Qt::CTRL Qt::Key_P); if(ret) { connect(action, SIGNAL(triggered()), this, SLOT(onFilePrint())); menu->addAction(action); } menu->addSeparator(); //Exit ret = ret && makeAction(action, menu,"退出(&X)",0); if(ret) { connect(action, SIGNAL(triggered()), this, SLOT(onFileExit())); menu->addAction(action);//將菜單項(xiàng)加入到菜單 } } if(ret) { mb->addMenu(menu);//將菜單加入到菜單欄 } else { delete mb; } return ret;}/************************************************編輯菜單********************************************************/bool MainWindow::initEditMenu(QMenuBar* mb){ bool ret = true; QMenu* menu = new QMenu("編輯(&E)", mb); ret = (menu != NULL); if(ret) { QAction* action = NULL; //Undo ret = ret && makeAction(action, menu,"撤銷(&U)",Qt::CTRL Qt::Key_Z); if(ret) { connect(action, SIGNAL(triggered()), &mainEdit, SLOT(undo())); action->setEnabled(false); menu->addAction(action); } menu->addSeparator(); //Redo ret = ret && makeAction(action, menu,"重做(&R)...",Qt::CTRL Qt::Key_Y); if(ret) { connect(action, SIGNAL(triggered()), &mainEdit, SLOT(redo())); action->setEnabled(false); menu->addAction(action); } menu->addSeparator(); //Cut ret = ret && makeAction(action, menu,"剪切(&X)",Qt::CTRL Qt::Key_X); if(ret) { connect(action, SIGNAL(triggered()), &mainEdit, SLOT(cut())); action->setEnabled(false); menu->addAction(action); } menu->addSeparator(); //Copy ret = ret && makeAction(action, menu,"復(fù)制(&C)...",Qt::CTRL Qt::Key_C); if(ret) { connect(action, SIGNAL(triggered()), &mainEdit, SLOT(copy())); action->setEnabled(false); menu->addAction(action); } menu->addSeparator(); //Paste ret = ret && makeAction(action, menu,"粘貼(&P)...",Qt::CTRL Qt::Key_V); if(ret) { connect(action, SIGNAL(triggered()), &mainEdit, SLOT(paste())); menu->addAction(action); } menu->addSeparator(); //Delete ret = ret && makeAction(action, menu, "刪除(&L)",Qt::Key_Delete); if(ret) { connect(action, SIGNAL(triggered()), this, SLOT(onFileDelete())); menu->addAction(action); } menu->addSeparator(); //Find ret = ret && makeAction(action, menu,"查找(&F)...",Qt::CTRL Qt::Key_F); if(ret) { connect(action, SIGNAL(triggered()), this, SLOT(onEditFind())); menu->addAction(action); } menu->addSeparator(); //Replace ret = ret && makeAction(action, menu,"替換(&R)...",Qt::CTRL Qt::Key_H); if(ret) { connect(action, SIGNAL(triggered()), this, SLOT(onEditReplce())); menu->addAction(action); } menu->addSeparator(); //Goto ret = ret && makeAction(action, menu,"轉(zhuǎn)到(&G)",Qt::CTRL Qt::Key_G); if(ret) { connect(action, SIGNAL(triggered()), this, SLOT(onEditGoto())); menu->addAction(action); } menu->addSeparator(); //Select All ret = ret && makeAction(action, menu, "全選(&A)",Qt::CTRL Qt::Key_A); if(ret) { connect(action, SIGNAL(triggered()), &mainEdit, SLOT(selectAll())); menu->addAction(action); } } if(ret) { mb->addMenu(menu); } else { delete mb; } return ret;}/************************************************格式菜單********************************************************/bool MainWindow::initFormatMenu(QMenuBar* mb){ bool ret = true; QMenu* menu = new QMenu("格式(&O)", mb); ret = (menu != NULL); if(ret) { QAction* action = NULL; //Auto Wrap ret = ret && makeAction(action, menu,"換行(&W)",0); if(ret) { action->setCheckable(true); action->setChecked(true); connect(action, SIGNAL(triggered()), this, SLOT(onFormatWrap())); menu->addAction(action); } menu->addSeparator(); //Font ret = ret && makeAction(action, menu,"字體(&F)...",0); if(ret) { connect(action, SIGNAL(triggered()), this, SLOT(onFontDialog())); menu->addAction(action); } } if(ret) { mb->addMenu(menu); } else { delete mb; } return ret;}/************************************************視圖菜單********************************************************/bool MainWindow::initViewMenu(QMenuBar* mb){ bool ret = true; QMenu* menu = new QMenu("視圖(&V)", mb); ret = (menu != NULL); if(ret) { QAction* action = NULL; //Tool Bar ret = ret && makeAction(action, menu,"工具欄(&T)",0); if(ret) { action->setCheckable(true); action->setChecked(true); connect(action, SIGNAL(triggered()), this, SLOT(onViewToolBar())); menu->addAction(action); } menu->addSeparator(); //Status Bar ret = ret && makeAction(action, menu,"狀態(tài)欄(&S)",0); if(ret) { action->setCheckable(true); action->setChecked(true); connect(action, SIGNAL(triggered()), this, SLOT(onViewStatusBar())); menu->addAction(action); } } if(ret) { mb->addMenu(menu); } else { delete mb; } return ret;}/************************************************幫助菜單********************************************************/bool MainWindow::initHelpMenu(QMenuBar* mb){ bool ret = true; QMenu* menu = new QMenu("幫助(&H)", mb); ret = (menu != NULL); if(ret) { QAction* action = NULL; //User Manual ret = ret && makeAction(action, menu,"查看幫助",0); if(ret) { connect(action, SIGNAL(triggered()), this, SLOT(onHelpFindHelp())); menu->addAction(action); } menu->addSeparator(); //About NotePad ret = ret && makeAction(action, menu,"關(guān)于記事本...",0); if(ret) { connect(action, SIGNAL(triggered()), this, SLOT(onHelpAbout())); menu->addAction(action); } } if(ret) { mb->addMenu(menu); } else { delete mb; } return ret;}/*****************************************工具************************************************************/bool MainWindow::initFileToolItem(QToolBar* tb){ bool ret = true; QAction* action = NULL; ret = ret && makeAction(action, tb, "新建", ":/Res/pic/new.png"); if(ret) { connect(action, SIGNAL(triggered()), this, SLOT(onFileNew())); tb->addAction(action); } ret = ret && makeAction(action, tb,"打開", ":/Res/pic/open.png"); if(ret) { connect(action, SIGNAL(triggered()), this, SLOT(onFileOpen())); tb->addAction(action); } ret = ret && makeAction(action, tb,"保存", ":/Res/pic/save.png"); if(ret) { connect(action, SIGNAL(triggered()), this ,SLOT(onFlieSave())); tb->addAction(action); } ret = ret && makeAction(action, tb,"另存為", ":/Res/pic/saveas.png"); if(ret) { connect(action, SIGNAL(triggered()), this, SLOT(onFileSaveAs())); tb->addAction(action); } ret = ret && makeAction(action, tb,"打印", ":/Res/pic/print.png"); if(ret) { connect(action, SIGNAL(triggered()), this, SLOT(onFilePrint())); tb->addAction(action); } return ret;}bool MainWindow::initEditToolItem(QToolBar* tb){ bool ret = true; QAction* action = NULL; ret = ret && makeAction(action, tb,"撤銷", ":/Res/pic/undo.png"); if(ret) { action->setEnabled(false); connect(action, SIGNAL(triggered()), &mainEdit, SLOT(undo())); tb->addAction(action); } ret = ret && makeAction(action, tb,"重做", ":/Res/pic/redo.png"); if(ret) { action->setEnabled(false); connect(action, SIGNAL(triggered()), &mainEdit, SLOT(redo())); tb->addAction(action); } ret = ret && makeAction(action, tb, "剪切", ":/Res/pic/cut.png"); if(ret) { action->setEnabled(false); connect(action, SIGNAL(triggered()), &mainEdit, SLOT(cut())); tb->addAction(action); } ret = ret && makeAction(action, tb,"復(fù)制", ":/Res/pic/copy.png"); if(ret) { action->setEnabled(false); connect(action, SIGNAL(triggered()), &mainEdit, SLOT(copy())); tb->addAction(action); } ret = ret && makeAction(action, tb,"粘貼", ":/Res/pic/paste.png"); if(ret) { connect(action, SIGNAL(triggered()), &mainEdit, SLOT(paste())); tb->addAction(action); } ret = ret && makeAction(action, tb,"查找", ":/Res/pic/find.png"); if(ret) { connect(action, SIGNAL(triggered()), this, SLOT(onEditFind())); tb->addAction(action); } ret = ret && makeAction(action, tb,"替換", ":/Res/pic/replace.png"); if(ret) { connect(action, SIGNAL(triggered()), this, SLOT(onEditReplce())); tb->addAction(action); } ret = ret && makeAction(action, tb,"轉(zhuǎn)到", ":/Res/pic/goto.png"); if(ret) { connect(action, SIGNAL(triggered()), this, SLOT(onEditGoto())); tb->addAction(action); } return ret;}bool MainWindow::initFormatToolItem(QToolBar* tb){ bool ret = true; QAction* action = NULL; ret = ret && makeAction(action, tb, "換行", ":/Res/pic/wrap.png"); if(ret) { action->setCheckable(true); action->setChecked(true); connect(action, SIGNAL(triggered()), this, SLOT(onFormatWrap())); tb->addAction(action); } ret = ret && makeAction(action, tb,"字體", ":/Res/pic/font.png"); if(ret) { connect(action, SIGNAL(triggered()), this, SLOT(onFontDialog())); tb->addAction(action); } return ret;}bool MainWindow::initViewToolItem(QToolBar* tb){ bool ret = true; QAction* action = NULL; ret = ret && makeAction(action, tb,"工具欄", ":/Res/pic/tool.png"); if(ret) { action->setCheckable(true); action->setChecked(true); connect(action, SIGNAL(triggered()), this, SLOT(onViewToolBar())); tb->addAction(action); } ret = ret && makeAction(action, tb,"狀態(tài)欄", ":/Res/pic/status.png"); if(ret) { action->setCheckable(true); action->setChecked(true); connect(action, SIGNAL(triggered()), this, SLOT(onViewStatusBar())); tb->addAction(action); } return ret;}bool MainWindow::makeAction(QAction*& action,QMenu* menu, QString text, int key)//菜單項(xiàng){ bool ret = true; action = new QAction(text, menu); if(action != NULL) { action->setShortcut(QKeySequence(key));//創(chuàng)建快捷鍵 } else { ret = false; } return ret;}bool MainWindow::makeAction(QAction*& action,QToolBar* tb, QString tip, QString icon){ bool ret = true; action = new QAction("", tb); if(action != NULL) { action->setToolTip(tip); action->setIcon(QIcon(icon)); } else { ret = false; } return ret;}MainWindow::~MainWindow(){}

主窗口功能實(shí)現(xiàn)文件MainWindowSlots.cpp

#include <QFileDialog>#include <QStringList>#include <QFile>#include <QDebug>#include <QMessageBox>#include "MainWindow.h"#include <QMap>#include <QDragEnterEvent>#include <QDropEvent>#include <QMimeData>#include <QList>#include <QUrl>#include <QFileInfo>#include <QObjectList>#include <QToolBar>#include <QPrintDialog>#include <QPrinter>#include <QKeyEvent>#include <QApplication>#include <QInputDialog>#include <QStatusBar>#include <QList>#include "AboutDialog.h"#include <QFontDialog>#include <QDesktopServices>QString MainWindow::showFileDialog(QFileDialog::AcceptMode mode, QString title){ QString ret = ""; QFileDialog fd; QStringList filters; QMap<QString, QString> map; const char* fileArray[][2]= { {"Text(*.txt)", ".txt"}, {"All Files(*.*)", "*" }, {NULL, NULL} }; for(int i=0; fileArray[i][0] != NULL; i ) { filters.append(fileArray[i][0]); map.insert(fileArray[i][0], fileArray[i][1]); } fd.setWindowTitle(title); fd.setAcceptMode(mode); fd.setNameFilters(filters); if(mode==QFileDialog::AcceptOpen) { fd.setFileMode(QFileDialog::ExistingFile); } if(fd.exec()==QFileDialog::Accepted) { ret = fd.selectedFiles()[0]; QString posix = map[fd.selectedNameFilter()];//把下拉中選中的后綴對(duì)應(yīng)鍵值取出 if(posix != "*" && !ret.endsWith(posix)) { ret = posix; } } return ret;}void MainWindow::showErrorMessage(QString message){ QMessageBox mb(this); mb.setWindowTitle("Quession"); mb.setText(message); mb.setIcon(QMessageBox::Critical); mb.setStandardButtons(QMessageBox::Ok); mb.exec();}int MainWindow::showQuesstionMessage(QString message){ QMessageBox mb(this); mb.setWindowTitle("Error"); mb.setText(message); mb.setIcon(QMessageBox::Question); mb.setStandardButtons(QMessageBox::Yes | QMessageBox::No | QMessageBox::Cancel); return mb.exec();}QString MainWindow::saveCurrentData(QString path){ QString ret = path; if(ret == "") { ret = showFileDialog(QFileDialog::AcceptSave, "Save"); } if(ret != "") { QFile file(ret); if(file.open(QIODevice::WriteOnly | QIODevice::Text)) { QTextStream out(&file); out << QString(mainEdit.toPlainText()); file.close(); setWindowTitle("NotePad - [" ret "]"); m_isTextChanged = false;//保存后修改狀態(tài)值 } else { showErrorMessage(QString("Open file Error!nn") """ m_filePath """); ret = ""; } } return ret;}void MainWindow::preEditorChanged(){ if(m_isTextChanged) { int r = showQuesstionMessage("Do you want to Save?"); switch (r) { case QMessageBox::Yes: saveCurrentData(m_filePath); break; case QMessageBox::No: m_isTextChanged = false; break; case QMessageBox::Cancel: break; } }}void MainWindow::openFileToEdit(QString path){ if(path != "") { QFile file(path); if(file.open(QIODevice::ReadOnly | QIODevice::Text)) { mainEdit.setPlainText(QString(file.readAll())); file.close(); m_filePath = path;//報(bào)存當(dāng)前文件路徑 m_isTextChanged = false;//一定記得修改這個(gè) setWindowTitle("NotePad - [" m_filePath "]"); } else { showErrorMessage(QString("Open file Error!nn") """ m_filePath """); } }}QToolBar* MainWindow::toolBar(){ QToolBar* ret = NULL; const QObjectList& list = children();//獲得主窗口的組件 for(int i=0; i<list.count(); i ) { QToolBar* tb = dynamic_cast<QToolBar*>(list[i]);//只有工具欄能夠轉(zhuǎn)換成功 if(tb != NULL) { ret = tb; } } return ret;}QAction* MainWindow::findMenuBarAction(QString text){ QAction* ret = NULL; const QObjectList& list = menuBar()->children();//獲得菜單欄 for(int i=0; i<list.count(); i ) { QMenu* menu = dynamic_cast<QMenu*>(list[i]);//獲得每個(gè)子菜單 if(menu != NULL) { for(int j=0; j<menu->actions().count(); j ) { if(menu->actions()[j]->text().startsWith(text)) { ret = menu->actions()[j]; break; } } } } return ret;}QAction* MainWindow::findToolBarAction(QString text){ QAction* ret = NULL; QToolBar* tb = toolBar(); if(tb != NULL) { for(int j=0; j<tb->actions().count(); j ) { if(tb->actions()[j]->toolTip().startsWith(text))//注意工具欄里面的是toolTip() { ret = tb->actions()[j]; break; } } } return ret;}void MainWindow::onFileNew(){ preEditorChanged(); if(!m_isTextChanged) { mainEdit.clear(); m_filePath = ""; m_isTextChanged = false; setWindowTitle("NotePad-[New]"); }}void MainWindow::onFileOpen(){ preEditorChanged(); if(!m_isTextChanged) { QString path = showFileDialog(QFileDialog::AcceptOpen, "open"); openFileToEdit(path); }}void MainWindow::onFlieSave(){ QString path = saveCurrentData(m_filePath); if(path != "") { m_filePath = path; }}void MainWindow::onFileSaveAs(){ QString path = saveCurrentData();//使用默認(rèn)參數(shù) if(path != "") { m_filePath = path; }}void MainWindow::onFilePrint(){ QPrintDialog dlg(this); dlg.setWindowTitle("Print"); if(dlg.exec()==QPrintDialog::Accepted) { QPrinter* p = dlg.printer();//將參數(shù)設(shè)置進(jìn)打印機(jī) mainEdit.document()->print(p); }}void MainWindow::onTextChanged(){ if(!m_isTextChanged) { setWindowTitle("*" windowTitle()); } m_isTextChanged = true;}void MainWindow::onCopyAvailable(bool available){ QAction* action = findToolBarAction("復(fù)制"); if(action != NULL) { action->setEnabled(available); } action = findMenuBarAction("復(fù)制"); if(action != NULL) { action->setEnabled(available); } action = findMenuBarAction("剪切"); if(action != NULL) { action->setEnabled(available); } action = findToolBarAction("剪切"); if(action != NULL) { action->setEnabled(available); }}void MainWindow::onUndoAvailable(bool available){ QAction* action = findMenuBarAction("撤銷"); if(action != NULL) { action->setEnabled(available); } action = findToolBarAction("撤銷"); if(action != NULL) { action->setEnabled(available); }}void MainWindow::onRedoAvailable(bool available){ QAction* action = findMenuBarAction("重做"); if(action != NULL) { action->setEnabled(available); } action = findToolBarAction("重做"); if(action != NULL) { action->setEnabled(available); }}void MainWindow::closeEvent(QCloseEvent *e)//沒有對(duì)應(yīng)的信號(hào)來處理,只能重寫事件處理函數(shù){ preEditorChanged(); QFont font = mainEdit.font(); bool isWrap = (mainEdit.lineWrapMode() == QPlainTextEdit::WidgetWidth); bool tbVisible = (findMenuBarAction("工具欄")->isChecked() && (findToolBarAction("工具欄")->isChecked())); bool sbVisible = (findMenuBarAction("狀態(tài)欄")->isChecked() && (findToolBarAction("狀態(tài)欄")->isChecked())); AppConfig config(font, tbVisible, sbVisible, isWrap, size(), pos()); config.store(); if(!m_isTextChanged) { QMainWindow::closeEvent(e);//調(diào)用父類的關(guān)閉事件處理函數(shù) } else { e->ignore();//點(diǎn)取消的話就忽略這個(gè)對(duì)話框 }}void MainWindow::dragEnterEvent(QDragEnterEvent* e){ if(e->mimeData()->hasUrls()) { e->acceptProposedAction(); } else { e->ignore(); }}void MainWindow::dropEvent(QDropEvent* e){ if(e->mimeData()->hasUrls()) { QList<QUrl> list = e->mimeData()->urls(); QString path = list[0].toLocalFile(); QFileInfo fi(path); if(fi.isFile()) { preEditorChanged(); if(!m_isTextChanged) { openFileToEdit(path); } } else { showErrorMessage("Canot open a folder"); } } else { e->ignore(); }}void MainWindow::onCursorPositionChanged(){ int pos = mainEdit.textCursor().position(); QString text = mainEdit.toPlainText();//獲取編輯器中的內(nèi)容 int ln = 0; int col = 0; int flag = -1; for(int i=0; i<pos; i ) { if(text[i] == 'n') { ln ; flag = i; } } flag ;//如光標(biāo)在第十一行的某個(gè)位置,那么一共有十個(gè)換行符,flag 就是在第十行最后一個(gè)位置(即最后一個(gè)光標(biāo)所在位置) col = pos - flag;//用第十一行光標(biāo)所在位置減去前十行的總數(shù)就得第十一行的字符個(gè)數(shù),即縱坐標(biāo) statusLabel.setText("行: " QString::number(ln 1) " 列: " QString::number(col 1));}void MainWindow::onFileDelete(){ QKeyEvent keyPress(QEvent::KeyPress, Qt::Key_Delete,Qt::NoModifier); QKeyEvent keyRelease(QEvent::KeyRelease, Qt::Key_Delete,Qt::NoModifier); QApplication::sendEvent(&mainEdit, &keyPress);//發(fā)送事件模擬鍵盤的delete鍵 QApplication::sendEvent(&mainEdit, &keyRelease);}void MainWindow::onFileExit(){ close();}void MainWindow::onEditFind(){ m_findDialogPoiter->show();}void MainWindow::onEditReplce(){ m_ReplaceDialogPoiter->show();}void MainWindow::onEditGoto(){ bool ok = false; int ln = QInputDialog::getInt(this, "轉(zhuǎn)到", "行號(hào): ", 1, 1, mainEdit.document()->lineCount(), 1, &ok);//第一步 if(ok) { QString text = mainEdit.toPlainText(); QTextCursor c = mainEdit.textCursor(); int pos = 0; int next = -1; for(int i=0; i<ln; i )//第二步 { pos = next 1;//最后一個(gè)換行符的下一個(gè)字符就是目標(biāo)行的第一個(gè)字符 next = text.indexOf('n', pos); } c.setPosition(pos);//第三步 mainEdit.setTextCursor(c); }}void MainWindow::onViewStatusBar(){ QStatusBar* sb = statusBar(); bool visible = sb->isVisible(); sb->setVisible(!visible); QAction* action = findMenuBarAction("狀態(tài)欄"); if(action != NULL) { action->setChecked(!visible); } action = findToolBarAction("狀態(tài)欄"); if(action != NULL) { action->setChecked(!visible); }}void MainWindow::onViewToolBar(){ QToolBar* tb = toolBar(); if(tb != NULL) { bool visible = tb->isVisible(); tb->setVisible(!visible); QAction* action = findMenuBarAction("工具欄"); if(action != NULL) { action->setChecked(!visible); } action = findToolBarAction("工具欄"); if(action != NULL) { action->setChecked(!visible); } }}void MainWindow::onHelpAbout(){ AboutDialog(this).exec();}void MainWindow::onFontDialog(){ bool ok = false; QFont font = QFontDialog::getFont(&ok, mainEdit.font(), this); if( ok ) { mainEdit.setFont(font); }}void MainWindow::onHelpFindHelp(){ QDesktopServices::openUrl(QUrl("http://www.baidu.com"));}void MainWindow::onFormatWrap(){ QPlainTextEdit::LineWrapMode mode = mainEdit.lineWrapMode(); if( mode == QPlainTextEdit::NoWrap )//如果不是自動(dòng)換行 { mainEdit.setLineWrapMode(QPlainTextEdit::WidgetWidth);//就反轉(zhuǎn)為自動(dòng)換行 findMenuBarAction("換行")->setChecked(true); findToolBarAction("換行")->setChecked(true); } else { mainEdit.setLineWrapMode(QPlainTextEdit::NoWrap); findMenuBarAction("換行")->setChecked(false); findToolBarAction("換行")->setChecked(false); }}void MainWindow::openFile(QString path){ preEditorChanged(); if(!m_isTextChanged) { openFileToEdit(path); }}

主函數(shù)main.cpp

#include <QtGui/QApplication>#include "MainWindow.h"#include <QTextCodec>#include <QFileInfo>int main(int argc, char *argv[]){ QTextCodec::setCodecForLocale(QTextCodec::codecForName("UTF-8")); //路徑名支持中文 QTextCodec::setCodecForTr(QTextCodec::codecForName("UTF-8")); //QString支持中文 QTextCodec::setCodecForCStrings(QTextCodec::codecForName("UTF-8")); //string支持中文 QApplication a(argc, argv); MainWindow* w = MainWindow::NewInstance(); int ret = -1; if(w != NULL) { if(argc > 1) { QFileInfo fi(argv[1]); if(fi.exists()) { w->openFile(fi.absoluteFilePath()); } } w->show(); ret = a.exec(); } delete w; return ret;}

資源文件:

Qt開發(fā)-應(yīng)用程序的打包與發(fā)布(qt程序打包發(fā)布安裝)

五、小結(jié)

(1)、程序開發(fā)時(shí)能夠生成debug和release版

(2)、打包時(shí)首先要解決程序的庫(kù)依賴

(3)、程序執(zhí)行需要特殊環(huán)境依賴時(shí)必須開發(fā)部署程序

(4)、Windows中有豐富的部署程序開發(fā)工具可供選擇

(5)、Linux中可以采用Shell腳本開發(fā)部署程序  

【領(lǐng)QT開發(fā)教程學(xué)習(xí)資料,點(diǎn)擊下方鏈接莬費(fèi)領(lǐng)取↓↓,先碼住不迷路~】

「鏈接」

相關(guān)新聞

聯(lián)系我們
聯(lián)系我們
在線咨詢
分享本頁(yè)
返回頂部