QT += core gui widgets //引入需要用到的库

qDebug()<<"t="<<t<<QTime::currentTime();//在控制台输出当前时间

label->setStyleSheet("background:red; border-radius:25px");//设置样式表

label->setFont(QFont("宋体",20));//设置标签的字体及大小

ctrl + i 格式化代码

F4在cpp文件和h文件之间切换

signal函数和slot函数都是void类型的


QT入门_public

mainwindow.h

#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include "QTimer"
#include "QLabel"
#include <QWidget>
class MainWindow : public QWidget
{
    Q_OBJECT
public:
    explicit MainWindow(QWidget *parent = 0);
signals:
public slots:
    void slotTimeout();
private:
    QTimer timer;//定义全局变量
    QLabel *label;
    int t = 0;
};
#endif // MAINWINDOW_H

mainwindow.cpp

#include "mainwindow.h"
#include "QString"
#include "QFont"
MainWindow::MainWindow(QWidget *parent) :
    QWidget(parent)
{
    this->setGeometry(200,200,600,400);
    label = new QLabel(this);//将标签添加到当前的mainwindow中
    label->setGeometry(10,10,230,20);//设置标签的位置及大小
    label->setFont(QFont("宋体",20));//设置标签的字体及大小
//    connect(&timer,SIGNAL(timeout()),this,SLOT(close()));
    connect(&timer,SIGNAL(timeout()),this,SLOT(slotTimeout()));
//信号...槽
//    t = 0;//这句注释掉,因为在mainwindow.h中已经初始化
    timer.start(10);//每10毫秒触发一次
}
void MainWindow::slotTimeout(){
    t++;
    label->setText(QString::number(t));
}

main.cpp

#include "QApplication"
//#include "QMainWindow"
#include "mainwindow.h"
int main(int argc, char *argv[]){
    QApplication a(argc, argv);
    MainWindow w;
    w.show();
    return a.exec();
}


QT入门_public_02


QFile::exists("/chord.wav") ? qDebug()<<"chemin ok" : qDebug()<<"chemin faux";
QString dir=QCoreApplication ::applicationDirPath();
QString filename(dir+"/chord.wav");
QSound::play ( filename );//播放声音


发出信号,现在头文件中定义

signals:
    void signalTimeToDoSth();//自定义信号在

在必要的时候发出信号

if(t == 100){
    emit signalTimeToDoSth();//发出信号
}

connect(this,SIGNAL(signalTimeToDoSth()),this,SLOT(slotTimeToDoSth()));

链接槽函数响应该信号。此处省略槽函数的具体写法。

QT用LCD方式显示时间

先在头文件定义

QLCDNumber *shizhong;
QVBoxLayout *layout;
QTimer timer1;//定义全局变量

shizhong = new QLCDNumber(this);
shizhong -> setFont(QFont("宋体",50));
shizhong-> move(420,30);
shizhong -> resize(90,40);
shizhong->setDigitCount(10);
shizhong->setMode(QLCDNumber::Dec);//十进制显示
shizhong->setSegmentStyle(QLCDNumber::Flat);//显示方式
layout = new QVBoxLayout();
layout->addWidget(shizhong);
timer1.start(1000) ;
QObject::connect(&timer1, SIGNAL(timeout()), this, SLOT(onTimerOut()));

槽函数

void MainWindow::onTimerOut()
{
    QTime time = QTime::currentTime();
    shizhong -> display(time.toString("hh:mm:ss"));
}

格式化显示时间

QString s;
QString r = s.sprintf("%02d:%02d:%02d",t/3600,t/60%60,t%60);
label->setText(r);


圆角矩形显示label

label->setStyleSheet("background:#f69; border-radius:25px");
label->setFixedSize(250, 50);

QT入门_include_03


小球碰撞边界检测算法

void MainWindow::slotMove()
{
    if(flagx == 0)
        x++;
    else
        x--;
    if(x + label->width() >= this->width())
    {
        flagx = 1;//It's time to move to the right derection
    }
    else if(x <= 0)
    {
        flagx = 0;//It's time to move to the left derection
    }
    if(flagy == 0)
        y++;
    else
        y--;
    if(y + label->height() >= this->height())
    {
        flagy = 1;//向上
    }
    else if(y <= 0)
    {
        flagy = 0;//向下
    }
    label->move(x,y);
}