Qt记事本实例源码学习

Qt

实现的功能

  • 打开文件模板
  • 保存文件
  • 获取系统当前时间
  • 设置文本字体
  • 设置文本颜色
  • 使用默认浏览器打开一个网址
  • 打开一个Dialog
  • 关闭事件

  • 添加程序图标

  • 添加菜单图标
  • 添加TextEdit背景图

打开文件模板

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
void MainWindow::openFileSlot()
{
//得到文件名
QString openFileName=QFileDialog::getOpenFileName(this,"打开文件",QDir::currentPath());
// qDebug()<<"file name is"<<openFileName; //调试用
//检测是否选中了文件
if(openFileName.isEmpty())
{
QMessageBox::information(this,"警告!","请选择一个文件!");
return;
}
//利用这个openFileName打开文件
QFile *file = new QFile;//生成一个文件对象
file->setFileName(openFileName);
bool ok = file->open(QIODevice::ReadOnly);//打开经典语句
if(ok)//如果打开成功
{
QTextStream in(file);
ui->textEdit->setText(in.readAll());//读并显示
file->close();
delete file; //释放内存
/*str = path.section('/', -1); // str == "myapp"*/
QString name = openFileName.section('/',-1);//字符串分节操作
this->setWindowTitle(name);
}
else//失败
{
// QMessageBox::information(this,"提示","打开文件失败"+file->errorString());
// return;
}
}

保存文件

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
void MainWindow::saveFileSlot()
{
//打开保存文件对话框,获取保存文件路径
QString saveFileName = QFileDialog::getSaveFileName(this, "保存文件",
QDir::currentPath(),
"文档 (*.txt *.doc *.bat);;Text files (*.txt)");
//检测路径是否正确
if(saveFileName.isEmpty())
{
QMessageBox::information(this,"提示","文件名不能为空!");
return ;
}
//file类似一个水箱,QTextStream类似一个管道
QFile *file = new QFile;//生成一个文件对象
file->setFileName(saveFileName);
bool ok = file->open(QIODevice::WriteOnly);//打开经典语句
if(ok)//如果打开成功
{
QTextStream out(file);
out<<ui->textEdit->toPlainText();//读并显示
file->close();
delete file; //关闭
}
else//失败
{
QMessageBox::information(this,"提示","保存文件失败"+file->errorString());
return;
}
}

获取系统当前时间

1
2
3
4
5
6
7
8
9
void MainWindow::setDataTimeSlot()
{
//获取当前时间
QDateTime current= QDateTime::currentDateTime();
QString time = current.toString("yyyy-M-dd hh:mm:ss");更改格式
//添加到文本的末尾
ui->textEdit->append(time);
}

设置文本字体

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
void MainWindow::setFontSlot()
{
/*
bool ok;
QFont font = QFontDialog::getFont(
&ok, QFont("Helvetica [Cronyx]", 10), this);
if (ok) {
// 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
}
*/
bool ok;
QFont font = QFontDialog::getFont(&ok,this);
if(ok)
{
ui->textEdit->setFont(font);
}
else
{
}
}

设置文本颜色

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
void MainWindow::setColorSlot()
{
/* const QColorDialog::ColorDialogOptions options = QFlag(colorDialogOptionsWidget->value());
const QColor color = QColorDialog::getColor(Qt::green, this, "Select Color", options);
if (color.isValid()) {
colorLabel->setText(color.name());
colorLabel->setPalette(QPalette(color));
colorLabel->setAutoFillBackground(true);
}*/
//调用颜色对话框
QColor color = QColorDialog::getColor(Qt::red,this,"选择颜色");
if (color.isValid())
{
ui->textEdit->setTextColor(color);//设置文本颜色
}
}

使用默认浏览器打开一个网址

1
2
3
4
5
6
7
void MainWindow::aboutWebsiteSlot()
{
/*QDesktopServices::openUrl(
* QUrl("file:///C:/Documents and Settings/All Users/Desktop", QUrl::TolerantMode));*/
QDesktopServices::openUrl(QUrl("https://www.baidu.com/"));
}

打开一个Dialog

1
2
3
4
5
void MainWindow::openAboutDialog()
{
about *aboutDialog = new about;
aboutDialog->show();//非模态//aboutDialog.exec();
}

关闭事件

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
void MainWindow::closeEvent(QCloseEvent *event)
{
/*void MainWindow::closeEvent(QCloseEvent *event)
{
if (maybeSave()) {
writeSettings();
event->accept();
} else {
event->ignore();
}
}*/
//如果文本发生改变
if(ui->textEdit->document()->isModified())
{
QMessageBox msgBox;
msgBox.setText("文件已经变更:");
msgBox.setInformativeText("你想保存文件吗?");
msgBox.setStandardButtons(QMessageBox::Save | QMessageBox::Discard | QMessageBox::Cancel);
msgBox.setDefaultButton(QMessageBox::Save);
int ret = msgBox.exec();
switch (ret) {
case QMessageBox::Save:
// Save was clicked
this->saveFileSlot();
event->ignore();
break;
case QMessageBox::Discard:
// Don't Save was clicked
event->accept();
break;
case QMessageBox::Cancel:
// Cancel was clicked
event->ignore();
break;
default:
// should never be reached
event->ignore();
break;
}
}
else
{
return;
}
}

添加程序图标

在.pro文件加入

RC_ICONS = myappico.ico

也可在帮助中查找setting the application icon ,有一个添加.rc文件的方法

添加菜单图标

右键文件列表最上的工程名->选择添加新文件->点击Qt->点击Qt Resource File->点击右下角choose->输入名字(如res)->下一步到完成

此时.pro文件就会有res文件加入,文件列表多出Resource文件夹

点击res.qrc->点击Add prefix添加前缀->之后点击Add Files添加图片

进入ui界面,在Action Editor 右键任意Action选Edit,选择图标

工具栏图标可直接拖拽

添加TextEdit背景图

ui界面

右键TextEdit部件

改变样式表->添加资源->border-image

点一下刷新按钮,选择图片

当前网速较慢或者你使用的浏览器不支持博客特定功能,请尝试刷新或换用Chrome、Firefox等现代浏览器