qt对话框

完善文本编辑器

#include "second.h"
#include "ui_second.h"

second::second(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::second)
{
    ui->setupUi(this);
    this->setWindowTitle("聊天室界面");//设置标题
    this->setWindowIcon(QIcon("C:\\Users\\86150\\Desktop\\icon\\1\\denglu_1.png"));//设置图标
    //创建logo
}
void second::jump_slot()
{
    this->show();
}
second::~second()
{
    delete ui;
}

void second::on_pushButton_clicked()
{
    bool ok;
    QFont f=QFontDialog::getFont(
                &ok,
                QFont("宋体",10,2,false),
                this);
    if(ok)
    {
        ui->textEdit->setCurrentFont(f);
    }else {
        QMessageBox::information(this,"错误","没有识别到你要的字体");
    }
}

void second::on_pushButton_2_clicked()
{
    QColor c=QColorDialog::getColor(
                Qt::black,
                this,
                "字体颜色"
                );
    if(c.isValid())
    {
        ui->textEdit->setTextColor(c);
    }else {
        QMessageBox::information(this,"错误","设置颜色失败");
    }
}

void second::on_pushButton_3_clicked()
{
   QString path=QFileDialog::getOpenFileName(
               this,
               "打开文件",
               "./",
               "所有文件(*.*);;文本文件(*.txt);;");
   QFile file(path);
   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 second::on_pushButton_4_clicked()
{
    QString path=QFileDialog::getSaveFileName(
                this,
                "保存文件",
                "./",
                "所有文件(*.*);;文本文件(*.txt);;");
    QFile file(path);
    if(!file.open(QIODevice::ReadWrite))
    {
        QMessageBox::information(this,"错误信息","不存在");
        return;
    }
    QString msg=ui->textEdit->toPlainText();
    file.write(msg.toLocal8Bit());
    file.close();
}

运行结果

qt对话框_第1张图片

 qt对话框_第2张图片

 

你可能感兴趣的:(命令模式)