qt学习之2048

    最近学习qt的时候发现了一个很好玩的demo,学着做了一下。下面是一些收获。

1:Q_OBJECT 类中添加这个宏 才能使用slot 和signal 机制

2:复习connect connect(sender,singnal(),receiver,slot())

系统自带 CLOSE( )关闭widget

3:qreal类型 除非用户自己配置qreal为float,否则qreal将默认为double。

4:设置背景颜色的方法

    setAutoFillBackground(true);
    QPalette pal=palette();
    pal.setColor(QPalette::Background,QColor("#FAF8F0"));
    setPalette(pal);

5:样式表 QSS

titleLabel->setStyleSheet("color:#746D65");
highScoreLbl ->setStyleSheet("QLable{color:#FFFFEE;background:#BEAD9D}");

参考文章:https://blog.csdn.net/liang19890820/article/details/51691212 语法简介

https://blog.csdn.net/u014563989/article/details/54896213 例子说明

6:对齐方式

highScoreLbl ->setAlignment(Qt::AlignCenter);
 
 

居中,除此之外还有左对齐 右对齐 两端对齐

7:resize(w,h) 改变当前窗口的大小 

move(x,y) 移动窗口到某个位置

move((QApplication::desktop()->width()-width())/2,(QApplication::desktop()->height() - height())/4);

居中显示

8:

ratioW=width()/400.0f;
    ratioH=height()/510.0f;
    titleLabel->setGeometry(20 * ratioW, 40 * ratioH , 130 * ratioW, 50 * ratioH);
    tipsLabel->setGeometry(20 * ratioW, 100 * ratioH , 300 * ratioW, 20 * ratioH);
    gameWidget->setGeometry(18 * ratioW, 140 * ratioH, 365 * ratioW, 365 * ratioH);
    restartBtn->setGeometry(280 * ratioW, 90 * ratioH, 100 * ratioW, 30 * ratioH);
    highScoreLbl->setGeometry(300 * ratioW, 40 * ratioH, 80 * ratioW, 40 * ratioH);
    scoreLbl->setGeometry(210 * ratioW, 40 * ratioH, 80 * ratioW, 40 * ratioH);

这种设置子窗口 相对于父窗口大小的方法我觉得很巧妙。

9:读写文件

    QFile file("score.j");      
    if(file.open(QIODevice::ReadOnly))
    {
        file.read((char *)&highScore,sizeof(highScore));
        file.close();
    }

        QFile file("score.j");
        file.open(QIODevice::WriteOnly);
        file.write((char *)&highScore,sizeof(highScore));
        file.close();

10:设置字体格式

QFont font ( “Microsoft YaHei”, 10, 75); //第一个属性是字体(微软雅黑),第二个是大小,第三个是加粗(权重是75 
ui->label->setFont(font);

常见权重 
QFont::Light - 25
高亮 
QFont::Normal - 50
正常 
QFont::DemiBold - 63
半粗体 
QFont::Bold - 75
粗体 
QFont::Black - 87
黑体

11:几个事件

QWidget::mousePressEvent()
QWidget::mouseReleaseEvent()
QWidget::mouseDoubleClickEvent()

QWidget::mouseMoveEvent()

12:emit 发射 发射信号 来调用与之相关的槽函数

13

highScoreLbl =new QLabel(QString("BEST\n%1").arg(highScore),this);

%1 会被.arg()的highscore 代替。


大佬的代码:https://blog.csdn.net/taiyang1987912/article/details/45483719

界面做的很美观!!


猜你喜欢

转载自blog.csdn.net/Sunburst_Lf/article/details/80324720
今日推荐