QT应用编程: 开发TCP网络调试助手

论坛 期权论坛     
选择匿名的用户   2021-5-21 15:29   54   0
<h1><span style="color:#f33b45;">一、环境介绍</span></h1>
<p><span style="color:#3399ea;"><strong>操作系统:</strong> </span>win10 64位</p>
<p><span style="color:#3399ea;"><strong>QT版本: </strong></span> QT5.12.6</p>
<p><span style="color:#3399ea;"><strong>编译器:</strong> </span> MinGW 32</p>
<p>TCP网络调试助手,在网络相关开发、嵌入式开发里使用较多,主要用来测试网络传输,调试等等。</p>
<p>这个版本设计的比较早,没有加入多线程处理连接上来的客户端,一般的调试场景已经满足。对于基本的QT网络开发入门学习借鉴已经足够了。</p>
<p><span style="color:#f33b45;"><strong>软件下载:</strong></span>  <a href="https://download.csdn.net/download/xiaolong1126626497/17044958">https://download.csdn.net/download/xiaolong1126626497/17044958</a></p>
<p><strong><span style="color:#f33b45;">工程完整源代码下载:</span></strong>  <a href="https://download.csdn.net/download/xiaolong1126626497/17058921">https://download.csdn.net/download/xiaolong1126626497/17058921</a></p>
<h1><span style="color:#f33b45;">二、效果图</span></h1>
<p><img alt="" height="1080" src="https://beijingoptbbs.oss-cn-beijing.aliyuncs.com/cs/5606289-a50c1f23db87c9a7bee1205b2055a0b3.png" width="1200"></p>
<p><img alt="" height="1080" src="https://beijingoptbbs.oss-cn-beijing.aliyuncs.com/cs/5606289-c64b2cb3556ce59d420aaaa261b1f83a.png" width="1200"></p>
<h1><span style="color:#f33b45;">三、源代码</span></h1>
<h2><span style="color:#3399ea;">3.1 widget.cpp</span></h2>
<pre class="blockcode"><code class="language-cpp">#include &#34;widget.h&#34;
#include &#34;ui_widget.h&#34;

Widget::Widget(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::Widget)
{
    ui-&gt;setupUi(this);
    setWindowIcon(QIcon(&#34;:/image/14g.png&#34;)); //设置图标
    SetStyle(&#34;:/qss/blue.css&#34;); //设置样式表
    setWindowTitle(&#34;TCP网络调试助手_v2.2 2020年7月11日&#34;);
    comboBox_config();

    timer &#61; new QTimer(this); //创建定时器
    connect(timer, SIGNAL(timeout()), this, SLOT(update())); //关联槽函数

    /*控件名字设置*/
    ui-&gt;checkBox_ShowTime-&gt;setText(&#34;接收时间显示&#34;);
    ui-&gt;checkBox_TxBit-&gt;setText(&#34;自动发送附加位\\r\\n&#34;);
    ui-&gt;checkBox_LoopTx-&gt;setText(&#34;循环发送数据&#34;);
    ui-&gt;label_4-&gt;setText(&#34;设置循环间隔ms时间:&#34;);
    ui-&gt;pushButton_connect-&gt;setText(&#34;连接&#34;);
    ui-&gt;pushButton_SendData-&gt;setText(&#34;发送&#34;);
    ui-&gt;About-&gt;setText(&#34;帮助&#34;);
    ui-&gt;label-&gt;setText(&#34;1.协议类型&#34;);
    ui-&gt;label_ip-&gt;setText(&#34;2.本地IP地址&#34;);
    ui-&gt;label_prot-&gt;setText(&#34;3.本地端口&#34;);
    ui-&gt;label_SendCnt-&gt;setText(&#34;TX&#34;);
    ui-&gt;label_RxCnt-&gt;setText(&#34;RX&#34;);
    ui-&gt;pushButton_clear-&gt;setText(&#34;复位&#34;);
    ui-&gt;plainTextEdit_ShowData-&gt;setReadOnly(true);
    ui-&gt;pushButton_cleanShow-&gt;setText(&#34;清除显示&#34;);
}

//设置指定样式
void Widget::SetStyle(const QString &amp;qssFile) {
    QFile file(qssFile);
    if (file.open(QFile::ReadOnly)) {
        QString qss &#61; QLatin1String(file.readAll());
        qApp-&gt;setStyleSheet(qss);
        QString PaletteColor &#61; qss.mid(20, 7);
        qApp-&gt;setPalette(QPalette(QColor(PaletteColor)));
        file.close();
    }
}


//基本配置
void Widget::comboBox_config()
{
    /*第一部分:网络设置*/
    //1.1 配置协议
    ui-&gt;comboBox_protocol-&gt;addItem(&#34;TCP Server&#34;);
    ui-&gt;comboBox_protocol-&gt;addItem(&#34;TCP Client&#34;);
    //1.2. 获取并配置本地IP地址
    QList&lt;QHostAddress&gt; list &#61; QNetworkInterface::allAddresses();
    for(int i&#61;0;i&lt;list.count();i&#43;&#43;)
    {
        QHostAddress addr&#61;list.at(i);
        if(addr.protocol() &#61;&#61; QAbstractSocket::IPv4Protocol)
        {
          ui-&gt;comboBox_ipaddress-&gt;addItem(addr.toString());
        }
    }
   //1.3 设置可编辑性
    ui-&gt;comboBox_ipaddress-&gt;setEditable(true);
   //1.4. 设置默认端口号
    ui-&gt;lineEdit_prot-&gt;setText(&#34;8080&#34;);

    /*第二部分:发送设置*/
    //2.1设置循环发送时间
    ui-&gt;lineEdi_LoopTime-&gt;setText(&#34;1000&#34;);
    //2.2 设置默认发送的内容
    ui-&gt;lineEdit_InputTxDtat-&gt;setText(&#34;https://blog.csdn.net/xiaolong1126626497&#34;);

    //接收数量置0
    TxCount&#61;0;
    RxCount&#61;0;

    /*第三部分:选择客户端时用于显示本地端口和IP*/
    ClientShowLocalProtLabel &#61;new QLabel(&#34;本地端口:&#34;);
    ClientShowLocalIptLabel &#61;new QLabel(&#34;本地IP地址:&#34;);
    ClientEditProt&#61; new QLineEdit;
    ClientEditIP&#61; new QLineEdit;

    ClientLayout &#61; new QHBoxLayout;
    ClientLayout-&gt;addWidget(ClientShowLocalIptLabel);
    ClientLayout-&gt;addWidget(ClientEditIP);
    ClientLayout-&gt;addWidget(ClientShowLocalProtLabel);
    ClientLayout-&gt;addWidget(ClientEditProt);

    /*第四部分:选服务器时用于显示已经连接的客户端*/
    SaverShowLocalProtLabel &#61;new QLabel(&#34;已连接的客户端列表:&#34;);
    ClientComboBoxList&#61;new QComboBox;
    ClientComboBoxList-&gt;setMinimumWidth(180);
    ServerLayout &#61; new QHBoxLayout;
    Serve
分享到 :
0 人收藏
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

积分:3875789
帖子:775174
精华:0
期权论坛 期权论坛
发布
内容

下载期权论坛手机APP