qt day 3

1.完成自定义的记事本文件的保存功能

cpp 复制代码
-------------------------------------------------------------------------
widget.cpp
-------------------------------------------------------------------------
#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_fontbtn_clicked()
{
    //调用QFontDialog中的成员函数getFont函数来获取系统提供的字体对话框
    bool ok;    //接收用户是否选择字体
    QFont font = QFontDialog::getFont(&ok,QFont("隶书",14,14,0),this,"选择字体");
    if(ok == 1) //选中了字体
    {
        //将字体设置到文本上
        //ui->textEdit->setFont(font);
        //将字体设置到选中文本上
        ui->textEdit->setCurrentFont(font);
    }
    else        //未选中字体
    {
        QMessageBox::information(this,"提示","未选择字体");
    }
}
void Widget::on_colbtn_clicked()
{
    QColor col = QColorDialog::getColor(QColor("black"),this,"颜色选择");
    if(col.isValid())//颜色合法
    {
        //ui->textEdit->setTextColor(col);
        ui->textEdit->setTextBackgroundColor(col);
    }
    else    //颜色不合法
    {
        QMessageBox::information(this,"提示","颜色不合法");
    }
}
void Widget::on_openbtn_clicked()
{
    QString fname = QFileDialog::getOpenFileName(this,
                                                 "选择文件",
                                                 "./",
                                                 "Image File(*.png *.jpg *.bmp);;Text File(*.txt);;All(*.*)");
    if(fname.isNull())
    {
        QMessageBox::information(this,"文件选择","取消选择");
    }
    else
    {
        QFile rf(fname);
        if(!rf.exists())
        {
            return;
        }
        if(!rf.open(QFile::ReadWrite))
        {
            return;
        }
        QByteArray msg = rf.readAll();
        ui->textEdit->setText(msg);
        rf.close();
    }
}
void Widget::on_savebtn_clicked()
{
    QString fname = QFileDialog::getSaveFileName(this,"保存","./","Text File(*.txt);;Image File(*.jpg *.png *.bmp);;All File(*.*)");
    if(fname.isNull())
    {
        QMessageBox::information(this,"文件选择","取消选择");
    }
    else
    {
        QFile wf(fname);
        if(!wf.open(QFile::WriteOnly))
        {
            return;
        }
        QString buf;
        buf = ui->textEdit->toPlainText();
        qDebug() << buf;
        wf.write(buf.toUtf8());
    }
}

2.使用键盘事件实现图片的上下左右移动

cpp 复制代码
-------------------------------------------------------------------------
widget.cpp
-------------------------------------------------------------------------
#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() << "键盘按下" << event->text() << "键值为 : " << event->key();

    switch(event->key())
    {
    case 'W':
        ui->label->move(ui->label->x(),ui->label->y()-1);
        if(ui->label->y() <= 0-ui->label->height())
        {
            ui->label->move(ui->label->x(),this->height());
        }
        break;
    case 'S':
        ui->label->move(ui->label->x(),ui->label->y()+1);
        if(ui->label->y() >= this->height())
        {
            ui->label->move(ui->label->x(),0-ui->label->height());
        }
        break;
    case 'A':
        ui->label->move(ui->label->x()-1,ui->label->y());
        if(ui->label->x() <= 0-ui->label->width())
        {
            ui->label->move(this->width(),ui->label->y());
        }
        break;
    case 'D':
        ui->label->move(ui->label->x()+1,ui->label->y());
        if(ui->label->x() >= this->width())
        {
            ui->label->move(0-ui->label->width(),ui->label->y());
        }
    }
}
相关推荐
不想写代码的星星12 小时前
std::function 详解:用法、原理与现代 C++ 最佳实践
c++
Felix_One1 天前
Qt 串口通信避坑指南:QSerialPort 的 5 个常见问题
qt
樱木Plus2 天前
深拷贝(Deep Copy)和浅拷贝(Shallow Copy)
c++
blasit4 天前
笔记:Qt C++建立子线程做一个socket TCP常连接通信
c++·qt·tcp/ip
肆忆_5 天前
# 用 5 个问题学懂 C++ 虚函数(入门级)
c++
不想写代码的星星5 天前
虚函数表:C++ 多态背后的那个男人
c++
端平入洛7 天前
delete又未完全delete
c++
端平入洛8 天前
auto有时不auto
c++
哇哈哈20219 天前
信号量和信号
linux·c++
多恩Stone9 天前
【C++入门扫盲1】C++ 与 Python:类型、编译器/解释器与 CPU 的关系
开发语言·c++·人工智能·python·算法·3d·aigc