1、快捷键:

  1. //整行移动 ctrl + shift + ↓
  2. //帮助文档 F1 或者是assist里面
  3. //自动对齐 ctrl + i;
  4. //同名之间的.h和.cpp切换 F4

2、设置主界面的图标方式

  1. //效果一样
  2. this->setWindownIcon(QPixmap());
  3. this->setWindownIcon(QIcon());

3、设置主界面的背景

  1. //设置主界面背景
  2. QPixmap pixmap ( ":/images/background.jpg");
  3. QPalette palettel;
  4. palette.setBrush(backgroundRole(),QBrush(pixmap));
  5. setPalette(palette);

4、窗口API:

  1. //resize(600,400); //只是设置窗口大小,可以拖拽改变大小
  2. setFixedSize(600,400); //固定窗口大小
  3. setWindowTitle("Title"); //设置窗口标题

5、QString转为char
一般在打印输出时,QString类型会出现双引号括起来,但char
没有,对于想让QString转为char *

先把QString转为QByteArray,再转为char *

1、QPushButton常用API

1、创建 QPushButton * b = new QPushButton;
2、嵌套到窗口中 b->setParent(this);
3、设置按钮显示文本 b->setText(“文本”);
4、自定义按钮位置 b->move();
5、自定义按钮大小 b->resize();

第二个重载版本,直接创建按钮,弊端:会按照按钮大小创建窗口,不方便
QPushButton * b2 = new QPushButton(“num2”,this);

2、对象树

在QT中new的对象会提供一个Parent指针,QObject是以对象树形式组织起来的,统一析构。
对象树.png

3、继承QWidge

要创建新的类/重写自己的控件时,如果找不到具体的父类,就找父类的父类,如继承QWidge,接下来要改三个地方:
头文件 #include

  1. 类class myButton : public QPushButton
  2. .cpp文件 myButton::myButton(QWidge* parent):QPushButton(parent)

4、信号和槽

4.1、系统的信号和槽

信号:返回值void,需要声明,不需要实现,可以有参数,发生函数重载。
槽:返回值void,需要声明,也需要实现(接受信号需要处理),可以有参数,发生函数重载。

  1. //参数1 信号的发送者 参数2 发送的信号(函数的地址)
  2. //参数3 信号的接受者 参数4 处理的槽函数(函数的地址)
  3. //connect(发送者,&发送的信号,接收者,&处理的槽函数)
  4. connect(zt,&Teacher::hunger,st,&Student::treat);

4.2、自定义信号和槽

创建类,继承于QObject,QT会对C++扩展 signals 和 slots。

信号函数:信号函数必须写在signals下,需要声明不需要实现
槽函数:早期的qt版本槽函数必须写在public slots作用域下,在5.6版本后可直接写在public作用域或全局作用域下,需要声明也需要实现,可以有参数,可发生重载。

因为信号和槽传的是函数的地址,对于重载的信号和槽函数需要定义函数指针(一般由指针指向地址,所以函数地址由函数指针指向)

  1. void(Teacher::*teacherSignal)(QString)= &Teacher::hunger;
  2. void(Student::*studentSlot)(QString)= &Student::treat;
  3. connect(zt,teacherSignal,st,studentSlot);

tips:
1、信号可以连接信号

connect(btn,&QpushButton::clicked,zt,teacherSignal); connect(zt,teacherSignal,st,studentSlot);

2、一个信号可以连接多个槽函数 (并不是同步的)
3、多个信号可以 连接 同一个槽函数(并不是同步的)
4、信号和槽函数的参数类型,必须一一对应
5、信号的参数个数 可以多于 槽函数的参数

4.3、lambda表达式:用于定义并创建匿名的函数

[ ] ( ){ } ; 理解为函数声明

[ ] ( ){ } ( ) ; 理解为函数调用

1、函数参数:[ ], 标识一个Lambda的开始,这部分必须存在,不能省略。
里面的参数主要有下面几种形式:
空。 没有使用任何函数对象参数。
= 。Lambda所在作用范围内所有可见的局部变量,并且是值传递方式
& 。Lambda所在作用范围内所有可见的局部交量,并且是引用传递方式

2、操作符重载函数参数:( ) 传参数用的
没有参数时,这部分省略。有参数时参数可以按值(如: (a,b)) 和按引用(如: (&a,&b)) 两种方式进行传递

3、函数返回值: -> 返回值类型, 函数返回值的类型,当返回值为void,或者函数体中只有一处return的地方时,这部分可以省略。

  1. int ret = []() ->int { return 1000; }();

4、函数体:{} 函数的实现,这部分不能省略,但函数体可以为空

Qt Creator 提供的默认基类只有 QMainWindow、QWidget、QDialog三种窗体,在5,6章介绍
应用程序主窗口QMainWindow 和 对话框窗口QDialog 都继承自 QWidget

5、QMainWindow

QMainWindow为用户提供主窗口程序的类,他包含了菜单栏、工具栏、锚接部件(浮动窗口,可停靠)、状态栏(一般在底部)、中心部件

5.1、菜单栏(最多有一个菜单栏)

  1. //创建菜单栏(开始是空的),创建了具体菜单才会显示
  2. QMenuBar* bar = menuBar() ; //menuBar源码中已将创建的对象放入到对象树中,所以可以不用new
  3. setMenuBar(bar);
  4. //创建菜单
  5. QMenu* fileMenu = bar->addMenu("文件"); //bar->addMenu()有返回值QMenu类型
  6. //创建菜单项
  7. fileMenu->addAction("新建文件");
  8. fileMenu->addAction("打开文件");
  9. fileMenu->addSeparator(); //添加分隔线
  10. fileMenu->addAction("保存文件");

5.2、工具栏(可以有多个)

  1. //创建工具栏
  2. QToolBar* toolBar = new QToolBar(this);
  3. addToolBar(Qt::LeftToolArea); //初始停靠位置
  4. toolBar->setAllowedArea(Qt::LeftToolArea); //后期更改可以停靠的位置
  5. toolBar->setMovable(false); //不允许拖拽
  6. toolBar->addAction();

5.3、状态栏(最多有一个状态栏)

  1. QStatusBar * stBar = statusBar();
  2. setStatusBar(stBar);
  3. //放标签控件 需要包含头文件<QLabel>
  4. QLabel * label = new QLabel("提示信息", this);
  5. stBar->addWidget(label);
  6. QLabel * label2 = new QLabel("右侧提示信息", this);
  7. stBar->addPermanentWidget(label2);

5.4、锚接部件(浮动窗口)(可以有多个)

  1. QDockWidget * dockWidget = new QDockWidget("输出", this);
  2. addDockWidget(Qt::BottomDockWidgetArea, dockWidget); //停靠在下边

5.5、中心部件(只能有一个)

  1. QTextEdit * edit = new QTextEdit(this);
  2. setCentralWidget(edit);

小技巧:
1、栏只能创建一个的不需要用new(系统封装的就已经放在对象树里了),如果可以创建多个的就要new
2、部件只能创建一个的,接口形式都是set……,部件可以创建多个的,接口形式是add……

6、QDialog

对话框:用来完成短小任务或者和用户进行简单交互的顶层窗口

6.1 模态与非模态

模态对话框:当启动后,不可以对其他窗口进行操作(阻塞、独占、垄断)
非模态对话框:当启动后,可以对其他窗口进行操作

  1. //模态对话框
  2. //对象创建在栈区,但由于dlg.exec()阻塞,对话框不会一闪而过
  3. QDialog dlg(this);
  4. dlg. resize (200, 100);
  5. dlg.exec(); //阻塞
  6. qDebug() << "模态对话框弹出了”;
  7. //非模态对话框
  8. //QDialog dlg2(this) ; //在代码执行完就被自动释放了,所以后面调用dlg2->show()只会一闪而过
  9. //防止对话框一闪而过,将对象创建在堆区
  10. QDialog * dlg2 = new QDialog (this);
  11. dlg2->resize (200, 100);
  12. dlg2->show();
  13. dlg2->setAttribute (Qt: :WA_ DeleteOnClose); //Qt的55号 在关闭新对话框时释放内存,防止内存泄漏
  14. qDebug() << "非模态对话框弹出了";

其实也可以通过->show()来建立模态对话框,只需要在前面使用setModal(true) 函数

6.2 标准对话框

所谓标准对话框,是Qt内置的一系列对话框,用于简化开发。事实上,有很多对话框都是通用的,比如打开文件、设置颜色、打印设置等。这些对话框在所有程序中几乎相同,因此没有必要在每一个程序中都自己实现这么一个对话框。使用标准对话框前记得包含头文件
Qt的内置对话框大致分为以下几类:
QMessageBox:模态对话框,用于显示信息、询问问题等(属于模态对话框)

  1. //错误消息
  2. QMessageBox::critical(this, "critical", "错误");
  3. //信息消息()
  4. QMessageBox::information(this, "info", "信息");
  5. //提问消息
  6. //参数1父亲参数 2标题参数 3提示内容参数 4按键类型参数 5默认关联回车按键
  7. QMessageBox::question (this, "ques", "提问" , QMessageBox::Save | QMessageBox::Cancle, QMessageBox::Cancle);

QColorDialog:

  1. QColorDialog::getColor(QColor(255, 0, 0));

QFileDialog(非模态)

  1. //参数1父亲 参数2标题 参数3默认打开路径 参数4过滤文件格式
  2. QFileDialog::getopenFileName(this, "打开文件", "C:\Users\administrator\Desktop", "(*. txt)") ;

QFontDialog: 选择字体
QInputDialog: 允许用户输入一个值,并将其值返回
QPegeSetupDialos: 为打印机提供纸张相关的选项
QPrintDialog: 打印机配置
QPrintPreviewDialog: 打印预览
QProgressDialog: 显示操作过程

6.3 自定义对话框

7、布局界面

如实现一个登录窗口
1、随意创建几个标签、输入框、按钮等
2、选取containers里面的Widget 给窗口进行布局(利用栅格布局)(类似于图层锁定)
在参数列表中的 sizePolicy 参数可以调节Widget的水平和垂直尺度
3、多利用弹簧进行布局(有些参数可以调)

4、默认窗口和控件之间有9间隙,可以调整layout LeftMargin
5、密码的echoMode设置为password

8、控件

8.1 Button

1 QPush Button 控件按钮。
2 QTool Button 工具按钮用于显示图片,如果想显示图片文字,修改参数:toolButtonStyle
凹凸参数 autoRaise
3 Radio Button 单选按钮, 设置默认ui->rBtnMan->setChecked(true)
4 Check Box 多选按钮,监听状态, 2选中 0未选中 1半选

8.2 Widget / View

![ZLP2T_S~E~{`I%ZZIU@$6V.png
1、QList Widget 列表控件(类似与记事本)

  1. ui->listWidget->additem();
  2. //也可利用additems一次添加多行
  3. ui->listWidget->additems();

2、QTree Widget 树控件(类似与通讯录列表)

  1. //设置控件头名称
  2. ui->treeWidget->setHeaderLabels(QStringList() << "英雄" << "英雄介绍");
  3. //创建根节点
  4. QTreeWidgetltem * pltem = new QTreeWidgetltem(QstringList()<<"力量");
  5. //添加根节点到树控件上
  6. ui->treewidget->addTopLeveltem(liltem);
  7. //创建子节点
  8. QTreeWidgetltem * n = new QTreeWidgetltem(QstringList()<<"lallalallallal");
  9. //添加子节点到根上
  10. pItem->addChild(n);

8.3 Table Widget

  1. //设置列数
  2. ui->tablewidget->setColumnCount(3);
  3. //设置水平表头
  4. ui->tablewidget->setHorizontalHeaderLabels(QstringList()<<"姓名"<<"性别"<<"年龄");
  5. //设置行数
  6. ui->tablewidget->setRowCount(5);
  7. //设置正文
  8. ui->tablewidget->setItem (0, 0, new QTablewidgetItem ("亚瑟"));

8.3 自定义控件(带ui的)

封装:
1、在QT模板中选择Qt设计师界面类 创建后包含了(.h .cpp .ui)三个文件
2、在 .ui 文件中可以组合一些控件
3、在widget.ui文件中使用封装好的控件,拖拽相同类型的控件,提升为封装的 ui类
如果类型不同这样修改(三个地方:.h头文件中 include 和 public QWidget
.cpp文件中 继承QWidget (parent))
4、就可以使用了
也可以封装不带ui的,那就在普通的c++模板中创建文件即可。

9、事件

//事件更底层,它是封装windows消息形成的,如鼠标,
//信号更上层,一般是组件释放的消息,本质上信号在对象中的事件处理函数中产生,信号

封装自定义标签
1、在QT模板中选择Qt设计师界面类 创建后包含了(.h .cpp .ui)三个文件
2、在 .ui 文件中可以组合一些控件
3、在widget.ui文件中使用封装好的控件,拖拽相同类型的控件,提升为封装的 ui类
如果类型不同这样修改 三个地方:头文件include”Qlabel” public Qlabel .cpp文件中继承 Qlabel(parent)

9.1 鼠标事件

mouseEvent
2、鼠标进入事件enterEvent
3、鼠标离开事件leaveEvent
4、鼠标按下mousePressEvent(QMouseEvent *)
5、鼠标释放mouseReleaseEvent
6、鼠标移动mouseMoveEvent

9.2 定时器

timerEvent
方式一:
1、利用事件 void timerEvent ( QTimerEvent* ev)
2、启动定时器 startTimer(1000);

  1. //定时器第二种方式
  2. QTimer * timer = new QTimer (this) ;
  3. //启动定时器
  4. timer->start (500);
  5. connect (timer, &QTimer::timeout, [=](){
  6. static int num = 1;
  7. //label_4每隔0.5秒+1
  8. ui->label_4->setText(Qstring::number(num++));
  9. });
  10. //暂停定时器
  11. timer->stop();

9.3 绘图事件

记得包含头文件 #include

  1. void widget: :paintEvent(QPaintEvent *){
  2. //实例化画家对象this指定的是绘图设备
  3. QPainter painter(this);
  4. //设置画笔
  5. QPen pen(gcolor (255,0,0)) ;
  6. //让画家使用这个笔
  7. painter.setPen(pen);
  8. //设置画刷
  9. QBrush brush(Qt::cyan);
  10. //让画家使用画刷
  11. painter.setBrush(brush);
  12. //画线
  13. painter.drawLine (QPoint (0,0) , QPoint (100,100) );
  14. //画圆(椭圆)
  15. painter.drawEllipse ( QPoint(100,100), 50,50);
  16. //画矩形
  17. painter.drawRect (QRect (20,20,50,50));
  18. //画文字
  19. painter.drawText(QRect (10,200,150,50), "好好学习,天天向上");
  20. }

案例

1、封装一个金币类

金币按钮控件与前面的开始按钮和返回按钮实现方式都不同,故新封装一个金币类

  1. class myCoin : public QPushButton
  2. {
  3. public:
  4. //explicit myCoin(QWidget *parent = nullptr);
  5. //重写构造函数,传入路径参数
  6. myCoin(QString btnImgPath);
  7. //金币属性
  8. int m_posX;
  9. int m_posY;
  10. bool m_flag;
  11. int m_min = 1;
  12. int m_max = 8;
  13. QTimer *m_timer1; //正面->反面
  14. QTimer *m_timer2; //反面->正面
  15. //翻转的标志位函数
  16. void changeFlag();
  17. bool m_isWin = false;
  18. signals:
  19. };

重写构造函数

  1. myCoin::myCoin(QString btnImgPath)
  2. {
  3. //拿到图片
  4. QPixmap pix = QPixmap(btnImgPath);
  5. //设置图片固定大小
  6. this->setFixedSize(pix.width() ,pix.height() );
  7. //设置不规则图片样式,把边框去了
  8. this->setStyleSheet ("QPushButton { border : 0px; } ");
  9. //设置图标
  10. this->setIcon (pix);
  11. //设置图标大小,适应于上面创建的setFixedSize大小
  12. this->setIconSize (QSize(pix.width(), pix.height()));
  13. m_timer1 = new QTimer(this);
  14. m_timer2 = new QTimer(this);
  15. //监听正面翻反面的信号,并且翻转金币
  16. connect(m_timer1, &QTimer::timeout, this, [=](){
  17. QPixmap pix;
  18. QString imagePath = QString(":/images/Coin000%1.png").arg(this->m_min++);
  19. if (!pix.load(imagePath))
  20. {
  21. qDebug() << "wrong path:" << imagePath;
  22. return;
  23. }
  24. else
  25. {
  26. this->setFixedSize(pix.width(), pix.height());
  27. this->setStyleSheet("QPushButton{border:0px;}");
  28. this->setIcon(pix);
  29. this->setIconSize(QSize{pix.width(), pix.height()});
  30. }
  31. if (this->m_min > this->m_max)
  32. {
  33. this->m_min = 1;
  34. m_timer1->stop();
  35. this->m_isAnimation = false;
  36. }
  37. });
  38. connect(m_timer2, &QTimer::timeout, [=]() {
  39. QPixmap pix;
  40. QString imagePath = QString(":/images/Coin000%1.png").arg(this->m_max--);
  41. if (!pix.load(imagePath))
  42. {
  43. qDebug() << "wrong path:" << imagePath;
  44. return;
  45. }
  46. else
  47. {
  48. this->setFixedSize(pix.width(), pix.height());
  49. this->setStyleSheet("QPushButton{border:0px;}");
  50. this->setIcon(pix);
  51. this->setIconSize(QSize{pix.width(), pix.height()});
  52. };
  53. if (this->m_min > this->m_max)
  54. {
  55. this->m_max = 8;
  56. m_timer2->stop();
  57. this->m_isAnimation = false;
  58. }
  59. });
  60. }

实例化金币类

  1. myCoin * mycoin = new myCoin(":/images/Coin0001.png");
  2. mycoin->setParent(playLabel);
  3. mycoin->move(2, 2);

根据dataconfig配置每个关卡默认的金币和银币显示状态

  1. class dataConfig : public QObject
  2. {
  3. public:
  4. explicit dataConfig(QObject *parent = 0);
  5. public:
  6. // 第几关 几行几列的元素
  7. QMap<int, QVector<QVector<int>>> mData;
  8. };
  1. 通过dataconfig config;
  2. config.mData[i][j][k] 可以拿到各个位置的元素值

2、初始化选定的关卡

在playScene类中声明一个成员变量m_array,记录当前关卡的二维数组

为什么不用QMap > >类型的数据呢,因为在类中已经有Levelnum表示传进来第几关,故用m_array[4] [4]

  1. class playScene : public QMainWindow
  2. {
  3. public:
  4. playScene(int Levelnum);
  5. int levelIndex;
  6. //重写paintEvent事件,画背景图
  7. void paintEvent(QPaintEvent * );
  8. //二维数组,维护当前关卡的具体数组
  9. int m_array[4][4];
  10. myCoin * m_coinList[4][4];
  11. bool isWin = true;
  12. signals:
  13. //自定义信号,告诉主场景点击了返回按钮
  14. void chooseSceneback2();
  15. };

在palyScene界面开始具体实现翻金币

  1. //初始化每个关卡的二维数组
  2. dataConfig config;
  3. for(int i = 0; i < 4; i++)
  4. {
  5. for (int j = 0;j < 4; j++)
  6. {
  7. //这个目的是将config.mData[i][j][k]降维成二维m_array[i][j],得到当前关卡的各个元素值
  8. this->m_array[i][j] = config.mData[this->levelIndex][i][j];
  9. qDebug() << this->m_array[i][j];
  10. }
  11. }
  12. //游戏界面
  13. for (int i = 0; i < 4; ++i)
  14. {
  15. for (int j = 0; j < 4; ++j)
  16. {
  17. //绘制金币的灰色背景图片BoardNode.png
  18. QLabel *playLabel = new QLabel;
  19. QPixmap pix = QPixmap(":/images/BoardNode.png");
  20. playLabel->setGeometry(0, 0, pix.width(), pix.height());
  21. playLabel->setPixmap(pix);
  22. playLabel->setParent(this);
  23. playLabel->move(57 + i * pix.width(), 200 + j * pix.height());
  24. //实例化一个coin对象,如果m_array[i][j] 为1,将imagePath = 金币;m_array[i][j] 为0,将imagePath = 银币
  25. QString imagePath;
  26. if (this->m_array[i][j])
  27. {
  28. imagePath = ":/images/Coin0001.png";
  29. }
  30. else
  31. {
  32. imagePath = ":/images/Coin0008.png";
  33. }
  34. myCoin * coin = new myCoin(imagePath);
  35. coin->setParent(playLabel);
  36. coin->move(2, 2);
  37. //给金币属性赋值
  38. coin->m_posX = i;
  39. coin->m_posY = j;
  40. coin->m_flag = this->m_array[i][j]; //强制int 转 bool
  41. //将金币放入到金币的二维数组中
  42. m_coinList[i][j] = coin;
  43. //点击硬币,进行翻转
  44. connect(coin, &myCoin::clicked, this, [=]() {
  45. coin->changeFlag();
  46. //维护数据,每次翻转之后记录当前的元素值
  47. this->m_array[i][j] = this->m_array[i][j] == 1 ? 0 : 1;
  48. QTimer::singleShot(30, this, [=]() {
  49. if (coin->m_posX > 0)
  50. {
  51. this->m_coinList[coin->m_posX - 1][coin->m_posY]->changeFlag();
  52. this->m_array[i - 1][j] = this->m_array[i - 1][j] == 1 ? 0 : 1;
  53. }
  54. if (coin->m_posX < 3)
  55. {
  56. this->m_coinList[coin->m_posX + 1][coin->m_posY]->changeFlag();
  57. this->m_array[i + 1][j] = this->m_array[i + 1][j] == 1 ? 0 : 1;
  58. }
  59. if (coin->m_posY > 0)
  60. {
  61. this->m_coinList[coin->m_posX][coin->m_posY - 1]->changeFlag();
  62. this->m_array[i][j - 1] = this->m_array[i][j - 1] == 1 ? 0 : 1;
  63. }
  64. if (coin->m_posY < 3)
  65. {
  66. this->m_coinList[coin->m_posX][coin->m_posY + 1]->changeFlag();
  67. this->m_array[i][j + 1] = this->m_array[i][j + 1] == 1 ? 0 : 1;
  68. }
  69. this->isWin = true;
  70. for (int i = 0; i < 4; ++i)
  71. {
  72. for (int j = 0; j < 4; ++j)
  73. {
  74. if (m_coinList[i][j]->m_flag == false)
  75. {
  76. this->isWin = false;
  77. break;
  78. }
  79. }
  80. }
  81. if (this->isWin)
  82. {
  83. //当所有的 游戏胜利
  84. qDebug() << "win!";
  85. //将所有的标志位m_coinList[i][j]->m_isWin置为true,鼠标事件直接return,按钮不能再次响应
  86. for (int i = 0; i < 4; ++i)
  87. {
  88. for (int j = 0; j < 4; ++j)
  89. {
  90. this->m_coinList[i][j]->m_isWin = true;
  91. }
  92. }
  93. // winSound->play();
  94. // QPropertyAnimation *animation1 = new QPropertyAnimation(winLabel, "geometry");
  95. // animation1->setDuration(1000);
  96. // animation1->setStartValue(QRect(winLabel->x(), winLabel->y(), winLabel->width(), winLabel->height()));
  97. // animation1->setEndValue(QRect(winLabel->x(), winLabel->y() + 114, winLabel->width(), winLabel->height()));
  98. // animation1->setEasingCurve(QEasingCurve::OutBounce);
  99. // animation1->start(QAbstractAnimation::DeleteWhenStopped);
  100. }
  101. });
  102. });
  103. }
  104. }

3、核心:金币和银币的翻转

因为在实例化硬币时myCoin * coin = new myCoin(imagePath);没有赋予任何属性,只知道显示图片,不知道它在界面中的第几行第几列,所以后面在class myCoin中加入了三个属性

  1. class myCoin : public QPushButton
  2. {
  3. //金币属性
  4. int m_posX;
  5. int m_posY;
  6. bool m_flag;
  7. //另外再加上改变标志的函数,实现硬币翻转
  8. void changeFlag();
  9. int m_min = 1;
  10. int m_max = 8;
  11. QTimer * m_timer1; //正面->反面
  12. QTimer * m_timer2; //反面->正面
  13. };

翻

  1. //实例化定时器
  2. m_timer1 = new QTimer(this);
  3. m_timer2 = new QTimer(this);
  4. //监听正面翻反面的信号this->m_flag = true,并且翻转金币,每30毫秒变一张图片
  5. connect(m_timer1, &QTimer::timeout, this, [=](){
  6. QPixmap pix;
  7. QString imagePath = QString(":/images/Coin000%1.png").arg(this->m_min++);
  8. pix.load(imagePath);
  9. this->setFixedSize(pix.width(), pix.height());
  10. this->setStyleSheet("QPushButton{border:0px;}");
  11. this->setIcon(pix);
  12. this->setIconSize(QSize{pix.width(), pix.height()});
  13. if (this->m_min > this->m_max)
  14. {
  15. this->m_min = 1;
  16. m_timer1->stop();
  17. }
  18. });
  19. connect(m_timer2, &QTimer::timeout, this, [=]() {
  20. QPixmap pix;
  21. QString imagePath = QString(":/images/Coin000%1.png").arg(this->m_max--);
  22. pix.load(imagePath);
  23. this->setFixedSize(pix.width(), pix.height());
  24. this->setStyleSheet("QPushButton{border:0px;}");
  25. this->setIcon(pix);
  26. this->setIconSize(QSize{pix.width(), pix.height()});
  27. if (this->m_min > this->m_max)
  28. {
  29. this->m_max = 8;
  30. m_timer2->stop();
  31. }
  32. });
  33. void myCoin::changeFlag()
  34. {
  35. //如果是正面,翻为反面
  36. if(this->m_flag)
  37. {
  38. m_timer1->start(30);
  39. this->m_flag = false;
  40. }
  41. //如果是反面,翻为正面
  42. else
  43. {
  44. m_timer2->start(30);
  45. this->m_flag = true;
  46. }
  47. }

————————————————————

QT网络编程

网络编程有TCP和UDP

1、TCP

TCP编程需要用到俩个类:QTcpServer 和 QTcpSocket
客户端部分只用到了QTcpSocket,服务器端两个都用到了,下面实现一个服务器与客户端之间通信

1.1 客户端

QT提供了QTcpSocket类,可以直接实例化一个客户端

QT另外提供了QTcpServer类,可以直接实例化一个服务器端

2、UDP

udp不分客户端和服务器,只需要使用一个类QUdpSocket