• C++ QT学习笔记


    C++ QT学习笔记

    信号和槽

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

    绘图

    QPainter painter(this);
    QPixmap pix;
    pix.load(":/res/PlayLevelSceneBg.png");
    painter.drawPixmap(0,0,this->width(),this->height(),pix);
    
    • 1
    • 2
    • 3
    • 4

    音效

    QSoundEffect *startSound=new QSoundEffect(this);
    startSound->setSource(QUrl::fromLocalFile(":/res/TapButtonSound.wav"));
    startSound->play();
    
    • 1
    • 2
    • 3

    菜单栏

    //创建菜单栏
    QMenuBar *bar=menuBar();
    setMenuBar(bar);
    QMenu* startMenu=bar->addMenu("开始");
    QAction* quitAction= startMenu->addAction("退出");
    
    • 1
    • 2
    • 3
    • 4
    • 5

    自定义信号

    #include 
    #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();
    });
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31

    文字

    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);
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20

    动画

    //动画对象
    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();
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16

    按钮

    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);
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25

    计时器

    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;
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21

    窗口偏移

    解决方案:

    chooseLevel->setGeometry(this->geometry());
    this->setGeometry(play->geometry());
    
    • 1
    • 2

    案例

    金币翻转小游戏案例

  • 相关阅读:
    6.【opencv鼠标回调事件】
    Centos下Ambari2.7.5的编译和安装
    功能实现_用户认证_基于token_实现携带token访问购物车页面
    JNDI注入的理解、JDK给出的修复
    Python中prettytable库
    java 抽象类与接口——接口
    SpringCloud微服务实战——搭建企业级开发框架(五十):集成移动端推送功能的系统通知公告数据库设计
    【go.mod file not found in current directory or any parent directory】
    【无标题】
    Mongodb操作基础 分片
  • 原文地址:https://blog.csdn.net/qq_45041871/article/details/133064909