函数模板时用于生产函数的,T是类型参数,代表类型,编译器由模板自动生成函数时,会用具体的类型名对模板中所有的类型参数进行替换,其他部分则原封不动地保留。
widget.h
#ifndef WIDGET_H
#define WIDGET_H
#include <QWidget>
namespace Ui {
class Widget;
class Widget : public QWidget
Q_OBJECT
public:
explicit Widget(QWidget *parent = 0);
~Widget();
template<class T>
void swap(T & a,T & b);
private:
Ui::Widget *ui;
#endif
widget.cpp
#include "widget.h"
#include "ui_widget.h"
#include <QDebug>
#include "classtemplate.h"
Widget::Widget(QWidget *parent) :
QWidget(parent),
ui(new Ui::Widget)
ui->setupUi(this);
int num1 = 1;
int num2 = 3;
swap(num1,num2);
qDebug()<<"["<<__FILE__<<"]"<<__LINE__<<__FUNCTION__<<" "<<num1;
qDebug()<<"["<<__FILE__<<"]"<<__LINE__<<__FUNCTION__<<" "<<num2;
double num3 = 2.5;
double num4 = 3.6;
swap(num3,num4);
qDebug()<<"["<<__FILE__<<"]"<<__LINE__<<__FUNCTION__<<" "<<num3;
qDebug()<<"["<<__FILE__<<"]"<<__LINE__<<__FUNCTION__<<" "<<num4;
Widget::~Widget()
delete ui;
template<class T>
void Widget::swap(T &a, T &b)
T temp;
temp = a;
a = b;
b = temp;
[ ..\templateTest\widget.cpp ] 15 Widget::Widget 3
[ ..\templateTest\widget.cpp ] 16 Widget::Widget 1
[ ..\templateTest\widget.cpp ] 21 Widget::Widget 3.6
[ ..\templateTest\widget.cpp ] 22 Widget::Widget 2.5
类模板在STL中应用广泛,比如list、vector等都是类模板
classtemplate.h
#ifndef CLASSTEMPLATE_H
#define CLASSTEMPLATE_H
#include <QObject>
using namespace std;
template<class T1,class T2>
class ClassTemplate
public:
T1 key;
T2 value;
ClassTemplate(T1 k,T2 v):key(k),value(v){}
bool operator < (const ClassTemplate<T1,T2>& p) const;
template<class T1,class T2>
bool ClassTemplate<T1,T2>::operator < (const ClassTemplate<T1,T2>& p) const
return key < p.key;
#endif
ClassTemplate<QString,int> student("Tom",19);
qDebug()<<"["<<__FILE__<<"]"<<__LINE__<<__FUNCTION__<<" "<<student.key;
qDebug()<<"["<<__FILE__<<"]"<<__LINE__<<__FUNCTION__<<" "<<student.value;
[ ..\templateTest\widget.cpp ] 25 Widget::Widget "Tom"
[ ..\templateTest\widget.cpp ] 26 Widget::Widget 19
函数模板函数模板时用于生产函数的,T是类型参数,代表类型,编译器由模板自动生成函数时,会用具体的类型名对模板中所有的类型参数进行替换,其他部分则原封不动地保留。widget.h#ifndef WIDGET_H#define WIDGET_H#include <QWidget>namespace Ui {class Widget;}class Widget : public QWidget{ Q_OBJECTpublic: explicit Widge
前面我们了解了关于Qt字符串的一些简单操作,容器类的分类和各自的主要特点以及用途,这一次我们了解一些常见的工具类和常见的控件。
二、QByteArry和QVariant
2.1 QByteArry
关于QByteArry,我们在上篇中曾经看到过。QByteArry和QString的功能和API基本类似,具有很多相似的函数。不同的地方在于QByteArry能够存储原生的二进...
1、 int QObject::startTimer ( int interval ) ;
这个是开启一个定时器的函数,他的参数interval是毫秒级别。当开启成功后会返回这个定时器的ID, 并且每隔interval 时间后会进入timerEvent 函数。直到定时器被杀死。
2、 void QObject::timerEvent ( QTimerEvent * even.
2.使用QTimer的简单流程如下
首先先创建一个QTimer对象–>通过QTimer中的start方法让它开始计时(start方法可以设定定时运行的时间)–>每当计时的时间超过了给定的时间后,就会调用一次timeout.connect(xx)中的xx函数–>使用完后调用stop方法关闭计时器
3.详细流程
这里我们设计这样一个项目,通过点击button来开
QTimer类提供了重复和单次触发信号的定时器。
a.void timeout ()定时器超时后,这个信号被发射。
b.void start()开启定时器,它的重载函数void start(int msec),启动或重新启动一个超时时间间隔为毫秒的定时器。
如果定时器正在运行,它将被停止和重新启动。
c.void stop()停止定时器.
d.void setInterval(int msec)设置超时间隔(毫秒为单位)。
QTimer *timer = new QT
Qt中提供的容器模板类类似于STL,它提供了Java-style iterators and STL-style iterators两种风格的迭代器,此外还提供了foreach关键字。Container Classessequential containers:
QVector<T>
QVector的内存模型是预先分配好大小的连续数组,所以可以通过索引快速访问,但如果在头部或者中间插入删除需要大量
在进行控制系统上位机编程时,我们经常需要用到定时器,在定时服务函数中执行控制动作或者显示数据等,编程时候使用到的类是QTimer类,我们要创建两个定时器,第一个定时器是在对话框的界面线程中创建的,他的定时事件在主循环中进行,而第二个定时器是在一个子线程中创建的,它的定时事件在子线程中循环,不受主线程其他事件的影响,我认为如果程序对于定时精度要求高的话,最好采用第二种设计方法,如果是诸如显示数据之类的对定时精度要求不高的动作,则可以采用第一种设计方法,更加省时省力。
2 准备工作
定时器需要用到QT
while (true) {
QTcpSocket *client = server.nextPendingConnection(); // 等待客户端连接
connect(client, &QTcpSocket::readyRead, [=]() {
QByteArray data = client->readAll(); // 读取客户端发送的数据
qDebug() << "Received data: " << data;
client->write("Hello, client!"); // 发送数据给客户端
// 客户端代码
QTcpSocket socket;
socket.connectToHost("127.0.0.1", 8888); // 连接服务器
if (socket.waitForConnected()) {
socket.write("Hello, server!"); // 发送数据给服务器
socket.waitForReadyRead(); // 等待服务器回复
QByteArray data = socket.readAll(); // 读取服务器回复的数据
qDebug() << "Received data: " << data;
} else {
qDebug() << "Failed to connect to server!";
注意:以上代码仅为示例,实际使用时需要进行错误处理、断开连接等操作。