个人日程管理系统的设计

2015/07/06 个人项目

本项目主要是基于C++界面开发库QT平台设计一个日程管理系统,主要实现的功能有登陆功能、注册功能、写日记功能(包括添加、修改和删除功能)、日程管理提醒功能(包括添加、修改、删除、弹窗和播放音乐提醒功能)、托盘和开机启动功能。主要涉及到的知识包括C++、QT的使用和SQL数据库的操作。

开机启动

主要原理是修改Windows的注册表来实现的,Qt的QSettings提供了访问ini,注册表的功能。

//读取注册表的信息,让程序自启动
void MainWindow::Vistregedit(bool isAutoRun)
{
	QSettings *reg=new QSettings("HKEY_LOCAL_MACHINE//SOFTWARE//Microsoft""//Windows//CurrentVersion//Run",
	QSettings::NativeFormat);

	//开机自动运行
	if (isAutoRun){
		reg->setValue("app",QApplication::applicationFilePath());
	}else{
		reg->setValue("app","");
	}
}

设置开机自启动,主窗体隐藏:

if(argc>1 && (argv[1]==(char *)"//min")){
	w.hide();
}
else{
	w.showNormal();
}

托盘实现

托盘实现也比较简单,QT有提供例子,修改以后加入项目程序中就可以使用:

class myclass: public QWidget
{
public:
    myclass();
private:
    QSystemTrayIcon *trayIcon;
    QAction *minimizeAction;
    QAction *restoreAction;
    QAction *quitAction;
    QMenu   *trayIconMenu;
 
private slots:
    void trayiconActivated(QSystemTrayIcon::ActivationReason reason);
 
};
 
myclass::myclass()
{
    //创建托盘图标
    QIcon icon = QIcon(":/logo.png");
    trayIcon = new QSystemTrayIcon(this);
    trayIcon->setIcon(icon);
    trayIcon->setToolTip(tr("子曰USay"));
    QString titlec=tr("子曰USay");
    QString textc=tr("子曰USay:给你神一般的体验");
    trayIcon->show();
 
    //弹出气泡提示
    trayIcon->showMessage(titlec,textc,QSystemTrayIcon::Information,5000);
 
    //添加单/双击鼠标相应
    connect(trayIcon,SIGNAL(activated(QSystemTrayIcon::ActivationReason)),
            this,SLOT(trayiconActivated(QSystemTrayIcon::ActivationReason)));
 
    //创建监听行为
    minimizeAction = new QAction(tr("最小化 (&I)"), this);
    connect(minimizeAction, SIGNAL(triggered()), this, SLOT(hide()));
    restoreAction = new QAction(tr("还原 (&R)"), this);
    connect(restoreAction, SIGNAL(triggered()), this, SLOT(showNormal()));
    quitAction = new QAction(tr("退出 (&Q)"), this);
    connect(quitAction, SIGNAL(triggered()), qApp, SLOT(quit()));
 
    //创建右键弹出菜单
    trayIconMenu = new QMenu(this);
    trayIconMenu->addAction(minimizeAction);
    trayIconMenu->addAction(restoreAction);
    trayIconMenu->addSeparator();
    trayIconMenu->addAction(quitAction);
    trayIcon->setContextMenu(trayIconMenu);
}
 
void myclass::trayiconActivated(QSystemTrayIcon::ActivationReason reason)
{
    switch (reason)
    {
    case QSystemTrayIcon::Trigger:
        //单击托盘图标
    case QSystemTrayIcon::DoubleClick:
        //双击托盘图标
        this->showNormal();
        this->raise();
        break;
    default:
        break;
    }
}

登陆和注册

登陆和注册主要SQL数据库的查询和插入,在本项目中使用的是SQL server 2005数据库管理系统,在数据库管理系统中建立登陆表loginTable,包含了两个字段name和password。

登陆功能就是获取用户名编辑框和密码编辑框的内容,读取数据库,查询表格中项,是否有匹配的,如果有则登陆成功,弹出主窗口,否则提示登录失败。

void PSMSystem::LoginButton(){
	ZhangHao=ui.lineEdit_ZhangHao->text();
	Password=ui.lineEdit_Password->text();
	QSqlQuery query;                                        //用于执行SQL语言            
	query.exec("select * from LoginTable");
	bool opensucc=false;
	while(query.next()){    
		if(query.value(0).toString()==ZhangHao&&query.value(1).toString()==Password){
			mainwdow = new MainWindow(NULL,ZhangHao);      //创建主对话框
			mainwdow->show();
			opensucc=true;
			hide();                                        //关闭当前对话框
			break;
		}
	}   
	if(opensucc==false){
		QMessageBox::warning(0, qApp->tr("Warning!"),  
        "User name or password error, login failed!", QMessageBox::Cancel); 
	}
}

注册窗口主要是获取编辑框数据,再把数据插入数据库中:

主窗口

在登陆界面登陆成功以后,关闭登陆界面,同时弹出主窗口,要把用户名传递给主窗口。主窗口包括两部分,也就是日记管理和个人日程管理。

个人日记管理

主要是添加、修改、删除功能。在日记管理中单独使用了一张数据库表DiaryTable,字段包括:

添加就是获取界面中的数据,按照一定的格式插入数据库中:

添加完成以后,点击刷新按钮,从数据中读取DiaryTable表中的所有行,在Qtableview窗口部件中显示结果:

在Qtableview窗口部件中显示结果中选中某行,可以对该行的数据进行修改,修改完成以后重新更新数据库:

void MainWindow::BtnRJmodify(){                                  //修改日记按钮
	int row = ui->RJtableView->currentIndex().row();
	QAbstractItemModel *modessl = ui->RJtableView->model();
	QModelIndex indextemp = modessl->index(row,2);               //遍历行的所有列
	QString str1 = modessl->data(indextemp).toString();
	rjModify = new RJModify(NULL,ZhanHao,str1);
	rjModify->show();
}

同时也可以在Qtableview窗口部件中显示结果中选中某行,可以删除该行的数据,重新更新数据库。

void MainWindow::BtnRJDelete(){                                  //删除日记按钮
	int row = ui->RJtableView->currentIndex().row();
	QAbstractItemModel *modessl = ui->RJtableView->model();
	QModelIndex indextemp = modessl->index(row,2);               //遍历行的所有列
	QString str1 = modessl->data(indextemp).toString();
	QSqlQuery query;                                             //用于执行SQL语言            
	query.exec("delete from DiaryTable where title="+tr("'")+str1+tr("'"));
}

个人日程管理

点击添加按钮可以添加日程提醒事件,包括对事件的描述,设置事件的提醒时间,保存即可写到数据库中,每次开机启动,软件都会读取数据库中的设定时间来进行定时提醒:

日程管理数据也是通过Qtableview窗体部件显示,功能也包括添加、修改、删除。

个人日程提醒

设置时间以后,管理系统会根据所设置的时间来进行提醒功能。主要原理是设置定时器,每隔一段时间进行执行提醒代码:

QTimer *timer = new QTimer(this);
connect(timer,SIGNAL(timeout()),this,SLOT(SetClocktime()));
timer->start(5000);

void MainWindow::SetClocktime(){                                 //日程提醒
	QDateTime current_date_time = QDateTime::currentDateTime();
	QString current_date = current_date_time.toString("yyyy-MM-dd hh:mm:ss");
	QString year=current_date.mid(0,4);
	QString mouth=current_date.mid(5,2);
	QString date=current_date.mid(8,2);
	QString hour=current_date.mid(11,2);
	QString minute=current_date.mid(14,2);
	for(int i=0;i<5;i++){
		if(remindtime[i].m==true){
			if(remindtime[i].Hour==hour&&remindtime[i].Minute==minute){
				QMessageBox::information(this,"TIME",tr("Now the time is ")+hour+tr(":")+minute);
				remindtime[i].m=false;
			}
		}
	}
}

弹出窗口显示提醒:

点击确认以后还要删除相关的数据库数据。

同时播放相关音乐文件,实现原理是:

dialog2::dialog2(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::dialog2),sound(":/sound/duckP.wav")
{
    ui->setupUi(this);
    ui->groupBox2->hide();
    ui->cancleButton->hide();
    layout()->setSizeConstraint(QLayout::SetFixedSize);
    connect(ui->okButton,SIGNAL(clicked()),this,SLOT(ok()));
    connect(ui->cancleButton,SIGNAL(clicked()),this,SLOT(back()));
//    QSound sound(":/sound/duckP.wav");
}

dialog2::~dialog2()
{
    delete ui;
}

void dialog2::ok(){
    ui->groupBox2->setVisible(true);
    ui->cancleButton->setVisible(true);

    sound.play();
}

Search

    Post Directory