整合营销服务商

电脑端+手机端+微信端=数据同步管理

免费咨询热线:

Qt中插入html样式

Qt中插入html样式

t中引入html调节样式

HTML

  • 设置行间距字体高度和颜色
<html><head/><body><p style=\"height:16px;line-height:24px;color:#787878\"> helloWorld</p></body></html>

技巧

  • 实现Html链接动态下划线
    • 继承QLabel并重写进入离开事件(对Html文字链接中text-decoration: none;属性进行控制). 即可实现鼠标停留时显示下划线, 鼠标离开时隐藏下划线.

、前言

这个IP地址输入框控件,估计写烂了,网上随便一搜索,保证一大堆,估计也是因为这个控件太容易了,非常适合新手练手,一般的思路都是用4个qlineedit控件拼起来,然后每个输入框设置正则表达式过滤只能输入3位数字,然后安装事件过滤器识别回车自动跳到下一个输入框。关于如何设置正则表达式过滤,这个可以搜索查到,本人也不大懂这个规则,貌似还有专门的书籍专门介绍正则表达式,可能这块非常强大。

开源地址:https://gitee.com/feiyangqingyun/QWidgetDemo https://github.com/feiyangqingyun/QWidgetDemo

二、实现的功能

  • 1:可设置IP地址,自动填入框
  • 2:可清空IP地址
  • 3:支持按下小圆点自动切换
  • 4:支持退格键自动切换
  • 5:支持IP地址过滤
  • 6:可设置背景色/边框颜色/边框圆角角度

三、效果图

四、头文件代码

#ifndef IPADDRESS_H
#define IPADDRESS_H

/**
 * IP地址输入框控件 作者:feiyangqingyun(QQ:517216493) 2017-8-11
 * 1:可设置IP地址,自动填入框
 * 2:可清空IP地址
 * 3:支持按下小圆点自动切换
 * 4:支持退格键自动切换
 * 5:支持IP地址过滤
 * 6:可设置背景色/边框颜色/边框圆角角度
 */

#include <QWidget>

class QLabel;
class QLineEdit;

#ifdef quc
#if (QT_VERSION < QT_VERSION_CHECK(5,7,0))
#include <QtDesigner/QDesignerExportWidget>
#else
#include <QtUiPlugin/QDesignerExportWidget>
#endif

class QDESIGNER_WIDGET_EXPORT IPAddress : public QWidget
#else
class IPAddress : public QWidget
#endif

{
    Q_OBJECT
    Q_PROPERTY(QString ip READ getIP WRITE setIP)

public:
    explicit IPAddress(QWidget *parent=0);

protected:
    bool eventFilter(QObject *watched, QEvent *event);

private:
    QLabel *labDot1;    //第一个小圆点
    QLabel *labDot2;    //第二个小圆点
    QLabel *labDot3;    //第三个小圆点

    QLineEdit *txtIP1;  //IP地址网段输入框1
    QLineEdit *txtIP2;  //IP地址网段输入框2
    QLineEdit *txtIP3;  //IP地址网段输入框3
    QLineEdit *txtIP4;  //IP地址网段输入框4

    QString ip;         //IP地址
    QString bgColor;    //背景颜色
    QString borderColor;//边框颜色
    int borderRadius;   //边框圆角角度

private slots:
    void textChanged(const QString &text);

public:
    //获取IP地址
    QString getIP()                 const;

    QSize sizeHint()                const;
    QSize minimumSizeHint()         const;

public Q_SLOTS:
    //设置IP地址
    void setIP(const QString &ip);
    //清空
    void clear();

    //设置背景颜色
    void setBgColor(const QString &bgColor);
    //设置边框颜色
    void setBorderColor(const QString &borderColor);
    //设置边框圆角角度
    void setBorderRadius(int borderRadius);

};

#endif // IPADDRESS_H


【领QT开发教程学习资料,点击下方链接莬费领取↓↓,先码住不迷路~】

点击这里:Qt资料领取(视频教程+文档+代码+项目实战)

五、核心代码

#pragma execution_character_set("utf-8")

#include "ipaddress.h"
#include "qlabel.h"
#include "qlineedit.h"
#include "qboxlayout.h"
#include "qregexp.h"
#include "qvalidator.h"
#include "qevent.h"
#include "qdebug.h"

IPAddress::IPAddress(QWidget *parent) : QWidget(parent)
{
    bgColor="#FFFFFF";
    borderColor="#A6B5B8";
    borderRadius=3;

    //用于显示小圆点的标签,居中对齐
    labDot1=new QLabel;
    labDot1->setAlignment(Qt::AlignCenter);
    labDot1->setText(".");

    labDot2=new QLabel;
    labDot2->setAlignment(Qt::AlignCenter);
    labDot2->setText(".");

    labDot3=new QLabel;
    labDot3->setAlignment(Qt::AlignCenter);
    labDot3->setText(".");

    //用于输入IP地址的文本框,居中对齐
    txtIP1=new QLineEdit;
    txtIP1->setObjectName("txtIP1");
    txtIP1->setAlignment(Qt::AlignCenter);
    txtIP1->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);
    connect(txtIP1, SIGNAL(textChanged(QString)), this, SLOT(textChanged(QString)));

    txtIP2=new QLineEdit;
    txtIP2->setObjectName("txtIP2");
    txtIP2->setAlignment(Qt::AlignCenter);
    txtIP2->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);
    connect(txtIP2, SIGNAL(textChanged(QString)), this, SLOT(textChanged(QString)));

    txtIP3=new QLineEdit;
    txtIP3->setObjectName("txtIP3");
    txtIP3->setAlignment(Qt::AlignCenter);
    txtIP3->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);
    connect(txtIP3, SIGNAL(textChanged(QString)), this, SLOT(textChanged(QString)));

    txtIP4=new QLineEdit;
    txtIP4->setObjectName("txtIP4");
    txtIP4->setAlignment(Qt::AlignCenter);
    txtIP4->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);
    connect(txtIP4, SIGNAL(textChanged(QString)), this, SLOT(textChanged(QString)));

    //设置IP地址校验过滤
    QRegExp regExp("(2[0-5]{2}|2[0-4][0-9]|1?[0-9]{1,2})");
    QRegExpValidator *validator=new QRegExpValidator(regExp, this);
    txtIP1->setValidator(validator);
    txtIP2->setValidator(validator);
    txtIP3->setValidator(validator);
    txtIP4->setValidator(validator);

    //绑定事件过滤器,识别键盘按下
    txtIP1->installEventFilter(this);
    txtIP2->installEventFilter(this);
    txtIP3->installEventFilter(this);
    txtIP4->installEventFilter(this);

    QFrame *frame=new QFrame;
    frame->setObjectName("frameIP");

    QStringList qss;
    qss.append(QString("QFrame#frameIP{border:1px solid %1;border-radius:%2px;}").arg(borderColor).arg(borderRadius));
    qss.append(QString("QLabel{min-width:15px;background-color:%1;}").arg(bgColor));
    qss.append(QString("QLineEdit{background-color:%1;border:none;}").arg(bgColor));
    qss.append(QString("QLineEdit#txtIP1{border-top-left-radius:%1px;border-bottom-left-radius:%1px;}").arg(borderRadius));
    qss.append(QString("QLineEdit#txtIP4{border-top-right-radius:%1px;border-bottom-right-radius:%1px;}").arg(borderRadius));
    frame->setStyleSheet(qss.join(""));

    QVBoxLayout *verticalLayout=new QVBoxLayout(this);
    verticalLayout->setMargin(0);
    verticalLayout->setSpacing(0);
    verticalLayout->addWidget(frame);

    //将控件按照横向布局排列
    QHBoxLayout *layout=new QHBoxLayout(frame);
    layout->setMargin(0);
    layout->setSpacing(0);
    layout->addWidget(txtIP1);
    layout->addWidget(labDot1);
    layout->addWidget(txtIP2);
    layout->addWidget(labDot2);
    layout->addWidget(txtIP3);
    layout->addWidget(labDot3);
    layout->addWidget(txtIP4);
}

bool IPAddress::eventFilter(QObject *watched, QEvent *event)
{
    if (event->type()==QEvent::KeyPress) {
        QLineEdit *txt=(QLineEdit *)watched;
        if (txt==txtIP1 || txt==txtIP2 || txt==txtIP3 || txt==txtIP4) {
            QKeyEvent *key=(QKeyEvent *)event;

            //如果当前按下了小数点则移动焦点到下一个输入框
            if (key->text()==".") {
                this->focusNextChild();
            }

            //如果按下了退格键并且当前文本框已经没有了内容则焦点往前移
            if (key->key()==Qt::Key_Backspace) {
                if (txt->text().length() <=1) {
                    this->focusNextPrevChild(false);
                }
            }
        }
    }

    return QWidget::eventFilter(watched, event);
}

void IPAddress::textChanged(const QString &text)
{
    int len=text.length();
    int value=text.toInt();

    //判断当前是否输入完成一个网段,是的话则自动移动到下一个输入框
    if (len==3) {
        if (value >=100 && value <=255) {
            this->focusNextChild();
        }
    }

    //拼接成完整IP地址
    ip=QString("%1.%2.%3.%4").arg(txtIP1->text()).arg(txtIP2->text()).arg(txtIP3->text()).arg(txtIP4->text());
}

QString IPAddress::getIP() const
{
    return this->ip;
}

QSize IPAddress::sizeHint() const
{
    return QSize(250, 20);
}

QSize IPAddress::minimumSizeHint() const
{
    return QSize(30, 10);
}

void IPAddress::setIP(const QString &ip)
{
    //先检测IP地址是否合法
    QRegExp regExp("((2[0-4]\\d|25[0-5]|[01]?\\d\\d?)\\.){3}(2[0-4]\\d|25[0-5]|[01]?\\d\\d?)");
    if (!regExp.exactMatch(ip)) {
        return;
    }

    if (this->ip !=ip) {
        this->ip=ip;

        //将IP地址填入各个网段
        QStringList list=ip.split(".");
        txtIP1->setText(list.at(0));
        txtIP2->setText(list.at(1));
        txtIP3->setText(list.at(2));
        txtIP4->setText(list.at(3));
    }
}

void IPAddress::clear()
{
    txtIP1->clear();
    txtIP2->clear();
    txtIP3->clear();
    txtIP4->clear();
    txtIP1->setFocus();
}

void IPAddress::setBgColor(const QString &bgColor)
{
    if (this->bgColor !=bgColor) {
        this->bgColor=bgColor;
    }
}

void IPAddress::setBorderColor(const QString &borderColor)
{
    if (this->borderColor !=borderColor) {
        this->borderColor=borderColor;
    }
}

void IPAddress::setBorderRadius(int borderRadius)
{
    if (this->borderRadius !=borderRadius) {
        this->borderRadius=borderRadius;
    }
}


六、控件介绍

  1. 超过160个精美控件,涵盖了各种仪表盘、进度条、进度球、指南针、曲线图、标尺、温度计、导航条、导航栏,flatui、高亮按钮、滑动选择器、农历等。远超qwt集成的控件数量。
  2. 每个类都可以独立成一个单独的控件,零耦合,每个控件一个头文件和一个实现文件,不依赖其他文件,方便单个控件以源码形式集成到项目中,较少代码量。qwt的控件类环环相扣,高度耦合,想要使用其中一个控件,必须包含所有的代码。
  3. 全部纯Qt编写,QWidget+QPainter绘制,支持Qt4.6到Qt5.13的任何Qt版本,支持mingw、msvc、gcc等编译器,支持任意操作系统比如windows+linux+mac+嵌入式linux等,不乱码,可直接集成到Qt Creator中,和自带的控件一样使用,大部分效果只要设置几个属性即可,极为方便。
  4. 每个控件都有一个对应的单独的包含该控件源码的DEMO,方便参考使用。同时还提供一个所有控件使用的集成的DEMO。
  5. 每个控件的源代码都有详细中文注释,都按照统一设计规范编写,方便学习自定义控件的编写。
  6. 每个控件默认配色和demo对应的配色都非常精美。
  7. 超过130个可见控件,6个不可见控件。
  8. 部分控件提供多种样式风格选择,多种指示器样式选择。
  9. 所有控件自适应窗体拉伸变化。
  10. 集成自定义控件属性设计器,支持拖曳设计,所见即所得,支持导入导出xml格式。
  11. 自带activex控件demo,所有控件可以直接运行在ie浏览器中。
  12. 集成fontawesome图形字体+阿里巴巴iconfont收藏的几百个图形字体,享受图形字体带来的乐趣。
  13. 所有控件最后生成一个动态库文件(dll或者so等),可以直接集成到qtcreator中拖曳设计使用。
  14. 目前已经有qml版本,后期会考虑出pyqt版本,如果用户需求量很大的话。
  15. 自定义控件插件开放动态库使用(永久免费),无任何后门和限制,请放心使用。
  16. 目前已提供32个版本的dll,其中qt_5_7_0_mingw530_32这个版本会一直保证最新的完整的。
  17. 不定期增加控件和完善控件,不定期更新SDK,欢迎各位提出建议,谢谢!
  18. Qt入门书籍推荐霍亚飞的《Qt Creator快速入门》《Qt5编程入门》,Qt进阶书籍推荐官方的《C++ GUI Qt4编程》。
  19. 强烈推荐程序员自我修养和规划系列书《大话程序员》《程序员的成长课》《解忧程序员》,受益匪浅,受益终生!
  20. SDK地址:https://gitee.com/feiyangqingyun/QUCSDK https://github.com/feiyangqingyun/qucsdk

原文链接:https://www.cnblogs.com/feiyangqingyun/p/11665022.html

【领QT开发教程学习资料,点击下方链接莬费领取↓↓,先码住不迷路~】

点击这里:「链接」

i,今天用一个小例子,陈述一下 Qt 里使用 TCP 通讯的流程。

代码链接:

https://doc.qt.io/qt-5/examples-network.html

运行效果:


Server 端

运行效果:

显示 IP + 端口,然后静静地的等待客户端的连接。

源码文件:

msg_server/
├── msg_server.pro
├── main.cpp
├── server.cpp
└── server.h

源码分析如下。


创建 TCP Server

在构造函数中进行初始化:

// server.cpp
Server::Server(QWidget *parent)
    : QDialog(parent)
    , statusLabel(new QLabel)
{
    // 建立 TCP Server,并监听
    tcpServer=new QTcpServer(this);
    tcpServer->listen()
    
    // 获取 Server 的 IP 地址,并用其初始化 UI
    [...]

    // 一旦有 TCP 连接,则调用 sendMsg() 发送数据给客户端
    connect(tcpServer, &QTcpServer::newConnection, this, &Server::sendMsg);
}

要点:

1、QTcpServer 是对 TCP-based server 的封装。

2、QTcpServer::listen() 用于监听是否有客户端发起连接。

3、一旦有客户端访问,QTcpServer 会发出 newConnection() 信号,我们通过绑定槽函数 sendMsg() 以实现发送消息的功能。

嵌入式物联网需要学的东西真的非常多,千万不要学错了路线和内容,导致工资要不上去!

无偿分享大家一个资料包,差不多150多G。里面学习内容、面经、项目都比较新也比较全!某鱼上买估计至少要好几十。

点击这里找小助理0元领取:嵌入式物联网学习资料(头条)




获取 Server IP

在界面上显示服务端的 IP 信息:

// server.cpp
Server::Server(QWidget *parent)
    : QDialog(parent)
    , statusLabel(new QLabel)
{
    // 创建 TCP Server
    [...]

    QString ipAddress;

    // 获得所有的 IP 地址
    QList<QHostAddress> ipAddressesList=QNetworkInterface::allAddresses();

    // 解析出第一个可用的 IPv4 地址
    for (int i=0; i < ipAddressesList.size(); ++i) {
        if (ipAddressesList.at(i) !=QHostAddress::LocalHost &&
            ipAddressesList.at(i).toIPv4Address()) {
            ipAddress=ipAddressesList.at(i).toString();
            break;
        }
    }

    // 初始化 UI
    [...]
}

要点:

1、QNetworkInterface 是对网络接口 (例如 lo、eth0...) 的封装。

2、QNetworkInterface::allAddresses() 会返回系统里所有的 IP 地址。

3、QHostAddress 是对 IP 地址(IPv4、IPv6) 的封装。

4、QHostAddress::toIPv4Address() 会将点分式的 IPv4 地址转换为数字式,例如 127.0.0.1 会被转换为 0x7F000001,失败则返回 0。


给客户端发送消息

当有客户端连接到来时,槽函数 sendMsg()会被调用 :

// server.cpp
void Server::sendMsg()
{
    // Prepare message
    QByteArray block;
    QDataStream out(&block, QIODevice::WriteOnly);
    out << message[QRandomGenerator::global()->bounded(message.size())];

    // Get pending connection
    QTcpSocket *clientConnection=tcpServer->nextPendingConnection();
    connect(clientConnection, &QAbstractSocket::disconnected,
            clientConnection, &QObject::deleteLater);

    // Send message
    clientConnection->write(block);
    clientConnection->disconnectFromHost();
}

要点:

1、QTcpSocket 是对 TCP Socket 的封装。

2、为了与主机无关 (字节序等),这里选用 QByteArray 以二进制的格式来存储数据。使用 QDataStream 可以轻松地将 Message 写到 QByteArray 里。

3、QDataStream 从各种 IO 设备 (QIODeice 的子类),例如 QByteArray、文件 (QFile) 等读写二进制数据。

4、从 QTcpServer::nextPendingConnection() 获得客户端的 Socket。

5、用 QTcpSocket::write() 将 Message 通过网络发给客户端。

6、最后,通过 QTcpSocket::disconnectFromHost 断开连接,它会等待直到数据成功被写出去。


Client 端

运行效果:

每次点击 "Get Message" 按钮,客户端都会从服务端随机获取到一条问候信息。

源码文件:

msg_client/
├── msg_client.pro
├── client.cpp
├── client.h
└── main.cpp

创建 TCP Socket

// client.cpp
Client::Client(QWidget *parent)
    : QDialog(parent)
    , hostCombo(new QComboBox)
    , portLineEdit(new QLineEdit)
    , statusLabel(new QLabel(tr("This examples requires that you run the\n Message Server example as well.")))
    , getMsgButton(new QPushButton(tr("Get Message")))
    , tcpSocket(new QTcpSocket(this))
{
    // Init UI
    [...]

    // Setup QDataStream's source
    in.setDevice(tcpSocket);
    in.setVersion(QDataStream::Qt_5_10);

    // Setup signal & slot
    connect(getMsgButton, &QAbstractButton::clicked,
            this, &Client::requestNewMsg);
    connect(tcpSocket, &QIODevice::readyRead, this, &Client::readMsg);
}

要点:

1、用 QTcpSocket 创建 TCP Socket。

2、将 QDataStream 数据流的输入源设置为 Socket。

3、设置信号槽:当 Socket 有数据时,调用 readMsg() 将其读走。


从服务端读取消息

当用户点击 "Get Message" 按钮时,requestNewMsg() 会被调用

// client.cpp
void Client::requestNewMsg()
{
    getMsgButton->setEnabled(false);
    tcpSocket->abort();
    tcpSocket->connectToHost(hostCombo->currentText(),
                             portLineEdit->text().toInt());
}

要点:

1、QTcpSocket::connectToHost() 向服务端发起连接。

2、该函数没有返回值,是因为当有错误发生时,socket 会发出 error() 信号,我们可以通过绑定对应的槽函数进行错误处理。

3、成功连接后,服务端会随机发送一条信息过来,客户端接收到消息后,readMsg() 会被调用。

// client.cpp
void Client::readMsg()
{
    QString ;
    in >> nextFortune;
    statusLabel->setText(nextFortune);
    getMsgButton->setEnabled(true);
}

很简单的流操作,读到信息后,将其显示在界面上。


总结

用一张图总结一下 Qt TCP 通讯流程:


原文链接:https://mp.weixin.qq.com/s/aCyV4HBwzTUZbCrdKsLtBw

文章转载自:老吴嵌入式

文章来源于:一小例子,了解 TCP 通讯流程 | Qt 示例

原文链接:一小例子,了解 TCP 通讯流程 | Qt 示例

版权声明:本文来源于网络,免费传达知识,版权归原作者所有,如涉及作品版权问题,请联系我进行删除