目录
一、注册登录功能
1、头文件:form.h。功能实现主要引用数据库保存用户信息,同时使用QPaintEvent事件和QPainter画家来实现。
2、form.cpp实现功能代码
3、效果
二、闹钟功能
2、form1.cpp文件,实现功能
3、效果
三、垃圾狗
设计文件操作,可以把这个小功能在桌面上拖动,并且把文件拖到狗身上时会把垃圾吃掉
2、formdog.cpp文件实现功能
3、效果图
四、主窗口功能
1、mainwindow.h头文件,包含其他前面所提到的各种类与功能,通过在主窗口调用其他类来实现功能;
2、mianwindow.cpp文件实现布局、菜单、工具栏、以及各种文件、样式、颜色字体的设计
3、效果图
五、main.cpp与.pro文件
1、main.cpp
2、qtproject.pro
六、总结
一、注册登录功能
1、头文件:form.h。功能实现主要引用数据库保存用户信息,同时使用QPaintEvent事件和QPainter画家来实现。
#ifndef FORM_H
#define FORM_H
#include <QWidget>
#include <QMediaPlayer>
#include <QTimeEdit>
#include <QTimer>
#include <QMainWindow>
#include "mainwindow.h"
#include <QWidget>
#include <QSqlDatabase>
#include <QPaintEvent>
#include <QPainter>
namespace Ui {
class Form;
}class Form : public QWidget
{Q_OBJECT
signals:void loginsuccess();
public:explicit Form(QWidget *parent = 0);~Form();private slots:void on_pushButton_2_clicked();void on_pushButton_clicked();bool selectdatabase(QString user, QString pass);void insertdatabase(QString user, QString pass);private:Ui::Form *ui;void paintEvent(QPaintEvent *event);QSqlDatabase db;
};#endif // FORM_H
2、form.cpp实现功能代码
#include "form.h"
#include "ui_form.h"
#include <QMessageBox>
#include <QSqlQuery>
#include <QDebug>
#include <QDialog>
Form::Form(QWidget *parent) :QWidget(parent),ui(new Ui::Form)
{ui->setupUi(this);//创建数据库db = QSqlDatabase::addDatabase("QSQLITE");db.setDatabaseName("user_pass");bool ok = db.open();//建表QSqlQuery query("create table USERPASS('user', 'pass')");}Form::~Form()
{delete ui;
}bool Form::selectdatabase(QString user, QString pass)
{//查询数据库QSqlQuery query(QString("select * from USERPASS where user='%0' and pass='%1'").arg(user).arg(pass));while (query.next()) {QString str = query.value(0).toString();QString str1 = query.value(1).toString();qDebug()<<str << str1;return true;}return false;
}void Form::insertdatabase(QString user, QString pass)
{//插入数据库QSqlQuery query(QString("insert into USERPASS values('%0','%1')").arg(user).arg(pass));
}void Form::on_pushButton_2_clicked()
{//if(ui->lineEdit_user->text() == ui->lineEdit_pass->text())if(selectdatabase(ui->lineEdit_user->text(), ui->lineEdit_pass->text())){emit loginsuccess();close();}elseQMessageBox::warning(this, "提示", "账号或密码错误", QMessageBox::Ok);
}void Form::on_pushButton_clicked()
{insertdatabase(ui->lineEdit_user->text(), ui->lineEdit_pass->text());}void Form::paintEvent(QPaintEvent *event)
{QPainter *painter = new QPainter(this);QPixmap pic = QPixmap(":/picture/moon.png");//pic = pic.scaled(this->width(),this->height());painter->drawPixmap(0,0,this->width(),this->height(),pic);}
3、效果
二、闹钟功能
1、form1.h文件:定义了单选按钮和普通按键来实现闹钟的开始与选择闹铃
#ifndef FORM1_H
#define FORM1_H#include <QWidget>
#include <QWidget>
#include <QMediaPlayer>
#include <QTimeEdit>
#include <QTimer>
#include <QMainWindow>
#include "mainwindow.h"
#include <QPaintEvent>
#include <QPainter>namespace Ui {
class Form1;
}class Form1 : public QWidget
{Q_OBJECTpublic:explicit Form1(QWidget *parent = nullptr);~Form1();
public slots:void TimerResponse(); //时间响应槽,不断检测:判断时间是否到响铃时间void on_pushButtonSet_clicked();void on_pushButtonControl_clicked();void on_pushButtonReset_clicked();void on_radioButtonMusicOne_clicked();void on_radioButtonMusicTwo_clicked();void on_radioButtonMusicThree_clicked();private:Ui::Form1 *ui;QTimer *m_myTimer = new QTimer();QMediaPlayer *player = new QMediaPlayer;QTime m_Temp;int tt = 0;Form1 *clok;void paintEvent(QPaintEvent *event);
};#endif // FORM1_H
2、form1.cpp文件,实现功能
#include "form1.h"
#include "ui_form1.h"
#include <QLCDNumber>
#include "mainwindow.h"
#include "ui_mainwindow.h"Form1::Form1(QWidget *parent) :QWidget(parent),ui(new Ui::Form1)
{ui->setupUi(this);connect(m_myTimer, SIGNAL(timeout()), this, SLOT(TimerResponse()));player->setVolume(50);//设置播放音量}
void Form1::TimerResponse()
{if(m_Temp.hour() == QTime::currentTime().hour()&&m_Temp.minute() == QTime::currentTime().minute()){qDebug() << "响铃";player->play(); //播放音乐m_myTimer->stop(); //停止播放}
}void Form1::on_pushButtonSet_clicked()
{m_Temp = ui->timeEdit->time();m_myTimer->start(500);//计时器开始计时,每隔0.5毫秒发出信号qDebug() << "计时开始!";
}void Form1::on_pushButtonControl_clicked()
{tt++;if(tt == 10) tt = 0;else if(tt % 2 == 1)player->play();elseplayer->stop();
}void Form1::on_pushButtonReset_clicked()
{player->stop();m_myTimer->stop();//停止计时
}void Form1::on_radioButtonMusicOne_clicked()
{player->setMedia(QUrl::fromLocalFile(":/music/乔佳旭 - 雪下的时候.mp3"));
}void Form1::on_radioButtonMusicTwo_clicked()
{player->setMedia(QUrl::fromLocalFile(":/music/王蓝茵 - 恶作剧.mp3"));
}void Form1::on_radioButtonMusicThree_clicked()
{player->setMedia(QUrl::fromLocalFile(":/music/许嵩 - 有何不可.mp3"));
}
Form1::~Form1()
{delete ui;
}
void Form1::paintEvent(QPaintEvent *event)
{QPainter *painter = new QPainter(this);QPixmap pic = QPixmap(":/picture/bridge.png");//pic = pic.scaled(this->width(),this->height());painter->drawPixmap(0,0,this->width(),this->height(),pic);}
3、效果
三、垃圾狗
设计文件操作,可以把这个小功能在桌面上拖动,并且把文件拖到狗身上时会把垃圾吃掉
1、formdog.h文件以各种事件来支撑,实现功能
#ifndef FORMDOG_H
#define FORMDOG_H
#include "mainwindow.h"
#include <QDialog>
#include <QWidget>
#include <QPainter>
#include <QMouseEvent>
#include <QDebug>
#include <QSystemTrayIcon>
#include <QPushButton>
#include <QBitmap>
#include <QPixmap>#include <QDragEnterEvent>
#include <QDropEvent>
#include <QMimeData>
#include <QLockFile>
#include <windows.h>
#include <QMenu>
#include <QAction>
#include <QWidget>namespace Ui {
class Formdog;
}class Formdog : public QWidget
{Q_OBJECTpublic:explicit Formdog(QWidget *parent = 0);~Formdog();private:Ui::Formdog *ui;public:Widget(QWidget *parent = 0);bool deletefile(QString &filepath);wchar_t *toWCharT(QString a_string);void paintEvent(QPaintEvent *event){QPainter p(this);p.drawPixmap(0,0,pix->scaled(300,300));}void mouseMoveEvent(QMouseEvent *event){pos_g = event->globalPos();update();this->move(pos_g.x()-x,pos_g.y()-y);}void mousePressEvent(QMouseEvent *event){x = event->pos().x();y = event->pos().y();}void mouseReleaseEvent(QMouseEvent *event){pix = new QPixmap(":/picture/dog.png");update();this->setMask(pix->scaled(300,300).mask());}void dragEnterEvent(QDragEnterEvent *event){pix = new QPixmap(":/picture/dog2.png");update();this->setMask(pix->scaled(300,300).mask());if(event->mimeData()->hasUrls())//判断拖拽文件是否有文件路径{event->acceptProposedAction();//接受时间数据中的文件路径}else{event->ignore();}}void dropEvent(QDropEvent *event){const QMimeData *data = event->mimeData();if(data->hasUrls()){QList<QUrl> urls = data->urls();QString file_path = urls.at(0).toLocalFile();deletefile(file_path);}}void leaveEvent(QEvent *event){pix = new QPixmap(":/picture/dog.png");update();this->setMask(pix->scaled(300,300).mask());}QPixmap *pix;QPoint pos_g;QSystemTrayIcon *ti;int x;int y;QMenu *mn;//托盘菜单QAction *action1;QAction *action2;public slots:void win_show();void win_eixt();};#endif // FORMDOG_H
2、formdog.cpp文件实现功能
#include "formdog.h"
#include "ui_formdog.h"
#include <QDir>
#include <QFileInfo>
#include <QApplication>Formdog::Formdog(QWidget *parent) :QWidget(parent),ui(new Ui::Formdog)
{ui->setupUi(this);pix = new QPixmap(":/picture/dog.png");this->setMask(pix->scaled(300,300).mask());ti = new QSystemTrayIcon;ti->setIcon(QIcon(":/picture/dog.png"));ti->setToolTip("小狗");ti->show();mn = new QMenu(this);action1 = new QAction;action2 = new QAction;action1->setText("显示");action2->setText("退出");mn->addAction(action1);mn->addAction(action2);ti->setContextMenu(mn);connect(action1,SIGNAL(triggered(bool)),this,SLOT(win_show()));connect(action2,SIGNAL(triggered(bool)),this,SLOT(win_eixt()));setWindowIcon(QIcon(":/picture/dog.png"));setWindowTitle("小狗");setAcceptDrops(true);//设置窗口接受拖拽文件操作}bool Formdog::deletefile(QString &filepath){/*QFileInfo fileinfo(filepath);if(fileinfo.isFile())//判断该路径指向的文件是否为普通文件{QFile::remove(filepath);qDebug() << "i eat " << filepath;}else if(fileinfo.isDir())//判断该文件是否为文件夹{QDir dir(filepath);dir.removeRecursively();qDebug() << "i eat " << filepath;}return true;*/bool ret = true;SHFILEOPSTRUCT opRecycle;opRecycle.hwnd = nullptr;//显示状态信息窗口的句柄,一般设为主窗体的句柄opRecycle.wFunc = FO_DELETE;// 要执行的操作 FO_DELETE表示删除//opRecycle.pFrom = toWCharT(filepath);// 源文件或目录opRecycle.pTo = L"\0\0";// 目标文件或目录opRecycle.fFlags = FOF_ALLOWUNDO; // 操作是否放弃此Flag表示可以撤销操作opRecycle.hNameMappings = nullptr;// 文件名映射对象的句柄,很少用opRecycle.lpszProgressTitle = L"Recycling files...";// 进度条标题,仅在 fFlags 标志中指定了 FOF_SIMPLEPROGRESS 时有效if(SHFileOperation(&opRecycle) != 0){ret = false;}if(opRecycle.fAnyOperationsAborted){ret = false;}return ret;}void Formdog::win_show(){this->showNormal();}void Formdog::win_eixt(){ti->deleteLater();QApplication *win;win->exit();}Formdog::~Formdog()
{delete ui;
}
3、效果图
四、主窗口功能
实现菜单,工具栏,设置快捷方式
1、mainwindow.h头文件,包含其他前面所提到的各种类与功能,通过在主窗口调用其他类来实现功能;
#ifndef MAINWINDOW_H
#define MAINWINDOW_H#include <QMainWindow>
#include <QTextEdit>
#include <QLabel>
#include "form.h"
#include "form1.h"
#include <QCalendarWidget>
#include "formdog.h"#include <QDialog>
#include <QWidget>
#include <QPainter>
#include <QMouseEvent>
#include <QDebug>
#include <QSystemTrayIcon>
#include <QPushButton>
#include <QBitmap>
#include <QPixmap>#include <QDragEnterEvent>
#include <QDropEvent>
#include <QMimeData>
#include <QLockFile>
#include <windows.h>
#include <QMenu>
#include <QAction>
#include <QWidget>
#include <QPaintEvent>
#include <QPainter>
namespace Ui {
class MainWindow;
}class MainWindow : public QMainWindow
{Q_OBJECTpublic slots:void open_file();void new_file();void save_file();void saveas_file();void close_file();void copy_file();void cut_file();void paste_file();void set_color();void set_font();void about();void file_changed();void show_time();void show_calendar();void dogshow();public:explicit MainWindow(QWidget *parent = 0);~MainWindow();private:Ui::MainWindow *ui;Ui::MainWindow *ui2;Ui::MainWindow *ui3;QAction *newAction(QString text, QString Icon, QString shortcut );QTextEdit *te;QLabel *lb;QString filename; //正在展开的文件的路径bool first_open;int Flag_isOpen = 0; //标记:判断是否打开或创建了一个文件int Flag_IsNew = 0; //标记:如果新建了文件就为1,初始值为0QString Last_FileName; //最后一次保存的文件的名字QString Last_FileContent; //最后一次保存文件的内容// clock *c ;QCalendarWidget *calen;private:bool deletefile(QString &filepath);wchar_t *toWCharT(QString a_string);void paintEvent(QPaintEvent *event){QPainter p(this);p.drawPixmap(0,0,pix->scaled(300,300));QPainter *painter = new QPainter(this);QPixmap pic = QPixmap(":/picture/bridge.png");//pic = pic.scaled(this->width(),this->height());painter->drawPixmap(0,0,this->width(),this->height(),pic);}void mouseMoveEvent(QMouseEvent *event){pos_g = event->globalPos();update();this->move(pos_g.x()-x,pos_g.y()-y);}void mousePressEvent(QMouseEvent *event){x = event->pos().x();y = event->pos().y();}void mouseReleaseEvent(QMouseEvent *event){pix = new QPixmap(":/picture/tool.png");update();this->setMask(pix->scaled(300,300).mask());}void dragEnterEvent(QDragEnterEvent *event){pix = new QPixmap(":/picture/tool.png");update();this->setMask(pix->scaled(300,300).mask());if(event->mimeData()->hasUrls())//判断拖拽文件是否有文件路径{event->acceptProposedAction();//接受时间数据中的文件路径}else{event->ignore();}}void dropEvent(QDropEvent *event){const QMimeData *data = event->mimeData();if(data->hasUrls()){QList<QUrl> urls = data->urls();QString file_path = urls.at(0).toLocalFile();}}void leaveEvent(QEvent *event){pix = new QPixmap(":/picture/tool.png");update();this->setMask(pix->scaled(300,300).mask());}QPixmap *pix;QPoint pos_g;QSystemTrayIcon *ti;int x;int y;QMenu *mn;//托盘菜单QAction *action1;QAction *action2;public slots:void win_show();void win_eixt();};#endif // MAINWINDOW_H
2、mianwindow.cpp文件实现布局、菜单、工具栏、以及各种文件、样式、颜色字体的设计
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include "form.h"
#include <QTextEdit>
#include <QAction>
#include <QMenu>
#include <QMenuBar>
#include <QDebug>
#include <QToolBar>
#include <QStatusBar>
#include <QFileDialog>
#include <QFile>
#include <QDebug>
#include <QMessageBox>
#include <QString>
#include <QFileInfo>
#include <QColorDialog>
#include <QFontDialog>
#include <QTextStream>#include <QtEvents>#include <QClipboard>
#include <QApplication>
#include <QMimeData>MainWindow::MainWindow(QWidget *parent) :QMainWindow(parent),ui(new Ui::MainWindow)
{ui->setupUi(this);Form *login = new Form;login->show();connect(login, SIGNAL(loginsuccess()), this, SLOT(show()));pix = new QPixmap(":/picture/tool.png");this->setMask(pix->scaled(300,300).mask());ti = new QSystemTrayIcon;ti->setIcon(QIcon(":/picture/tool.png"));ti->setToolTip("小工具");ti->show();mn = new QMenu(this);action1 = new QAction;action2 = new QAction;action1->setText("显示");action2->setText("退出");mn->addAction(action1);mn->addAction(action2);ti->setContextMenu(mn);connect(action1,SIGNAL(triggered(bool)),this,SLOT(win_show()));connect(action2,SIGNAL(triggered(bool)),this,SLOT(win_eixt()));setWindowIcon(QIcon(":/picture/dog.png"));setWindowTitle("小狗");setAcceptDrops(true);//设置窗口接受拖拽文件操作//0. 构造所有的actionQAction *open_fileact = newAction("打开文件", ":/picture/open.png", "Ctrl+O");QAction *new_fileact = newAction("新建文件", ":/picture/new.png", "Ctrl+N");QAction *save_fileact = newAction("保存文件", ":/picture/save.png", "Ctrl+S");QAction *saveas_fileact = newAction("另存文件", ":/picture/saveas.png", "Ctrl+Alt+S");QAction *close_fileact = newAction("关闭文件", ":/picture/close.png", "Ctrl+D");QAction *copy_fileact = newAction("复制", ":/picture/copy.png", "Ctrl+C");QAction *cut_fileact = newAction("剪切", ":/picture/cut.png", "Ctrl+X");QAction *paste_fileact = newAction("粘贴", ":/picture/paste.png", "Ctrl+V");QAction *set_coloract = newAction("颜色", ":/picture/color.png", "Ctrl+l");QAction *set_fontact = newAction("字体", ":/picture/font.png", "Ctrl+f");QAction *aboutact = newAction("关于", ":/picture/about.png", "Ctrl+h");QAction *timeact = newAction("时间",":/picture/time.png","Ctl+t");QAction *calendaract = newAction("日历",":/picture/calendar.png","Ctrl+d");QAction *dogact = newAction("垃圾狗",":/picture/dog.png","Ctl+m");connect(open_fileact, SIGNAL(triggered(bool)), this, SLOT(open_file()));connect(new_fileact, SIGNAL(triggered(bool)), this, SLOT(new_file()));connect(save_fileact, SIGNAL(triggered(bool)), this, SLOT(save_file()));connect(saveas_fileact, SIGNAL(triggered(bool)), this, SLOT(saveas_file()));connect(close_fileact, SIGNAL(triggered(bool)), this, SLOT(close_file()));connect(copy_fileact, SIGNAL(triggered(bool)), this, SLOT(copy_file()));connect(cut_fileact, SIGNAL(triggered(bool)), this, SLOT(cut_file()));connect(paste_fileact, SIGNAL(triggered(bool)), this, SLOT(paste_file()));connect(set_coloract, SIGNAL(triggered(bool)), this, SLOT(set_color()));connect(set_fontact, SIGNAL(triggered(bool)), this, SLOT(set_font()));connect(aboutact, SIGNAL(triggered(bool)), this, SLOT(about()));connect(timeact,SIGNAL(triggered(bool)),this,SLOT(show_time()));connect(calendaract,SIGNAL(triggered(bool)),this,SLOT(show_calendar()));connect(dogact,SIGNAL(triggered(bool)),this,SLOT(dogshow()));//1. 添加菜单QMenu *fileMenu = menuBar()->addMenu("&文件"); //在菜单栏中添加一个file菜单fileMenu->addAction(open_fileact); //在file菜单中加一个选项(action)fileMenu->addAction(new_fileact);fileMenu->addAction(save_fileact);fileMenu->addAction(saveas_fileact);fileMenu->addAction(close_fileact);QMenu *fileMenu1 = menuBar()->addMenu("&编辑"); //在菜单栏中添加一edit菜单fileMenu1->addAction(copy_fileact); //在菜单中加一个选项(action)fileMenu1->addAction(cut_fileact);fileMenu1->addAction(paste_fileact);QMenu *fileMenu2 = menuBar()->addMenu("&设置"); //在菜单栏中添加一edit菜单fileMenu2->addAction(set_coloract); //在菜单中加一个选项(action)fileMenu2->addAction(set_fontact);QMenu *fileMenu3 = menuBar()->addMenu("&关于"); //在菜单栏中添加一edit菜单fileMenu3->addAction(aboutact); //在菜单中加一个选项(action)QMenu *tm1 = menuBar()->addMenu("&时间");tm1->addAction(timeact);QMenu *ca1 = menuBar()->addMenu("&日历");ca1->addAction(calendaract);QMenu *dog = menuBar()->addMenu("&垃圾狗");//2. 添加工具栏// QToolBar *fileToolBar = new QToolBar;// addToolBar(Qt::BottomToolBarArea, fileToolBar); //向左侧放一个工具栏QToolBar *fileToolBar = addToolBar("文件功能"); //默认位置添加一个工具栏fileToolBar->addAction(open_fileact);fileToolBar->addAction(new_fileact);fileToolBar->addAction(saveas_fileact);QToolBar *fileToolBar1 = addToolBar("复制粘贴");fileToolBar1->addAction(copy_fileact);fileToolBar1->addAction(cut_fileact);fileToolBar1->addAction(paste_fileact);QToolBar *fileToolBar2 = addToolBar("颜色字体");fileToolBar2->addAction(set_coloract);fileToolBar2->addAction(set_fontact);QToolBar *fileToolBar3 = addToolBar("时间日历");fileToolBar3->addAction(timeact);fileToolBar3->addAction(calendaract);QToolBar *fileToolBar4 = addToolBar("垃圾狗");fileToolBar4->addAction(dogact);//3. 添加中央窗口部件te = new QTextEdit;te->setMinimumSize(640, 480);setCentralWidget(te);connect(te, SIGNAL(textChanged()), this, SLOT(file_changed()));//4. 添加状态栏lb = new QLabel;QStatusBar *stbr = statusBar(); //得到状态栏stbr->addWidget(lb); //将标签放入状态栏}void MainWindow::win_show()
{this->showNormal();
}void MainWindow::win_eixt()
{ti->deleteLater();QApplication *win;win->exit();
}void MainWindow::dogshow()
{Formdog *dia;dia = new Formdog;dia->show();}
void MainWindow::show_time()
{Form1 *clok;clok = new Form1;clok->show();}
void MainWindow::show_calendar()
{calen = new QCalendarWidget;calen->show();}void MainWindow::open_file()
{qDebug() << "open";//1.提取文件名filename = QFileDialog::getOpenFileName();lb->setText(filename);//在状态栏显示一下//2.提取文件内容QFile file(filename);file.open(QIODevice::ReadOnly);QByteArray buf = file.readAll();file.close();//3.显示first_open = true;te->setText(QString(buf));
}void MainWindow::new_file()
{//1. 如果在打开开文件编辑,则先保存文件if(lb->text().contains('*')){if(QMessageBox::Save == QMessageBox::warning(this, "提示", "是否保存文件", QMessageBox::Cancel, QMessageBox::Save))save_file();}//2. 清屏filename.clear();lb->clear();te->clear();
}void MainWindow::save_file()
{//1. 只写的方式打开文件if(filename.isEmpty()) //新建{filename = QFileDialog::getSaveFileName();}QFile file(filename);file.open(QIODevice::WriteOnly);file.write(te->toPlainText().toStdString().c_str());//2. 写入te里的内容//file.write(te->toHtml().toStdString().c_str());//2. 写入te里的内容file.close();//清除*lb->setText(filename);
}void MainWindow::saveas_file()
{QFileDialog fileDialog;QString fileName = fileDialog.getSaveFileName(this,tr("Open File"),"/home",tr("Text File(*.txt)"));if(fileName == ""){return;}QFile file(fileName);if(!file.open(QIODevice::WriteOnly | QIODevice::Text)){QMessageBox::warning(this,tr("错误"),tr("打开文件失败"));return;}else{QTextStream textStream(&file);QString str = te->toPlainText();textStream<<str;QMessageBox::warning(this,tr("提示"),tr("保存文件成功"));Last_FileContent = str;Last_FileName = fileName;Flag_IsNew = 0;file.close();}
}
void MainWindow::close_file()
{te->close();}void MainWindow::copy_file()
{te->copy();
}void MainWindow::cut_file()
{te->cut();
}void MainWindow::paste_file()
{te->paste();
}void MainWindow::set_color()
{QColor color = QColorDialog::getColor();te->setTextColor(color);
}void MainWindow::set_font()
{bool ok;QFont font = QFontDialog::getFont(&ok);if(ok)te->setCurrentFont(font);
}void MainWindow::about()
{}QAction *MainWindow::newAction(QString text, QString Icon, QString shortcut ){QAction *act = new QAction(text); //构造一个actionact->setIcon(QIcon(Icon)); //设置图标act->setShortcut(QKeySequence(shortcut)); //设置快捷键return act;
}void MainWindow::file_changed()
{if(first_open){first_open = false;return;}if(!lb->text().contains('*')) //如果没有*lb->setText(lb->text()+'*');
}MainWindow::~MainWindow()
{}
3、效果图
五、main.cpp与.pro文件
1、main.cpp
#include "mainwindow.h"
#include <QApplication>int main(int argc, char *argv[])
{QApplication a(argc, argv);MainWindow w;//w.show();return a.exec();
}
2、qtproject.pro
# Project created by QtCreator 2022-09-23T17:42:24
#
#-------------------------------------------------QT += core gui sql multimedia
CONFIG += resources_big
greaterThan(QT_MAJOR_VERSION, 4): QT += widgetsTARGET = qtproject
TEMPLATE = app# The following define makes your compiler emit warnings if you use
# any feature of Qt which has been marked as deprecated (the exact warnings
# depend on your compiler). Please consult the documentation of the
# deprecated API in order to know how to port your code away from it.
DEFINES += QT_DEPRECATED_WARNINGS# You can also make your code fail to compile if you use deprecated APIs.
# In order to do so, uncomment the following line.
# You can also select to disable deprecated APIs only up to a certain version of Qt.
#DEFINES += QT_DISABLE_DEPRECATED_BEFORE=0x060000 # disables all the APIs deprecated before Qt 6.0.0SOURCES += \main.cpp \mainwindow.cpp \form.cpp \form1.cpp \formdog.cppHEADERS += \mainwindow.h \form.h \form1.h \formdog.hFORMS += \mainwindow.ui \form.ui \form1.ui \formdog.uiRESOURCES += \picture.qrc \mu.qrc
RC_ICON=desk.icon
六、总结
1、此次Qt项目使用了数据库、文件操作、字体、颜色、闹钟、日历以及桌面托盘的设置。
2、闹钟、垃圾狗等用另一个类来实现。