【qtday5】作业

1.chatclient:

#ifndef WIDGET_H
#define WIDGET_H

#include <QWidget>
#include <QIcon>
#include <QPushButton>
#include <QLabel>
#include <QLineEdit>
namespace Ui {
class Widget;
}

class Widget : public QWidget
{
    Q_OBJECT

public:
    explicit Widget(QWidget *parent = nullptr);
    ~Widget();

private:
    Ui::Widget *ui;
};

#endif // WIDGET_H

widget.cpp:

#include <QDebug>
#include "widget.h"
#include "ui_widget.h"

Widget::Widget(QWidget *parent) :
    QWidget(parent)
{
//    qDebug()<<"hello world";//类似于cout的使用
//    qDebug("%s","hello world");//类似于printf
    qDebug()<<this->size();     //输出当前界面的尺寸,该函数是父类提供的,直接用即可
    qDebug()<<this->frameSize();//实际大小,不包括边框
    qDebug()<<this->rect().size();//当前组件所占用矩形框的尺寸
    //更改界面大小
    this->resize(800,600);//法一
    this->resize(QSize(400,300));//法二

    //设置最大尺寸
    this->setMaximumSize(1000,800);
    //设置最小尺寸
    this->setMinimumSize(300,200);
    //设置固定尺寸
    this->setFixedSize(500,500);

    //去掉组件的上部头
//    this->setWindowFlag(Qt::FramelessWindowHint);

    //设置窗口标题
    this->setWindowTitle("ZJH");
    //获取窗口标题
    qDebug()<<this->windowTitle();

    //设置窗口图标
    this->setWindowIcon(QIcon("C:\\Users\\15082\\Documents\\Tencent Files\\1508233581\\FileRecv\\icon_z8w8m9orsdk\\icon_z8w8m9orsdk\\QQ.png"));

    //使用样式表更改颜色
    this->setStyleSheet("background-color:pink; color:red;");

//    //设置窗口透明度
//    this->setWindowOpacity(0.9);

//    //移动窗口位置
//    this->move(30,50);

    /*****以上是关于窗口界面的代码************/
    //使用按钮的构造函数设置一个按钮
    QPushButton *btn1 = new QPushButton;
    //设置父组件
    btn1->setParent(this);//将当前界面设置成父组件
    btn1->setText("按钮1");
    btn1->resize(100,40);
    btn1->move(200,0);
    btn1->setIcon(QIcon("E:\\denglu_1.png"));
    //设置样式表
    btn1->setStyleSheet("background-color:white; border-radius:5px;");

    //构造时设置父组件
    QPushButton *btn2 = new QPushButton(this);
    btn2->setText("按钮2");
    btn2->resize(btn1->size());
    btn2->move(200,60);
    btn2->setStyleSheet("background-color:white; border-radius:5px;");
    //设置不可用状态
    btn2->setEnabled(false);
    btn2->setIcon(QIcon("E:\\denglu_1.png"));

    //构造时指定文本内容和父组件
    QPushButton *btn3 = new QPushButton("按钮3",this);
    btn3->resize(btn1->size());
    btn3->move(200,120);
    btn3->setStyleSheet("background-color:white; border-radius:5px;");
    btn3->setIcon(QIcon("E:\\denglu_1.png"));

    QPushButton *btn4 = new QPushButton(QIcon("E:\\denglu_1.png"),"按钮4",this);
    btn4->resize(btn1->size());
    btn4->move(200,180);

    /*****************************************************/
    //构造标签并指定父组件
    QLabel *labl = new QLabel(this);
    //给标签设置样式表
    labl->setStyleSheet("background-color:skyblue;");
    labl->resize(50,30);
    labl->setText("账户:");
    labl->move(0,100);

    QLabel *lab2 = new QLabel("密码:",this);
    lab2->resize(labl->size());
    lab2->move(0,140);
    //更改标签中的内容
    lab2->setPixmap(QPixmap("E:\\denglu_1.png"));
    //设置内容为自适应
    lab2->setScaledContents(true);
    /*****************************************************/
    QLineEdit *edit1 = new QLineEdit(this);
    //重新设置大小
    edit1->resize(120,30);
    edit1->move(50,100);
    //设置样式表
    edit1->setStyleSheet("background-color:white;border:none;");
    //设置占位文本
    edit1->setPlaceholderText("QQ号/微信/邮箱");

    QLineEdit *edit2 = new QLineEdit("密码",this);
    edit2->resize(edit1->size());
    edit2->move(50,140);
    //获取文本框内容
    qDebug()<<edit2->text();
    edit2->setEchoMode(QLineEdit::Password);
}

Widget::~Widget()
{
}

main.cpp

#include "widget.h"
#include <QApplication>

int main(int argc, char *argv[])
{
    QApplication a(argc, argv);
    Widget w;
    w.show();

    return a.exec();
}

2.chatserver:
widget.h:

#ifndef WIDGET_H
#define WIDGET_H

#include <QWidget>
#include <QTcpServer>
#include <QTcpSocket>
#include <QVector>
#include <QMessageBox>

namespace Ui {
class Widget;
}

class Widget : public QWidget
{
    Q_OBJECT

public:
    explicit Widget(QWidget *parent = nullptr);
    ~Widget();

private slots:
    void on_pushButton_clicked();

private:
    Ui::Widget *ui;
    QTcpServer *server;
    QVector<QTcpSocket *> clientVector;
    void newconnection_slot();
    void readyRead_slot();
};

#endif // WIDGET_H

widget.cpp:

#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_pushButton_clicked()
{
    //获取ui界面上的端口号
    quint16 port = ui->lineEdit->text().toUInt();
    //将服务器设置成监听状态
    //参数1:要监听的主机地址,如果是any,则表明监听任意主机地址,也可以给定监听特定的地址
    //参数2:接入的端口号,如果为0,表明端口号由系统自动分配,一般手动提供
    //返回值:成功返回true,失败返回false
    if(server->listen(QHostAddress::Any,port)){
        QMessageBox::information(this,"成功","打开服务器成功");
    }else {
        QMessageBox::information(this,"失败","打开服务器失败");
        return;
    }
    //如果有客户端发来链接请求,那么该服务器就会自动触发一个newconnection的信号
    //我们可以将该信号链接到自定义的槽函数中处理相关逻辑
    connect(server,&QTcpServer::newConnection,this,&Widget::newconnection_slot);
}

void Widget::newconnection_slot()
{
    // 获取最新连接的客户端套接字
    QTcpSocket *s = server->nextPendingConnection();

    //将该套接字放入套接字容器中
    clientVector.push_back(s);

    //客户端发数据会发射一个readyRead信号
    //连接到自定义的槽函数
    connect(s,&QTcpSocket::readyRead,this,&Widget::readyRead_slot);
}

void Widget::readyRead_slot()
{
    //删除无效的客户端
    for (int i = 0;i<clientVector.size();i++) {
        if(clientVector[i]->state()==0)
        {
            clientVector.removeAt(i);
        }
    }
    //判断是哪个客户端发来的数据
    for (int i = 0;i<clientVector.size();i++) {
        //判断当前的套接字中是否有数据待读
        if(clientVector[i]->bytesAvailable()!=0){
            //读取当前套接字中的数据
            QByteArray msg = clientVector[i]->readAll();
            ui->listWidget->addItem(QString::fromLocal8Bit(msg));
            //将获取的该套接字中的数据广播给所有客户端
            for (int j = 0;j<clientVector.size();j++) {
                clientVector[j]->write(msg);
            }
        }
    }
}

main.cpp:

#include "widget.h"
#include <QApplication>

int main(int argc, char *argv[])
{
    QApplication a(argc, argv);
    Widget w;
    w.show();

    return a.exec();
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值