记录开发
12. 文件
1. 实现功能
点击文件选取按钮,实现文件的选择及打印;
用 Lambda 表达式来实现按钮的连接,不是太难
connect(ui->pushButton,&QPushButton::clicked,[=](){
QFileDialog::getOpenFileName(this,"打开文件","C:\\Users\\zhangtao\\Desktop");
//记住路径要用双斜线或者反斜线;
}:
但打开时什么也不会显示,现在需要将路径显示在上方框中,则将 Open 的值返回到一个 QString 对象中;
connect(ui->pushButton,&QPushButton::clicked,[=](){
QString path = QFileDialog::getOpenFileName(this,"打开文件","C:\\Users\\zhangtao\\Desktop");
//记住路径要用双斜线或者反斜线;
//将路径放入到lineEdit中
ui->lineEdit->setText(path);
};
接下来读取文件并显示在下方大文本框中:
//读取内容 放入到 textEdit中
// QFile默认支持的格式是 utf-8
QFile file(path); //参数就是读取文件的路径
//设置打开方式
file.open(QIODevice::ReadOnly);
然后放入文本框:
QByteArray array = file.readAll();
//将读取到的数据 放入textEdit中
ui->textEdit->setText(array); //这才是放入操作;
//ui->textEdit->setText( codec->toUnicode(array) );
//默认支持的格式为 UTF-8
//对文件对象进行关闭
file.close();
文件打开的默认格式为 UTF-8,格式的更改需要用到 QTextCodeC 类;
//编码格式类
QTextCodec * codec = QTextCodec::codecForName("gbk");
ui->textEdit->setText(codec->toUnicode(array)); //用 codec 做一个类型转换;
按行读文件:
QByteArray array;
while( !file.atEnd())
{
array += file.readLine(); //按行读
}
以追加的方式写入文件
//进行写文件
file.open(QIODevice::Append); //用追加方式进行写
file.write("啊啊啊啊啊");
2. 读取文件信息
文件信息存储的类为 QFileInfo,查看帮助文档可知,其含有较丰富的函数库,常用的有后缀名,创建日期,作者,文件名,文件路径等;
读取方法
QFileInfo info(path);
qDebug() << "大小:" << info.size() << " 后缀名:" << info.suffix() << " 文件名称:"<<info.fileName() << " 文件路径:"<< info.filePath();
qDebug() << "创建日期:" << info.created().toString("yyyy/MM/dd hh:mm:ss"); //返回的是 QDateTime 类型,且可以格式化;
qDebug() << "最后修改日期:"<<info.lastModified().toString("yyyy-MM-dd hh:mm:ss");
13. 翻硬币小游戏
1. 菜单栏以及背景图
//设置窗口大小;
setFixedSize(320, 588);
//设置图标;
setWindowIcon(QIcon(":/res/Coin0001.png"));
//设置标题;
setWindowTitle("CoinFlip");
//退出操作;
connect(ui->quit, &QAction::triggered, [=](){
this->close();
});
void MainScene::paintEvent(QPaintEvent *)
{
QPainter painter(this);
QPixmap pix;
pix.load(":/res/PlayLevelSceneBg.png");
painter.drawPixmap(0, 0, this->width(), this->height(), pix); // 平铺拉伸;
pix.load(":/res/Title.png");
pix = pix.scaled(pix.width() * 0.5, pix.height() * 0.5);
painter.drawPixmap(10, 30, pix);
}
2. 创建新的按钮类
MyPushButton.h
class MyPushButton : public QPushButton
{
Q_OBJECT
public:
//explicit MyPushButton(QWidget *parent = nullptr);
MyPushButton(QString normalImg, QString pressImg = "");
QString normalImgPath;
QString pressImgPath;
//弹跳特效;
void zoom_down();
void zoom_up();
signals:
};
MyPushButton.cpp
MyPushButton::MyPushButton(QString normalImg, QString pressImg) //若声明里有默认值,则定义里不需要,反之亦然;
{
this->normalImgPath = normalImg;
this->pressImgPath = pressImg;
QPixmap pix;
bool ret = pix.load(normalImg);
if(!ret) {
qDebug() << "Error";
return;
}
this->setFixedSize(pix.width(), pix.height());
this->setStyleSheet("QPushButton{border:0px}"); //边框为 0 像素;
this->setIcon(pix);
this->setIconSize(QSize(pix.width(), pix.height()));
}
void MyPushButton::zoom_down() //向下弹跳动画;
{
QPropertyAnimation *animation = new QPropertyAnimation(this, "geometry");
animation->setDuration(200);
animation->setStartValue(QRect(this->x(), this->y(), this->width(), this->height()));
animation->setEndValue(QRect(this->x(), this->y() + 10, this->width(), this->height()));
animation->setEasingCurve(QEasingCurve::OutCurve);
animation->start();
}
void MyPushButton::zoom_up() //向上弹跳动画;
{
QPropertyAnimation *animation = new QPropertyAnimation(this, "geometry");
animation->setDuration(200);
animation->setStartValue(QRect(this->x(), this->y() + 10, this->width(), this->height()));
animation->setEndValue(QRect(this->x(), this->y(), this->width(), this->height()));
animation->setEasingCurve(QEasingCurve::OutCurve);
animation->start();
}
明天应该就能全部看完了,可以写出自己的第一个游戏了;