qt-------会话管理

#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_zibtn_clicked()
{
    bool ok = false;//定义是否选中字体

    //字体对话框
    QFont f = QFontDialog::getFont(&ok,//是否选中字体
                                   QFont("幼圆",20,10,false),//初始字体
                                   this,    //父组件
                                   "选择字体"); //标题

    if(ok)
    {
//        ui->textEdit->setFont(f);//设置所有字体
        ui->textEdit->setCurrentFont(f);
    }
    else
    {
        QMessageBox::information(this,"提示","没有选择字体");
    }


}

//颜色
void Widget::on_colbtn_clicked()
{


//    static QColor getColor(const QColor &initial = Qt::white,
//                           QWidget *parent = nullptr,
//                           const QString &title = QString(),
//                           ColorDialogOptions options = ColorDialogOptions());

    QColor c = QColorDialog::getColor( QColor(190,100,170,0.8),this,"颜色选择");

    if(c.isValid())
    {
//        ui->textEdit->setTextColor(c);    //设置字体颜色
        ui->textEdit->setTextBackgroundColor(c);        //设置字体底纹
    }
    else
    {
        QMessageBox::information(this,"提示","没有选择颜色");
    }

}

//文件
void Widget::on_openbtn_clicked()
{

//    static QString getOpenFileName(QWidget *parent = nullptr,
//                                   const QString &caption = QString(),
//                                   const QString &dir = QString(),
//                                   const QString &filter = QString(),
//                                   QString *selectedFilter = nullptr,//过滤器
//                                   Options options = Options());

       QString s = QFileDialog::getOpenFileName(this,"文件","./","all(*.*);;Images (*.png *.jpg);;file (*.txt *.cpp)");


       QFile f(s);

       if(!f.open(QIODevice::ReadOnly|QIODevice::Text))
       {
           return;
       }

       while(!f.atEnd())
       {
           QByteArray all = f.readAll();
            ui->textEdit->setText(all);
       }

       f.close();


}

void Widget::on_cunbtn_clicked()
{
    QString s = QFileDialog::getOpenFileName(this,"文件","./","all(*.*);;Images (*.png *.jpg);;file (*.txt *.cpp)");


    QFile f(s);

    if(!f.open(QIODevice::WriteOnly|QIODevice::Text|QIODevice::Truncate))
    {
        return;
    }
       QString str = ui->textEdit->toPlainText();
    f.write(qPrintable(str));
    f.close();

    this->close();
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值