基于QT記事本源代碼_第1頁
基于QT記事本源代碼_第2頁
基于QT記事本源代碼_第3頁
基于QT記事本源代碼_第4頁
基于QT記事本源代碼_第5頁
已閱讀5頁,還剩9頁未讀, 繼續(xù)免費(fèi)閱讀

下載本文檔

版權(quán)說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請進(jìn)行舉報(bào)或認(rèn)領(lǐng)

文檔簡介

1、基于QT記事本源代碼dialog.h#include "mainwindow.h"#ifndef DIALOG_H#define DIALOG_H#include <QDialog>namespace Ui class Dialog;class Dialog : public QDialog Q_OBJECTpublic: explicit Dialog(QWidget *parent = 0); Dialog();private slots: void on_buttonBox_clicked(QAbstractButton *button); void on

2、_pushButton_clicked(); void on_pushButton_2_clicked(); void on_pushButton_3_clicked();private: Ui:Dialog *ui;#endif / DIALOG_Hmainwindow.h#ifndef MAINWINDOW_H#define MAINWINDOW_H#include <QMainWindow>#include <QFileDialog>#include <QFile>#include <QDir>#include <QMessageBo

3、x>#include <QTextStream>#include <QFont>#include <QFontDialog>#include <QColor>#include <QColorDialog>#include <QDateTime>#include <QDesktopServices>#include <QUrl>namespace Ui class MainWindow;class MainWindow : public QMainWindow Q_OBJECTpublic: e

4、xplicit MainWindow(QWidget *parent = 0); MainWindow();private: Ui:MainWindow *ui;private slots: void newFileSlot();/ nfs() void openFileSlot(); / open a text file void saveFileSlot(); void setColorSlot(); void setFontSlot(); void setDateTimeSlot(); void aboutSoftSlot(); void aboutWebSiteSlot(); void

5、 on_actionAa_destroyed(QObject *arg1); void on_actionS_4_triggered(); void on_actionAa_triggered(); void on_actionA_triggered();#endif / MAINWINDOW_Hnew_datebase.h#ifndef NEW_DATEBASE_H#define NEW_DATEBASE_H#include "QWidget"#include "qsql.h"#include "QSqlDatabase"#incl

6、ude "QSqlQuery"#include "QSqlQueryModel"#include "QDebug"#include <QDialog>#include "QSqlDriver"namespace Ui class new_datebase;class new_datebase : public QDialog Q_OBJECTpublic: explicit new_datebase(QWidget *parent = 0); new_datebase();private slots:

7、void on_pushButton_clicked(); void on_lineEdit_editingFinished();private: Ui:new_datebase *ui; bool connectDB();#endif / NEW_DATEBASE_Hdialog.cpp#include "new_datebase.h"#include "dialog.h"#include "mainwindow.h"#include "ui_dialog.h"#include "QtSql"

8、#include <cstdlib>Dialog:Dialog(QWidget *parent) : QDialog(parent), ui(new Ui:Dialog) ui->setupUi(this);Dialog:Dialog() delete ui;void Dialog:on_pushButton_clicked() MainWindow *m = new MainWindow(); m->show();void Dialog:on_pushButton_2_clicked() QSqlDatabase database = QSqlDatabase:add

9、Database("QSQLITE"); database.setDatabaseName("C:UsersAdministratorDesktoptext/aaa.db");/必須用絕對路徑,不知道為什么 database.open(); QSqlQuery query; QString ss,qq,s,q; q = ui->lineEdit_2->text();query.exec("select * from bb where 賬號 = "+ ui->lineEdit->text() );query.ne

10、xt();qq = query.value(1).toString().trimmed();qDebug()<<qq;if(q != NULL && q = qq) new_datebase *m = new new_datebase; m->show(); database.close();void Dialog:on_pushButton_3_clicked() this->close();main.cpp#include "dialog.h"#include "mainwindow.h"#include &l

11、t;QApplication>#include "qsqlquery.h"#include "QDebug"#include "QSqlDatabase"#include "iostream"#include "QList"#include "iostream"#include "windows.h"#include "shellapi.h"#include "QSqlDriver"#include "

12、;QDebug"#include "QSqlQueryModel"#include "QSqlQuery"#include "qsql.h"#include "QWidget"using namespace std;int main(int argc, char *argv) QApplication a(argc, argv); MainWindow w; w.show(); QSqlQueryModel *model = new QSqlQueryModel; QSqlDatabase db = QS

13、qlDatabase:addDatabase("QSQLITE"); db.setDatabaseName("C:UsersAdministratorDesktopnew one/textfile.db"); bool ku = db.open(); Widge w; if(ku) model->setQuery("select * from textfile"); model->setQuery("insert int student values(1,'a"); w.show(); else

14、 qDebug()<<"abcd" return a.exec();mainwindow.cpp#include "new_datebase.h"#include "dialog.h"#include "mainwindow.h"#include "ui_mainwindow.h"#include "QtSql"#include "string"#include "QtGui"#include "qfile.h&q

15、uot;MainWindow:MainWindow(QWidget *parent) : QMainWindow(parent), ui(new Ui:MainWindow) ui->setupUi(this); QObject:connect(ui->actionSs,SIGNAL(triggered(),this,SLOT(newFileSlot(); QObject:connect(ui->actionSs_2,SIGNAL(triggered(),this,SLOT(openFileSlot(); QObject:connect(ui->actionA,SIGN

16、AL(triggered(),this,SLOT(saveFileSlot(); QObject:connect(ui->actionSs_8,SIGNAL(triggered(),this,SLOT(close(); QObject:connect(ui->actionSs_5,SIGNAL(triggered(),ui->textEdit,SLOT(copy(); QObject:connect(ui->actionSs_4,SIGNAL(triggered(),ui->textEdit,SLOT(undo(); QObject:connect(ui->

17、actionSs_9,SIGNAL(triggered(),ui->textEdit,SLOT(paste(); QObject:connect(ui->actionS_2,SIGNAL(triggered(),ui->textEdit,SLOT(selectAll(); QObject:connect(ui->actionSs_10,SIGNAL(triggered(),ui->textEdit,SLOT(cut(); QObject:connect(ui->actionSdf,SIGNAL(triggered(),this,SLOT(setFontSlo

18、t(); QObject:connect(ui->actionS_3,SIGNAL(triggered(),this,SLOT(on_actionAa_destroyed(); /set current date time slot QObject:connect(ui->actionS,SIGNAL(triggered(),this,SLOT(setDateTimeSlot(); QObject:connect(ui->actionSs_12,SIGNAL(triggered(),this,SLOT(aboutSoftSlot(); QObject:connect(ui-&

19、gt;actionAa,SIGNAL(triggered(),this,SLOT(aboutSoftSlot(); /QObject:connect(ui->aboutQtAction,SIGNAL(triggered(),qApp,SLOT(aboutQt(); / QObject:connect(ui->編輯,SIGNAL(triggered(),ui->textEdit,SLOT(redo();MainWindow:MainWindow() delete ui;void MainWindow:newFileSlot() /clear the text edit ui-&

20、gt;textEdit->clear(); this->setWindowTitle("untitled-notepad ");void MainWindow:openFileSlot() QString fileName=QFileDialog:getOpenFileName(this,"Open Text File",QDir:currentPath(); if(fileName.isEmpty() return ; QFile *file =new QFile; file->setFileName(fileName); /bool

21、ean bool ok=file->open(QIODevice:ReadOnly); /if file open error if(ok) QTextStream in(file); ui->textEdit->setText(in.readAll(); file->close(); delete file; file=NULL; else QMessageBox:information(this,"Error Message","Can't open File"); delete file; return; void

22、MainWindow:saveFileSlot() QString fileName=QFileDialog:getSaveFileName(this,"Save Text File",QDir:currentPath(); if(fileName.isEmpty() return; QFile *file =new QFile; /must set a file name file->setFileName(fileName); bool ok=file->open(QIODevice:WriteOnly); if(ok) QTextStream out(fi

23、le); out<<ui->textEdit->toPlainText(); file->close(); delete file; file=NULL; else QMessageBox:information(this,"Error Message ","Save File Error "); delete file; file=NULL; return; void MainWindow:setFontSlot() bool ok; QFont font = QFontDialog:getFont( &ok, Q

24、Font("Helvetica Cronyx", 10), this); if (ok) ui->textEdit->setFont(font); / the user clicked OK and font is set to the font the user selected else / the user canceled the dialog; font is set to the initial / value, in this case Helvetica Cronyx, 10 QMessageBox:information(this,"

25、Error Message","Please Select a Font"); return ; void MainWindow:setColorSlot() /* QColor color; if (native->isChecked() color = QColorDialog:getColor(Qt:green, this); else color = QColorDialog:getColor(Qt:green, this, "Select Color", QColorDialog:DontUseNativeDialog); if

26、 (color.isValid() colorLabel->setText((); colorLabel->setPalette(QPalette(color); colorLabel->setAutoFillBackground(true); */ QColor color=QColorDialog:getColor(Qt:green, this); if(color.isValid() ui->textEdit->setTextColor(color); else QMessageBox:information(this,"Err

27、or message","Please Select correct color"); return ; void MainWindow:setDateTimeSlot() QDateTime current=QDateTime:currentDateTime(); ui->textEdit->append(current.toString("yyyy-MM-dd hh-mm-ss");void MainWindow:aboutWebSiteSlot() QDesktopServices:openUrl(QUrl("&qu

28、ot;);void MainWindow:on_actionS_4_triggered() Dialog * m = new Dialog; m->show();void MainWindow:on_actionA_triggered() QString sFilePath = "D:test.txt" QFile file(sFilePath); /方式:Append為追加,WriteOnly,ReadOnly if (!file.open(QIODevice:WriteOnly|QIODevice:Text) QMessageBox:critical(NULL,

29、"提示", "無法創(chuàng)建文件"); QTextStream out(&file); out<<"要寫入內(nèi)容"<<endl; out.flush(); file.close(); /*QSqlDatabase db = QSqlDatabase:addDatabase("QSQLITE"); /添加數(shù)據(jù)庫驅(qū)動(dòng) db.setDatabaseName("C:UsersAdministratorDesktoptext/aaa.db"); /數(shù)據(jù)庫連接命名 if(!db.op

30、en() /打開數(shù)據(jù)庫 QSqlQuery query; query.exec("select * from bb "); query.exec("insert into textfile values(1,q,1456)"); file.close();*/new_datebase.cpp#include "new_datebase.h"#include "dialog.h"#include "mainwindow.h"#include "new_datebase.h"#i

31、nclude "ui_new_datebase.h"#include "QtSql"#include "QFile"#include "QtGui"#include <cstdlib>#include "QSqlDatabase"#include "iostream"#include "QList"#include "iostream"#include "windows.h"#include "

32、shellapi.h"bool connectDB() QSqlDatabase db = QSqlDatabase:addDatabase("QSQLITE"); db.setDatabaseName("textfile.txt"); if(!db.open() return false; else return true; new_datebase:new_datebase(QWidget *parent) : QDialog(parent),ui(new Ui:new_datebase) ui->setupUi(this); QSq

33、lDatabase db = QSqlDatabase:addDatabase("QSQLITE"); db.setDatabaseName("C:UsersAdministratorDesktoptext/aaa.db"); db.open(); QSqlQuery query; while(query.next() QString name=query.value(0).toString(); QString ppth = query.value(1).toString(); QString Num = query.value(2).toString

34、(); QListWidgetItem *q =new QListWidgetItem(name); QListWidgetItem *v =new QListWidgetItem(ppth); QListWidgetItem *b =new QListWidgetItem(Num); ui->listWidget->addItem(q); ui->listWidget->addItem(v); ui->listWidget->addItem(b); QList<QListWidgetItem*> list =ui->listWidget->selectedItems(); if(list.size()!=0) QListWidgetItem* sel = list0; if (sel) /ui->lineEdit_xm->setText(s

溫馨提示

  • 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請下載最新的WinRAR軟件解壓。
  • 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
  • 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁內(nèi)容里面會(huì)有圖紙預(yù)覽,若沒有圖紙預(yù)覽就沒有圖紙。
  • 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
  • 5. 人人文庫網(wǎng)僅提供信息存儲(chǔ)空間,僅對用戶上傳內(nèi)容的表現(xiàn)方式做保護(hù)處理,對用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對任何下載內(nèi)容負(fù)責(zé)。
  • 6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請與我們聯(lián)系,我們立即糾正。
  • 7. 本站不保證下載資源的準(zhǔn)確性、安全性和完整性, 同時(shí)也不承擔(dān)用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。

評論

0/150

提交評論