如图所示设计一个闹钟
1、头文件
#ifndef WIDGET_H
#define WIDGET_H
#include <QWidget>
#include <QColor>
#include <QDebug>
#include <QMessageBox>
#include <QTimer> //定时器类的头文件
#include <QTime> //时间类的头文件
#include <QTimerEvent> //定时器事件处理类
#include <QDateTime> //日期时间类
namespace Ui {
class Widget;
}
class Widget : public QWidget
{
Q_OBJECT
public:
explicit Widget(QWidget *parent = nullptr);
~Widget();
QDateTime sys_time;
private slots:
void on_open_clicked();
void on_close_clicked();
void timerEvent(QTimerEvent *e);
private:
Ui::Widget *ui;
//定义一个定时器的标识
int tid;
int flag=0;
};
#endif // WIDGET_H
2、main函数
#include "widget.h"
#include <QApplication>
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
Widget w;
w.show();
return a.exec();
}
3、功能函数
#include "widget.h"
#include "ui_widget.h"
Widget::Widget(QWidget *parent) :
QWidget(parent),
ui(new Ui::Widget)
{
ui->setupUi(this);
ui->label->setFont(QFont("隶书",28));
ui->edit->setFont(QFont("隶书",28));
ui->label_2->setFont(QFont("隶书",28));
ui->edit_2->setFont(QFont("隶书",30));
ui->listWidget->setFont(QFont("隶书",28));
//给定时器指针实例化空间
// t1 = new QTimer(this);
tid = this->startTimer(1000);
}
Widget::~Widget()
{
delete ui;
}
void Widget::on_open_clicked()
{
if(ui->edit->text().toInt()<24&&ui->edit->text().toInt()>=0&&ui->edit->text()!=nullptr)
{ if(ui->edit_2->text().toInt()<60&&ui->edit_2->text().toInt()>=0&&ui->edit_2->text()!=nullptr)
{
ui->open->setEnabled(false);
ui->close->setEnabled(true);
ui->edit->setEnabled(false);
ui->edit_2->setEnabled(false);
flag=1;
}
else
{
QMessageBox::information(this,"错误","请输入正确的时间格式");
}
}
else
{
QMessageBox::information(this,"错误","请输入正确的时间格式");
}
}
void Widget::on_close_clicked()
{
ui->close->setEnabled(false);
ui->open->setEnabled(true);
ui->edit->setEnabled(true);
ui->edit_2->setEnabled(true);
flag=0;
ui->listWidget->clear();
}
//定时器事件处理函数
void Widget::timerEvent(QTimerEvent *e)
{
if(e->timerId() == tid)
{
// static int num = 1;
// ui->eventLab->setNum(num++);
//获取系统当前的日期时间
sys_time = QDateTime::currentDateTime();
//将时间展示到ui界面
ui->label->setText(sys_time.toString("yyyy MM dd ddd hh:mm:ss"));
}
qDebug()<<sys_time.toString("hh")<<ui->edit->text();
if(flag==1)
{
if(ui->edit->text()==sys_time.toString("hh"))
{
if(ui->edit_2->text()==sys_time.toString("mm"))
{
ui->listWidget->addItem("时间到了");
flag=0;
}
}
}
}
TCP
1、服务器
(头文件)
#ifndef WIDGET_H
#define WIDGET_H
#include <QWidget>
#include <QTcpServer>
#include <QTcpSocket>
#include <QList>
#include <QDebug>
#include <QMessageBox>
namespace Ui {
class Widget;
}
class Widget : public QWidget
{
Q_OBJECT
public:
explicit Widget(QWidget *parent = nullptr);
~Widget();
void newConnection_slot();
void readyread_slot();
private slots:
void on_bin_clicked();
private:
Ui::Widget *ui;
//定义服务器指针
QTcpServer *server;
//定义客户端容器,存客户端指针
QList<QTcpSocket*> clientList;
};
#endif // WIDGET_H
TCP功能函数
#include "widget.h"
#include "ui_widget.h"
Widget::Widget(QWidget *parent) :
QWidget(parent),
ui(new Ui::Widget)
{
ui->setupUi(this);
//实例化一个服务器
server = new QTcpServer(this);
}
Widget::~Widget()
{
delete ui;
}
void Widget::on_bin_clicked()
{
quint16 port=ui->edit->text().toUInt();
//进入监听状态
//参数1:any:监听所有主机
//参数2:监听端口号,如果是0,表示服务器自动选择
if(ui->bin->text()=="启动服务器")
{
if(server->listen(QHostAddress::Any,port) == true)
{
qDebug()<<"监听成功";
QMessageBox::information(this,"成功","服务器打开成功");
ui->edit->setEnabled(false);
ui->bin->setText("关闭服务器");
}
else
{
qDebug()<<"监听失败";
QMessageBox::information(this,"失败","服务器打开失败");
}
connect(server,&QTcpServer::newConnection,this,&Widget::newConnection_slot);
}
else if(ui->bin->text()=="关闭服务器")
{
while(clientList.size()!=0)
{
clientList.pop_back();
}
server->close();
ui->edit->setEnabled(true);
ui->bin->setText("启动服务器");
}
}
//槽函数的实现
void Widget::newConnection_slot()
{
//获取最新链接的客户端套接字
QTcpSocket *s=server->nextPendingConnection();
//将套接字放入链表
clientList.push_back(s);
//此时已经成功链接
//如果客户端发送信号,该客户端会自动发送信号
connect(s,&QTcpSocket::readyRead,this,&Widget::readyread_slot);
}
void Widget::readyread_slot()
{
//清理链表中无效客户端
for(int i=0; i<clientList.size();i++)
{
//找到任意一个客户端
if(clientList.at(i)->state()==0)
{
clientList.removeAt(i);
}
}
for(int i=0;i<clientList.size();i++)
{
//判断当前有无可读数据
if(clientList.at(i)->bytesAvailable()==0)
qDebug()<<"无数据可读";
else {
QByteArray msg = clientList.at(i)->readAll();
ui->qww->addItem(QString::fromLocal8Bit(msg));
for(int j=0;j<clientList.count();j++)
{
clientList.at(j)->write(msg);
}
}
}
}
客户端
(头文件)
#ifndef WIDGET_H
#define WIDGET_H
#include <QWidget>
#include <QTcpServer>
#include <QTcpSocket>
#include <QDebug>
#include <QMessageBox>
namespace Ui {
class Widget;
}
class Widget : public QWidget
{
Q_OBJECT
public:
explicit Widget(QWidget *parent = nullptr);
~Widget();
private slots:
void on_pushButton_2_clicked();
void on_pushButton_clicked();
void connected_slot();
void readyread_slot();
private:
Ui::Widget *ui;
QTcpSocket *socket;
QString username;
};
#endif // WIDGET_H
功能函数
#include "widget.h"
#include "ui_widget.h"
Widget::Widget(QWidget *parent) :
QWidget(parent),
ui(new Ui::Widget)
{
ui->setupUi(this);
ui->lineEdit->setPlaceholderText("ip");
ui->lineEdit_2->setPlaceholderText("port");
ui->lineEdit_3->setPlaceholderText("请输入你要发送的信息");
ui->lineEdit_4->setPlaceholderText("用户名");
socket = new QTcpSocket(this);
connect(socket,&QTcpSocket::connected,this,&Widget::connected_slot);
connect(socket,&QTcpSocket::readyRead,this,&Widget::readyread_slot);
}
Widget::~Widget()
{
delete ui;
}
void Widget::connected_slot()
{
//qDebug()<<"链接成功";
QMessageBox::information(this,"成功","链接成功");
QString msg = username + ": 加入群聊";
ui->pushButton_2->setText("断开链接");
ui->lineEdit->setEnabled(false);
ui->lineEdit_2->setEnabled(false);
ui->lineEdit_4->setEnabled(false);
socket->write(msg.toLocal8Bit());
ui->listWidget->addItem(msg);
}
void Widget::readyread_slot()
{
QByteArray msg = socket->readAll();
ui->listWidget->addItem(QString::fromLocal8Bit(msg));
}
void Widget::on_pushButton_2_clicked()
{
//获取ip,port,用户名
username = ui->lineEdit_4->text();
QString ip = ui->lineEdit->text();
quint16 port = ui->lineEdit_2->text().toUInt();
if(ui->pushButton_2->text()=="链接服务器")
{
socket->connectToHost(ip,port);
}
else if(ui->pushButton_2->text()=="断开链接")
{
//
QString msg = username + ": 离开群聊";
socket->write(msg.toLocal8Bit());
ui->lineEdit->setEnabled(true);
ui->lineEdit_2->setEnabled(true);
ui->lineEdit_4->setEnabled(true);
socket->disconnectFromHost();
ui->pushButton_2->setText("链接服务器");
}
}
void Widget::on_pushButton_clicked()
{
//发送
QString sendmsg = ui->lineEdit_3->text();
sendmsg = username + ": " + sendmsg;
socket->write(sendmsg.toLocal8Bit());
ui->listWidget->addItem(sendmsg);
ui->lineEdit_3->clear();
}
效果图
drawer
头文件
#ifndef WIDGET_H
#define WIDGET_H
#include <QWidget>
#include <QMouseEvent>
#include <QPaintEvent>
#include <QPoint>
#include <QPainter>
namespace Ui {
class Widget;
}
class Widget : public QWidget
{
Q_OBJECT
public:
explicit Widget(QWidget *parent = nullptr);
~Widget()override;
//声明要重写的鼠标移动事件
void mouseMoveEvent(QMouseEvent *event) override;
void mousePressEvent(QMouseEvent *event) override;
void paintEvent(QPaintEvent *event) override;
private:
Ui::Widget *ui;
//定义起始点坐标
QPoint startPoint;
//定义一个图像容器
QPixmap *pix;
};
#endif // WIDGET_H
功能函数
#include "widget.h"
#include "ui_widget.h"
Widget::Widget(QWidget *parent) :
QWidget(parent),
ui(new Ui::Widget)
{
ui->setupUi(this);
pix = new QPixmap(this->size());
pix->fill(Qt::white);
}
Widget::~Widget()
{
delete ui;
}
//鼠标移动事件
void Widget::mouseMoveEvent(QMouseEvent *event)
{
//定义一个画家,在pix上作画
QPainter p1(pix);
//定义画笔
QPen pen(Qt::SolidLine);
pen.setColor(QColor(53,125,125));
pen.setWidth(10) ;
//给画家设置笔
p1.setPen(pen);
//画线
p1.drawLine(startPoint,event->pos());
startPoint=event->pos();
//更新事件
this->update();
}
//鼠标按压事件
void Widget::mousePressEvent(QMouseEvent *event)
{
//获取鼠标起始点位置
startPoint = event->pos();
this->pix->size() = this->size();
}
//绘图事件
void Widget::paintEvent(QPaintEvent *event)
{
//定义第二个画家
QPainter p2(this);
//将p1的图❀到桌面上
p2.drawPixmap(this->rect(),*pix);
}
效果图文章来源:https://www.toymoban.com/news/detail-485642.html
文章来源地址https://www.toymoban.com/news/detail-485642.html
到了这里,关于QT day4 (time/tcp/draw)的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!