字体对话框(QFontDialog)、颜色对话框(QColorDialog)、文件对话框(QFileDialog)
- #include "widget.h"
- #include "ui_widget.h"
-
- Widget::Widget(QWidget *parent)
- : QWidget(parent)
- , ui(new Ui::Widget)
- {
- ui->setupUi(this);
- }
-
- Widget::~Widget()
- {
- delete ui;
- }
-
-
- void Widget::on_text_clicked()
- {
- //调用QFontDialog类中的静态成员函数,getFont函数来调取系统提供的字体对话框
- bool ok;//用于接收用户是否选择字体
- QFont f=QFontDialog::getFont(&ok,//返回是否选中字体
- QFont("隶书",10,10,false),//初始字体,大小,宽,下划线
- this, //父组件
- "选择字体");//对话框标题
- //将选中的字体进行使用
- if(ok)
- {
- //选中字体,将字体设置到文本上
- //ui->textEdit->setFont(f);//全换
- ui->textEdit->setCurrentFont(f);
- }else
- {
- //没有选中
- QMessageBox ::information(this,"提示","您取消了选择字体");
-
- }
- }
-
- void Widget::on_color_clicked()
- {
- //可以使用QColorDialog类中的静态成员函数getcolor来调取颜色对话框
- QColor c=QColorDialog::getColor(QColor("pink"),//初始颜色
- this,//父组件
- "选择颜色");//对话框标题
-
- //对选中的颜色判断合法性
- if(c.isValid())
- {
- //颜色合法,直接用即可
- ui->textEdit->setTextColor(c); //字体颜色
- //ui->textEdit->setTextBackgroundColor(c); //背景色
- }
- else
- {
- //颜色不合法
- QMessageBox::information(this,"提示","您取消了选择颜色");
- }
-
- }
- //打开按钮
- void Widget::on_open_clicked()
- {
- ///调用QFileDialog类中的静态成员函数,getOpenFileName来获取文件路径
- QString filename=QFileDialog::getOpenFileName(this, //父组件
- "选择文件",
- "./", //起始路径
- "Image File(*.png *.jpg *.bmp);;Text File(*.txt);;All(*.*)");//过滤器
- //判断是否选中文件
- if(filename.isNull())
- {
- QMessageBox::information(this,"提示","您取消了选择文件");
- return ;
- }
- //输出文件路径s
- qDebug()<
- //1.实例化一个文件对象
- QFile file(filename); //使用获取到的文件路径,实例化一个文件对象,后期对文件的操作都是基于该对象
- //2.判断文件是否存在
- if(!file.exists())
- {
- return;
- }
- //3.打开文件
- if(!file.open(QFile::ReadWrite))
- {
- return;
- }
- //4.读取文件中的内容
- QByteArray msg =file.readAll();
- //将内容展示到ui界面
- ui->textEdit->setText(QString::fromLocal8Bit(msg));
-
- //获取文本编辑器中的内容
- //ui->textEdit->toPlainText();
-
- //5.关闭文件
- file.close();
- }
-
- void Widget::on_save_clicked()
- {
- ///调用QFileDialog类中的静态成员函数,getOpenFileName来获取文件路径
- QString filename=QFileDialog::getSaveFileName(this, //父组件
- "保存",
- "./", //起始路径
- "Image File(*.png *.jpg *.bmp);;Text File(*.txt);;All(*.*)");//过滤器
- //判断是否选中文件
- if(filename.isNull())
- {
- QMessageBox::information(this,"提示","您取消了选择文件");
- return ;
- }
- //输出文件路径s
- qDebug()<
- //1.实例化一个文件对象
- QFile file(filename); //使用获取到的文件路径,实例化一个文件对象,后期对文件的操作都是基于该对象
- //2.判断文件是否存在
- if(!file.exists())
- {
- return;
- }
- //3.打开文件
- if(!file.open(QFile::ReadWrite|QFile::Truncate))
- {
- return;
- }
- // //4.读取文件中的内容
- // QByteArray msg =file.readAll();
- // //将内容展示到ui界面
- // ui->textEdit->setText(QString::fromLocal8Bit(msg));
-
- //获取文本编辑器中的内容
- QString msg=ui->textEdit->toPlainText();
-
- file.write(msg.toUtf8());
-
- //5.关闭文件
- file.close();
- }
- #include "widget.h"
- #include "ui_widget.h"
-
- Widget::Widget(QWidget *parent)
- : QWidget(parent)
- , ui(new Ui::Widget)
- {
- ui->setupUi(this);
- }
-
- Widget::~Widget()
- {
- delete ui;
- }
-
-
- void Widget::on_pushButton_clicked()
- {
-
- }
-
- void Widget::on_pushButton_3_clicked()
- {
- //使用QMessageBox实例化一个对象
- QMessageBox box(QMessageBox::Information, // 图标
- "信息", //对话框标题
- "zzzz", //对话框提示信息
- QMessageBox::Yes|QMessageBox::No,//对话框的提供的按钮
- this); //父组件
- box.setDefaultButton (QMessageBox::No);//将no设置成默认按钮
- //box.setDetailedText("啦啦啦拉拉阿拉啦");//提示
-
- //执行对话框
- int ret = box.exec();
- //对用户点击的按钮进行判断
- if(ret == QMessageBox::Yes)
- qDebug() << "ok";
- else
- qDebug() << "下次一定";
- }
-
- void Widget::on_pushButton_2_clicked()
- {
- QMessageBox box(QMessageBox::Question,
- "问题对话框", //对话框标题
- "?", //对话框提示信息
- QMessageBox::Yes|QMessageBox::No,//对话框的提供的按钮
- this);
- //执行对话框
- int ret = box.exec();
- //对用户点击的按钮进行判断
- if(ret == QMessageBox::Yes)
- qDebug() << "ok";
- else
- qDebug() << "下次一定";
- }
- //警告函数按钮对应的槽函数
- void Widget::on_pushButton_4_clicked()
- {
- int ret=QMessageBox::warning(this,
- "警告",
- "zky",
- QMessageBox::Yes|QMessageBox::No,
- QMessageBox::NoButton);
- //对用户点击的按钮进行判断
- if(ret==QMessageBox::Yes)
- qDebug()<<"等着就等着,荤的还是素的";
- else
- qDebug()<<"对不起";
-
- }
-
- void Widget::on_pushButton_5_clicked()
- {
- int ret=QMessageBox::critical(this,
- "错误",
- "写错了",
- QMessageBox::Yes|QMessageBox::No,
- QMessageBox::Yes);
- //对用户点击的按钮进行判断
- if(ret==QMessageBox::Yes)
- qDebug()<<"知道了";
- else
- qDebug()<<"不管";
- }
事件处理(核心机制) (wasd移动label)
- #include "widget.h"
- #include "ui_widget.h"
-
- Widget::Widget(QWidget *parent)
- : QWidget(parent)
- , ui(new Ui::Widget)
- {
- ui->setupUi(this);
- }
-
- Widget::~Widget()
- {
- delete ui;
- }
-
- void Widget::keyPressEvent(QKeyEvent *event)
- {
- qDebug()<<"键盘按下了"<
text()<<"键值为"<key(); - switch(event->key())
- {
- case 'W':
- {
- if(ui->label->y()<= 0-ui->label->height())
- {
- ui->label->move(ui->label->x(), this->height());
- }
- ui->label->move(ui->label->x(),ui->label->y()-5);
- }
- break;
- case 'S':
- {
- if(ui->label->y()>= this->height()+ui->label->height())
- {
- ui->label->move(ui->label->x(),0-ui->label->height());
- }
- ui->label->move(ui->label->x(),ui->label->y()+5);
- }
- break;
- case 'D':
- {
- if(ui->label->x()>=this->width()+ui->label->width())
- {
- ui->label->move(0-ui->label->width(), ui->label->y());
- }
- ui->label->move(ui->label->x()+5,ui->label->y());
- }
- break;
- case 'A':
- {
- if(ui->label->x()<= 0-ui->label->width())
- {
- ui->label->move(this->width(),ui->label->y());
- }
- ui->label->move(ui->label->x()-5,ui->label->y());
- }
- break;
- }
- }
-
- void Widget::keyReleaseEvent(QKeyEvent *event)
- {
-
- }

-
相关阅读:
介绍一个数据血缘的项目 OpenLineage
Maven是什么? Maven的概念+作用
phpcms V9实战标签代码记录 - list
工具推荐:身边人都在用的那些知识管理软件!
2022.11.16 英语背诵
【工具篇】SQLite本地数据库在Unity3D的应用
路由跟踪命令 tracert 命令详解
MVC您正在查找的资源可能被移除
单链表详解
RAD Studio 11.2详解其务实改进(Delphi & C++ Builder)-Alexandria
-
原文地址:https://blog.csdn.net/zky050213/article/details/132629845