qt-day3

#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_pushButton_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_pushButton_2_clicked()
{
    QColor c = QColorDialog::getColor(QColor(35,203,190)
                                      , this,
                                      "选择颜色");
    QColor d = QColorDialog::getColor(QColor(35,100,190)
                                      , this,
                                      "选择颜色");
    if(c.isValid())
    {
        //将该颜色添加到当前选中的文本
                ui->textEdit->setTextColor(c);       //设置字体颜色前景色


                 ui->textEdit->setTextBackgroundColor(d);   //设置背景色
    }else
    {
        QMessageBox::information(this,"错误","用户没有选择颜色");
    }
}
//打开文件
void Widget::on_pushButton_3_clicked()
{
    QString fileName = QFileDialog::getOpenFileName(
                   this,
                   "选择文件",
                   "./",
                   "all file(*.*);;Text(*.txt);;Image(*.png *.jpg *.gif)");
    //输出该路径
    qDebug()<textEdit->setText(msg);


    //关闭文件
    file.close();

}
//保存
void Widget::on_pushButton_4_clicked()
{
    QString pathName = QFileDialog::getSaveFileName(this, "保存文件", "./", "TEXT(*.txt)");

        //实例化文件对象
        QFile file(pathName);

        //打开文件
        file.open(QIODevice::WriteOnly | QIODevice::Truncate);

        //写入数据
        QString text = ui->textEdit->toPlainText();
        file.write(text.toLocal8Bit());

        //关闭文件
        file.close();

    }
//清空
void Widget::on_pushButton_5_clicked()  
{
    ui->textEdit->clear();  
}

你可能感兴趣的:(qt,开发语言)