C++ QT学习笔记

C++ QT学习笔记

信号和槽

cpp 复制代码
// static QMetaObject::Connection connect(const QObject *sender, PointerToMemberFunction signal, Functor functor);
connect(ui->actionQuit,&QAction::triggered,[=](){ this->close(); });

绘图

cpp 复制代码
QPainter painter(this);
QPixmap pix;
pix.load(":/res/PlayLevelSceneBg.png");
painter.drawPixmap(0,0,this->width(),this->height(),pix);

音效

cpp 复制代码
QSoundEffect *startSound=new QSoundEffect(this);
startSound->setSource(QUrl::fromLocalFile(":/res/TapButtonSound.wav"));
startSound->play();

菜单栏

cpp 复制代码
//创建菜单栏
QMenuBar *bar=menuBar();
setMenuBar(bar);
QMenu* startMenu=bar->addMenu("开始");
QAction* quitAction= startMenu->addAction("退出");

自定义信号

cpp 复制代码
#include <QMainWindow>
#include"playscene.h"

class ChooseLevelScene : public QMainWindow
{
    Q_OBJECT
public:
    explicit ChooseLevelScene(QWidget *parent = nullptr);
    void paintEvent(QPaintEvent *event);
    PlayScene* play=NULL;
signals:
    //写一个自定义信号,告诉主场景  点击了返回
    //只需要声明,不需要实现
    void chooseSceneBack();
};


connect(backBtn,&MyPushButton::clicked,[=](){
    //返回主场景
    backSound->play();
    QTimer::singleShot(500,this,[=](){
        emit this->chooseSceneBack();//发送信号
    });
});

chooseLevel=new ChooseLevelScene();
connect(chooseLevel,&ChooseLevelScene::chooseSceneBack,[=](){ //接收信号
    chooseLevel->setGeometry(this->geometry());
    chooseLevel->hide();
    this->show();
});

文字

cpp 复制代码
QLabel* levelLabel=new QLabel();
levelLabel->setParent(this);
levelLabel->setFixedSize(levelBtn->width(),levelBtn->height());
levelLabel->setText(QString::number(i+1));
levelLabel->move((i%4)*70+25,(i/4)*70+130);

//设置文字在Label中的对齐方式
levelLabel->setAlignment(Qt::AlignHCenter|Qt::AlignVCenter);
levelLabel->setAttribute(Qt::WA_TransparentForMouseEvents);

//设置字体
QLabel* label=new QLabel();
label->setParent(this);
QFont font;
font.setFamily("华文新魏");
font.setPointSize(20);
QString level_str=QString("Level:%1").arg(this->levelIndex);
label->setFont(font);
label->setText(level_str);
label->setGeometry(30,this->height()-60,120,50);

动画

cpp 复制代码
//动画对象
QPropertyAnimation* animation=new QPropertyAnimation(this,"geometry");
//动画时间
animation->setDuration(200);
//开始位置和结束位置
if(isUp){
    animation->setStartValue(QRect(this->x(),this->y()+10,this->width(),this->height()));
    animation->setEndValue(QRect(this->x(),this->y(),this->width(),this->height()));
}
else{
    animation->setStartValue(QRect(this->x(),this->y(),this->width(),this->height()));
    animation->setEndValue(QRect(this->x(),this->y()+10,this->width(),this->height()));
}
//设置弹跳曲线
animation->setEasingCurve(QEasingCurve::OutBounce);
animation->start();

按钮

cpp 复制代码
void MyPushButton::mousePressEvent(QMouseEvent *e){
    if(this->pressImgPath!=""){
        //切换图像
        QPixmap pix;
        bool res=pix.load(this->pressImgPath);
        if(!res){
            qDebug()<<"图片加载失败";
            return;
        }
        this->setFixedSize(pix.width(),pix.height());
        this->setStyleSheet("QPushButton{border:0px;}");
        this->setIcon(pix);
        this->setIconSize(QSize(pix.width(),pix.height()));
    }
    //让父类继续进行处理,相当于先拦截让函数先实现部分功能
    return QPushButton::mousePressEvent(e);
}

// 胜利过后,不可以点击按钮,其实也就是覆盖了父类按钮点击事件
void MyCoin::mousePressEvent(QMouseEvent *e){
    if(isAnimation||this->isWin){
        return;
    }
    return QPushButton::mousePressEvent(e);
}

计时器

cpp 复制代码
timer1=new QTimer(this);

connect(timer1,&QTimer::timeout,[=](){
    QPixmap pix;
    QString str=QString(":/res/Coin000%1.png").arg(this->min_N++);
    pix.load(str);
    this->setFixedSize(pix.width(),pix.height());
    this->setStyleSheet("QPushButton{border:0px;}");
    this->setIcon(pix);
    this->setIconSize(QSize(pix.width(),pix.height()));
    if(this->min_N>this->max_N){
        this->min_N=1;
        isAnimation=false;
        timer1->stop();
    }
});
if(this->flag){
    isAnimation=true;
    timer1->start(30);
    this->flag=false;
}

窗口偏移

解决方案:

cpp 复制代码
chooseLevel->setGeometry(this->geometry());
this->setGeometry(play->geometry());

案例

金币翻转小游戏案例

相关推荐
彭祥.1 小时前
Jetson边缘计算主板:Ubuntu 环境配置 CUDA 与 cudNN 推理环境 + OpenCV 与 C++ 进行目标分类
c++·opencv·分类
lzb_kkk2 小时前
【C++】C++四种类型转换操作符详解
开发语言·c++·windows·1024程序员节
程序员爱钓鱼3 小时前
【无标题】Go语言中的反射机制 — 元编程技巧与注意事项
开发语言·qt
胖大和尚4 小时前
clang 编译器怎么查看在编译过程中做了哪些优化
c++·clang
无畏烧风4 小时前
[Qt] visual studio code 安装 Qt插件
qt
钱彬 (Qian Bin)5 小时前
一文掌握Qt Quick数字图像处理项目开发(基于Qt 6.9 C++和QML,代码开源)
c++·开源·qml·qt quick·qt6.9·数字图像处理项目·美观界面
双叶8365 小时前
(C++)学生管理系统(正式版)(map数组的应用)(string应用)(引用)(文件储存的应用)(C++教学)(C++项目)
c语言·开发语言·数据结构·c++
源代码•宸5 小时前
C++高频知识点(二)
开发语言·c++·经验分享
jyan_敬言7 小时前
【C++】string类(二)相关接口介绍及其使用
android·开发语言·c++·青少年编程·visual studio
liulilittle7 小时前
SNIProxy 轻量级匿名CDN代理架构与实现
开发语言·网络·c++·网关·架构·cdn·通信