qt作业day3

#include "txteditor.h"
#include "ui_txteditor.h"

Txteditor::Txteditor(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::Txteditor)
{
    ui->setupUi(this);
}

Txteditor::~Txteditor()
{
    delete ui;
}

void Txteditor::on_btn1_clicked()
{
    bool ok;
    QFont f = QFontDialog :: getFont(&ok,QFont("楷体",10,10,false),this,"字体选择");
    //将选中字体使用

    if(ok)
    {
//        ui->textEdit->setFont(f);
        ui->textEdit->setCurrentFont(f);
    }
    else
    {

    }
}

void Txteditor::on_colorBtn_clicked()
{
    QColor col = QColorDialog :: getColor(QColor("black"),this,"颜色选择");

    if(col.isValid())
    {
        ui->textEdit->setTextColor(col);
    }
    else
    {
        QMessageBox :: information(this,"提示","取消选择",QMessageBox :: Ok);
    }
}

void Txteditor::on_open_clicked()
{
     QString filename = QFileDialog :: getOpenFileName(this,tr("队长"),"./",tr("Image File(*.png *.jpg *bmp);;Text File(*.txt);;All(*.*)"));
     if(filename.isNull())
     {
         QMessageBox :: information(this,"信息","取消选择");
         return;
     }
     qDebug() << filename;

     //使用获取到的文件实例化文件对象

     QFile file(filename);

     //判断文件是否存在
     if(!file.exists())
     {
         return;
     }

     //打开文件
     if(!file.open(QFile :: ReadWrite))
     {
         return;
     }

    //读取文件内容
     QByteArray msg = file.readAll();

     //将内容展示到ui界面
     ui->textEdit->setText(QString :: fromLocal8Bit(msg));

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

}

void Txteditor::on_save_clicked()
{
    QString filename = QFileDialog :: getSaveFileName(this,"保存文件","./",tr("Image File(*.png *.jpg *bmp);;Text File(*.txt);;All(*.*)"));
    if(filename.isNull())
    {
        return;
    }

    //用filename实例化文件对象

    QFile file(filename);

//    if(!file.exists())
//    {
//        return;
//    }

    file.open(QFile :: ReadWrite);



    QString rdbuf = ui->textEdit->toPlainText();

    file.write(rdbuf.toUtf8());
    file.close();
}

运行效果:

qt作业day3_第1张图片

qt作业day3_第2张图片

qt作业day3_第3张图片

qt作业day3_第4张图片

#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() << "按下" << "\t" << event->text();

    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() - 10);
            }
            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() - 10,ui->label->y());
            }
            break;
        case 'S':
            if(ui->label->y() >= this->height())
            {
                ui->label->move(ui->label->x(),0 - ui->label->height());
            }
            ui->label->move(ui->label->x(),ui->label->y() + 10);
            break;
        case 'D':
            if(ui->label->x() >= this->width())
            {
                ui->label->move(0 - ui->label->width(),ui->label->y());
            }
            ui->label->move(ui->label->x() + 10,ui->label->y());
            break;
    }

}

void Widget::keyReleaseEvent(QKeyEvent *event)
{
    qDebug() << "释放" << event->text();
}

运行效果

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