完善对话框功能
#include "widget.h"
#include "ui_widget.h"Widget::Widget(QWidget *parent) :QWidget(parent),ui(new Ui::Widget)
{ui->setupUi(this);// this->setWindowOpacity(0);
}
Widget::~Widget()
{delete ui;
}
//字体对应的槽
void Widget::on_fontBtn_clicked()
{//在该函数中调用系统的字体对话框选择字体bool ok;QFont f = QFontDialog::getFont(&ok,QFont("宋体,10,2,false"),this,"选择字体");if(ok){//ui->textEdit->setFont(f); //所有字体进行更改ui->textEdit->setCurrentFont(f);}else{QMessageBox::information(this,"错误","用户没有选中字体");}}//颜色的槽函数
void Widget::on_colorBtn_clicked()
{QColor c = QColorDialog::getColor(QColor(100,100,100),this,"选中颜色");if(c.isValid()){ui->textEdit->setTextColor(c);ui->textEdit->setTextBackgroundColor(c);}else{QMessageBox::information(this,"错误","用户没有选中颜色");}}
//打开文件按钮对应的槽函数
void Widget::on_openBtn_clicked()
{QString fileName = QFileDialog::getOpenFileName(this,"选择文件","./","所有文件(*.*) ;; 头文件(*.h);; 图片(*.png *.xpm *.jpg);;文本(*.txt)");qDebug()<<fileName;//实例化对象QFile file(fileName);//判断文件是否存在if(!file.exists()){QMessageBox::information(this, "提示", "文件不存在");return;}//打开文件if(!file.open(QIODevice::ReadWrite)){QMessageBox::information(this,"错误","文件打开失败");return;}//文件读写QByteArray msg = file.readAll();//关闭文件file.close();ui->textEdit->setText(QString::fromLocal8Bit(msg));}void Widget::on_saveBtn_clicked()
{QString fileName = QFileDialog::getOpenFileName(this,"选择文件","./","所有文件(*.*) ;; 头文件(*.h);; 图片(*.png *.xpm *.jpg);;文本(*.txt)");qDebug()<<fileName;//实例化对象QFile file(fileName);//打开文件if(!file.open(QIODevice::Append)){QMessageBox::information(this,"错误","文件打开失败");return;}//文件读写//QByteArray msg = file.readAll();QString msg = ui->textEdit->toPlainText();file.write(msg.toUtf8());//关闭文件file.close();}