QT程序代码

QT
控件的布局

#ifndef WIDGET_H
#define WIDGET_H

#include 
#include 
#include 
#include 

class Widget : public QWidget
{
    Q_OBJECT


public slots:

    void movestr()
    {

        ld1->setText(ld->text());
    }





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



private:


    QPushButton *pd;
    QLineEdit *ld,*ld1;
    QVBoxLayout *vx;







};
#endif // WIDGET_H

#include "widget.h"
#include "./ui_widget.h"

Widget::Widget(QWidget *parent)

{

    pd=new QPushButton("button",this);
    ld=new QLineEdit(this);
    ld1=new QLineEdit(this);
    pd->setGeometry(0,100,50,50);


    vx=new QVBoxLayout(this);
    vx->addWidget(pd);
    vx->addWidget(ld);
    vx->addWidget(ld1);

    this->setLayout(vx);

   //connect(pd,SIGNAL(click),ld,SLOT(clear));

   connect(pd,SIGNAL(clicked(bool)),this,SLOT(movestr()));

}

Widget::~Widget()
{

}


浏览图片的布局

#ifndef WIDGET_H
#define WIDGET_H

#include ]
#include
#include
#include
#include
#include

class Widget : public QWidget
{
    Q_OBJECT

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

public slots:
    void openfile(){
        index=0;
        files=QFileDialog::getOpenFileNames();
        QPixmap pix(files[index]);
        lb->setPixmap(pix);


    }

    void showr()
    {
        if(index+1setPixmap(pix);
    }

    void showl()
    {
        if(index-1>0)
            index--;
        else
            index=files.length()-1;
        QPixmap pix(files[index]);
        lb->setPixmap(pix);
    }



private:

    QLabel *lb;
    QPushButton *pb1,*pb2,*pb3;
    QStringList files;
    int index;
};
#endif // WIDGET_H

#include "widget.h"
#include
#include

Widget::Widget(QWidget *parent)
    : QWidget(parent)
{
    lb=new QLabel;
    lb->setMinimumSize(640,480);
    lb->setScaledContents(true); //设置图片自适应控件大小


    pb1=new QPushButton("<");
    pb2=new QPushButton("show");
    pb3=new QPushButton(">");

    QHBoxLayout *hbox=new QHBoxLayout;
    hbox->addStretch();
    hbox->addWidget(pb1);
    hbox->addWidget(pb2);
    hbox->addWidget(pb3);
    hbox->addStretch();

    QVBoxLayout *vbox=new QVBoxLayout;
    vbox->addWidget(lb);
    vbox->addLayout(hbox);

    this->setLayout(vbox);

    connect(pb2,SIGNAL(clicked(int)),this,SLOT(openfile()));
    connect(pb1,SIGNAL(clicked(int)),this,SLOT(showl));
    connect(pb3,SIGNAL(clicked(int)),this,SLOT(showr));



}

Widget::~Widget()
{
}


画笔的使用

#ifndef WIDGET_H
#define WIDGET_H

#include 
#include
#include
#include

class Widget : public QWidget
{
    Q_OBJECT

public:
    Widget(QWidget *parent = nullptr);
    ~Widget();
    void mousePressEvent(QMouseEvent *ev){
        startup=ev->pos();
        update();
    }

    void mouseReleaseEvent(QMouseEvent *ev){
        saveeit=true;
        update();
    }

    void  mouseMoveEvent(QMouseEvent *ev)
    {
        endup=ev->pos();
        update();
    }


    void paintEvent(QPaintEvent *ev)

    {

        if(saveeit)
        {
           QPainter px(pix);
           px.drawLine(startup,endup);
        }
        QPainter p(this);
        p.drawPixmap(0,0,*pix);
        p.drawLine(startup,endup);

    }




private:
    QPoint endup;
    QPoint startup;
    QPixmap *pix;
    bool saveeit;

};
#endif // WIDGET_H

#include "widget.h"

Widget::Widget(QWidget *parent)
    : QWidget(parent)
{
      pix=new QPixmap;
     pix->fill();
}


Widget::~Widget()
{
}


表盘:

#ifndef WIDGET_H
#define WIDGET_H

#include 
#include
#include

class Widget : public QWidget
{
    Q_OBJECT

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


    void paintEvent(QPaintEvent *ev)
    {
        qDebug()<<"paint.....";
        QPainter p(this);
        p.translate(this->width()/2,this->height()/2);
        p.drawEllipse(QPoint(0,0),100,100);
        p.rotate(i++*10);
        p.drawLine(0,0,50,50);
    }

    void timerEvent(QTimerEvent *event)
    {
        update();
    }

private:
    int i=0;
};
#endif // WIDGET_H

#include "widget.h"

Widget::Widget(QWidget *parent)
    : QWidget(parent)
{
    i=0;
    startTimer(1000);
}

Widget::~Widget()
{
}


mouse事件:

#ifndef WIDGET_H
#define WIDGET_H

#include 
#include
#include
class Widget : public QWidget
{
    Q_OBJECT

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

    void mousePressEvent(QMouseEvent *ev)
    {
        qDebug()<<"press ....."<< ev->pos();
    }
    void mouseMoveEvent(QMouseEvent *ev)
    {

        qDebug()<<"move....."<pos();

    }

    void keyPressEvent(QKeyEvent *ev)
    {
        qDebug()<<"key press...."<key();

    }

    void wheelEvent(QWheelEvent *ev)
    {
        qDebug()<<"wheel........."<delta();
    }

    void timerEvent(QTimerEvent *ev)
    {
        qDebug()<<"timer!";
    }
};
#endif // WIDGET_H

#include "widget.h"

Widget::Widget(QWidget *parent)
    : QWidget(parent)
{

    startTimer(1000);
}

Widget::~Widget()
{
}


窗口:

#ifndef MAINWINDOW_H
#define MAINWINDOW_H

#include 
#include
#include
#include
#include
#include

class MainWindow : public QMainWindow
{
    Q_OBJECT

public:
    MainWindow(QWidget *parent = nullptr);
    ~MainWindow();

private:
    QAction *openfile;
    QAction *copyfile;
    QAction *phasefile;
    QAction *setcolor;

    QTextEdit *te;
    QLabel *lb;

public slots:
    void openf(){
        QString filename=QFileDialog::getOpenFileName();
        te->setText(filename);


    }

    void copy(){
        te->copy();
    }

    void paste()
    {
        te->paste();

    }

    void setcol()
    {
        QColor ccc=QColorDialog::getColor();
        te->setTextColor(ccc);
    }

};
#endif // MAINWINDOW_H

#include "mainwindow.h"
#include
#include
#include
#include


MainWindow::MainWindow(QWidget *parent)
    : QMainWindow(parent)
{
    openfile=new QAction(QIcon("open.jpg"),"open");
    openfile->setShortcut(QKeySequence(tr("Ctrl+P")));


    copyfile=new QAction(QIcon("open.jpg"),"copy");
    phasefile=new QAction(QIcon("open.jpg"),"phase");
    setcolor=new QAction(QIcon("open.jpg"),"set");





     QMenu *fileMenu = menuBar()->addMenu(tr("&file"));
      fileMenu->addAction(openfile);



      QMenu *editMenu=menuBar()->addMenu(tr("&edit"));
      editMenu->addAction(copyfile);
      editMenu->addAction(phasefile);

      QMenu *setMenu=menuBar()->addMenu(tr("&set"));
      setMenu->addAction(setcolor);

      QToolBar *tfile=addToolBar("file");
      tfile->addAction(openfile);

       QToolBar *tedit=addToolBar("edit");
       tedit->addAction(copyfile);
       tedit->addAction(phasefile);


       QToolBar *tset=addToolBar("set");
       tset->addAction(setcolor);


       te=new QTextEdit("hello");
       te->setMaximumSize(240,680);
       setCentralWidget(te);

       lb=new QLabel("1.txt");
       QStatusBar *sb=statusBar();
       sb->addWidget(lb);


       connect(openfile,SIGNAL(triggered(bool)),this,SLOT(openf()));
       connect(copyfile,SIGNAL(triggered(bool)),this,SLOT(copy()));
       connect(phasefile,SIGNAL(triggered(bool)),this,SLOT(paste()));
       connect(setcolor,SIGNAL(triggered(bool)),this,SLOT(setcol()));


}

MainWindow::~MainWindow()
{
}


dailog:

#ifndef WIDGET_H
#define WIDGET_H

#include 
#include
#include
#include
#include
#include
#include
#include
#include


#include
#include

class Widget : public QWidget
{
    Q_OBJECT
public slots:
    void setcolor(){
        QColor col=QColorDialog::getColor();
        te->setTextColor(col);
    }

    void error(){
        QErrorMessage *e=QErrorMessage::qtHandler();
        e->showMessage("eeeeeeeeeee");
    }

    void showfile(){
        QString filename=QFileDialog::getOpenFileName();
        qDebug()<setText(filename);
    }

     void setfond(){
         bool ok;
          QFont font = QFontDialog::getFont(&ok);
          if(ok)

           te->setFont(font);
        }

     void showput(){
          QString text = QInputDialog::getText(this,"xxxx","yyyy");
          te->setText(text);

     }

     void showmsg(){
         QMessageBox::information(this,"aaaaaaa","bbbb","bbbbbbbb");


     }

     void showprog(){
         QProgressDialog *p;
         p->setValue(60);
         p->exec();
     }


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

private:
    QPushButton *cd;
     QPushButton *fd;
     QPushButton *fid;
     QPushButton *mb;
     QPushButton*id;
     QPushButton *em;
     QPushButton *pd;

    QPushButton *pb;
    QTextEdit *te;


};
#endif // WIDGET_H

#include "widget.h"
#include
#include

Widget::Widget(QWidget *parent)
    : QWidget(parent)
{

    cd=new  QPushButton("color");
    fd=new  QPushButton("file");
    fid=new  QPushButton("font");
    mb=new  QPushButton("message");
    id=new  QPushButton("input");
    em=new  QPushButton("error");
    pd=new  QPushButton("progress");


    te=new QTextEdit;

    QVBoxLayout *vbox=new QVBoxLayout;
    vbox->addWidget(cd);
    vbox->addWidget(fd);
    vbox->addWidget(fid);
    vbox->addWidget(mb);
    vbox->addWidget(id);
    vbox->addWidget(em);
    vbox->addWidget(pd);



    QHBoxLayout *hbox=new QHBoxLayout;
    hbox->addLayout(vbox);
    hbox->addWidget(te);
    this->setLayout(hbox);

    connect(cd,SIGNAL(clicked(bool)),this,SLOT(setcolor()));
    connect(em,SIGNAL(clicked(bool)),this,SLOT(error()));
    connect(fd,SIGNAL(clicked(bool)),this,SLOT(showfile()));
    connect(fid,SIGNAL(clicked(bool)),this,SLOT(setfond()));
    connect(id,SIGNAL(clicked(bool)),this,SLOT(showput()));
    connect(mb,SIGNAL(clicked(bool)),this,SLOT(showmsg()));
    connect(pd,SIGNAL(clicked(bool)),this,SLOT(showprog()));

}

Widget::~Widget()
{
}


stackwidget:

#ifndef WIDGET_H
#define WIDGET_H

#include 
#include
#include
#include

class Widget : public QWidget
{
    Q_OBJECT

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

private:
    QStackedWidget *sw;
    QTextEdit *te1,*te2,*te3;
    QComboBox *cb;
};
#endif // WIDGET_H

#include "widget.h"
#include

Widget::Widget(QWidget *parent)
    : QWidget(parent)
{

    sw=new QStackedWidget;
    te1=new QTextEdit("1234567");
    te2=new QTextEdit("2345678");
    te3=new QTextEdit("22233344");

    sw->addWidget(te1);
    sw->addWidget(te2);
    sw->addWidget(te3);


    QComboBox *cb=new QComboBox;
    cb->addItem("111");
    cb->addItem("222");
    cb->addItem("333");


    QVBoxLayout *vbox=new QVBoxLayout;
    vbox->addWidget(sw);
    vbox->addWidget(cb);

    this->setLayout(vbox);

    connect(cb,SIGNAL(activatecd),sw,SLOT(setCurrentIndex(int)));



}

Widget::~Widget()
{
}


tablewidget

#ifndef WIDGET_H
#define WIDGET_H

#include 
#include
#include

class Widget : public QWidget
{
    Q_OBJECT

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

private:
    QTabWidget *td;
    QTextEdit *te1,*te2,*te3;
};
#endif // WIDGET_H

#include "widget.h"
#include

Widget::Widget(QWidget *parent)
    : QWidget(parent)
{

    td=new QTabWidget;
    te1=new QTextEdit("1111111111");
    te2=new QTextEdit("222222222222");
    te3=new QTextEdit("333333333");

    td->addTab(te1,"1.c");
    td->addTab(te2,"2.c");
    td->addTab(te3,"3.c");

    QVBoxLayout *vbox=new QVBoxLayout;
    vbox->addWidget(td);
    this->setLayout(vbox);



}

Widget::~Widget()
{
}


toolbox:

#ifndef WIDGET_H
#define WIDGET_H

#include 
#include
#include

class Widget : public QWidget
{
    Q_OBJECT

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

private:
    QToolBox *tb;
    QPushButton *pb[3];
};
#endif // WIDGET_H

#include "widget.h"
#include "QVBoxLayout"
Widget::Widget(QWidget *parent)
    : QWidget(parent)
{

    tb=new QToolBox;
    pb[0]=new QPushButton;
    pb[1]=new QPushButton;
    pb[2]=new QPushButton;

    tb->addItem(pb[0],"111");
    tb->addItem(pb[1],"222");
    tb->addItem(pb[2],"333");

    QVBoxLayout *vbox=new QVBoxLayout;
    vbox->addWidget(tb);
    this->setLayout(vbox);






}

Widget::~Widget()
{
}


lable:

#ifndef WIDGET_H
#define WIDGET_H

#include 
#include
#include
class Widget : public QWidget
{
    Q_OBJECT

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

private:
    QLabel *lb;
    QScrollArea *sa;
};
#endif // WIDGET_H

#include "widget.h"
#include
#include

Widget::Widget(QWidget *parent)
    : QWidget(parent)
{
    lb=new QLabel(this);
    QPixmap pix("1.png");
    lb->setPixmap(pix);
    sa=new QScrollArea(this);

    sa->setWidget(lb);

    QVBoxLayout *vbox=new QVBoxLayout(this);
    vbox->addWidget(sa);
    this->setLayout(vbox);

}

Widget::~Widget()
{
}


groupbox:

#ifndef WIDGET_H
#define WIDGET_H

#include 
#include
#include

class Widget : public QWidget
{
    Q_OBJECT

public:
    Widget(QWidget *parent = nullptr);
    ~Widget();
private:
    QGroupBox *gb;
    QRadioButton *r1,*r2,*r3;
};
#endif // WIDGET_H
  
#include "widget.h"
#include

Widget::Widget(QWidget *parent)
    : QWidget(parent)
{
    gb=new QGroupBox("chioces",this);

    r1=new QRadioButton("r1");
    r2=new QRadioButton("r2");
    r3=new QRadioButton("r3");

    QVBoxLayout *vbox=new QVBoxLayout;
    vbox->addWidget(r1);
    vbox->addWidget(r2);
    vbox->addWidget(r3);

    gb->setLayout(vbox);

    QVBoxLayout *mainbox=new QVBoxLayout;
    mainbox->addWidget(gb);
    this->setLayout(mainbox);

}

Widget::~Widget()
{
}


input-output

#ifndef WIDGET_H
#define WIDGET_H


//input 控件
#include 
#include
#include
#include
#include
#include
#include
#include


//output  控件
#include
#include
#include

class Widget : public QWidget
{
    Q_OBJECT

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


private:
    QLineEdit *le;
    QTextEdit *te;
    QComboBox  *cb;
    QTimeEdit  *td;
    QDial  *da;
    QSlider *sl;
    QScrollBar  *sb;


    QLabel *lb,*lb1;
    QLCDNumber  *lu;
    QProgressBar *pr;
};
#endif // WIDGET_H

#include "widget.h"
#include 
#include
#include

Widget::Widget(QWidget *parent)
    : QWidget(parent)
{

    le=new QLineEdit;
    te=new QTextEdit;
    cb=new QComboBox;
    cb->addItem("read");
    cb->addItem("write");

    td=new QTimeEdit;
    da=new QDial;
    sl=new QSlider;
    sl->setOrientation(Qt::Horizontal);
    sb=new QScrollBar;
    sb->setOrientation(Qt::Horizontal);
    QVBoxLayout *vbox1=new QVBoxLayout;
    vbox1->addWidget(le);
    vbox1->addWidget(te);
    vbox1->addWidget(cb);
    vbox1->addWidget(td);
    vbox1->addWidget(da);
    vbox1->addWidget(sl);
    vbox1->addWidget(sb);
    this->setLayout(vbox1);



    lb=new QLabel("hello");

    lb1=new QLabel;
    QPixmap pix("1.jpg");
    lb1->setFixedSize(222,333);
    lb1->setScaledContents(true);
    lb1->setPixmap(pix);
    lu=new QLCDNumber;
    pr=new QProgressBar;
   QVBoxLayout *vbox2=new QVBoxLayout;
   vbox2->addWidget(lb1);
   vbox2->addWidget(lb);
   vbox2->addWidget(lu);
   vbox2->addWidget(pr);
   this->setLayout(vbox2);

   QHBoxLayout *hbox=new QHBoxLayout;
   hbox->addLayout(vbox1);
   hbox->addLayout(vbox2);
   this->setLayout(hbox);

   connect(da,SIGNAL(valueChange(int)),lu,SLOT(display(int)));
   connect(da,SIGNAL(valueChange(int)),pr,SLOT(setvalue()));


}

Widget::~Widget()
{
}


layouttest:

#ifndef WIDGET_H
#define WIDGET_H

#include 
#include
#include


class Widget : public QWidget
{
    Q_OBJECT

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

private:
    QPushButton *pd;
    QLineEdit *le1,*le2;
};
#endif // WIDGET_H

#include "widget.h"
#include 
#include
#include
#include

Widget::Widget(QWidget *parent)
    : QWidget(parent)
{

    pd=new QPushButton("ok",this);
    le1=new QLineEdit(this);
    le2=new QLineEdit(this);

    QVBoxLayout *vbox=new QVBoxLayout;

    QHBoxLayout *hbox=new QHBoxLayout;

    QGridLayout *gbox=new QGridLayout;

    QFormLayout *fbox=new QFormLayout;

    fbox->addRow("user",le1);
    fbox->addRow("passwd",le2);
    fbox->addRow("",pd);
    this->setLayout(fbox);

#if 0
    vbox->addWidget(le1);
    vbox->addWidget(le2);
    vbox->addWidget(pd);

    this->setLayout(vbox);

#endif

#if 0

    hbox->addWidget(le1);
    hbox->addWidget(le2);
    hbox->addWidget(pd);

    this->setLayout(hbox);
#endif

#if 0
    gbox->addWidget(le1,0,0);
    gbox->addWidget(le2,1,1);
    gbox->addWidget(pd,2,2);

    this->setLayout(gbox);


#endif


}

Widget::~Widget()
{
}


button:

#ifndef WIDGET_H
#define WIDGET_H

#include 
#include
#include
#include

class Widget : public QWidget
{
    Q_OBJECT

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

public slots:
    void setpass(bool flag){
        if(!flag)
            le->setEchoMode(QLineEdit::Password);
        else
            le->setEchoMode(QLineEdit::Normal);
    }

private:
    QPushButton *pb;
    QLineEdit *le;
    QCheckBox *cb;

};
#endif // WIDGET_H

#include "widget.h"
#include

Widget::Widget(QWidget *parent)
    : QWidget(parent)
{

    pb=new QPushButton("clear");
    le=new QLineEdit();
   // le->setEchoMode(QLineEdit::Password);
    cb=new QCheckBox("show passwd");

    QVBoxLayout *vbox=new QVBoxLayout;
    vbox->addWidget(le);
    vbox->addWidget(cb);
    vbox->addWidget(pb);
    this->setLayout(vbox);


    connect(pb,SIGNAL(clicked(bool)),le,SLOT(clear()));
    connect(cb,SIGNAL(clicked(bool)),this,SLOT(setpass(bool)));

}

Widget::~Widget()
{
}


TCP server.h

#ifndef TCPSERVER_H
#define TCPSERVER_H

#include
#include
class tcpServer : public QObject
{
    Q_OBJECT
public:
    explicit tcpServer(QObject *parent = nullptr);

signals:

public slots:
    void newconnected()
    {
        qDebug()<<"connect";
       clientsock= ser->nextPeddingConnection();
       connect(clientsock,SIGNAL(readyRead()),this,SLOT(readdata()));
    }

    void readdata()
    {
        QByteArray buf=clientsock->readAll();
        qDebug()<<"recv"<write(buf);
    }

private:
    QTcpServer *ser;
    QTcpServer *clientsock;

};

#endif // TCPSERVER_H

TCP server.c

#include "tcpserver.h"
#include

tcpServer::tcpServer(QObject *parent) : QObject(parent)
{
    ser=new QTcpServer;
    ser->listen(QHostAddress::AnyIPv4,8888);
    connect(ser,SIGNAL(newconnected(),this,SLOT(newconnected())));
    ser->waitForNewConnectiion();
}

main.c

#include 

int main(int argc, char *argv[])
{
    QCoreApplication a(argc, argv);
    tcpServer tcpserver;
    tcpserver.init();

    return a.exec();
}

tcp client.h

#ifndef WIDGET_H
#define WIDGET_H

#include 
#include
#include
#include
#include

class Widget : public QWidget
{
    Q_OBJECT

public:
    Widget(QWidget *parent = nullptr);
    ~Widget();
    
public slots:
    void senddata()
    {
       tcpsocket->write(le->text().toStdString().c_str());
       
    }
    
    void receivedata()
    {
       QByteArry buf=tcpsocket->readAll();
       te->append(buf);
    }

private:
    QPushButton *pb;
    QTextEdit *te;
    QLineEdit *le;
    
    QTcpSocket *tcpsocket;
};
#endif // WIDGET_H

tcp client.cpp

#include "widget.h"
#include
Widget::Widget(QWidget *parent)
    : QWidget(parent)
{
    
    pb=new QPushButton;
    le=new QLineEdit;
    te=new QTextEdit;
    
    
    QVBoxLayout *vbox=new QVBoxLayout;
    vbox->addWidget(te);
    vbox->addWidget(le);
    vbox->addWidget(pb);
    
    tcpsocket=new QTcpSocket;
    this->setLayout(vbox);
    
    
    tcpsocket->connecToHost(QHostAddress("192.168.31.134"_,8888));
    connect(pb,SIGNAL(clicked(bool)),this,SLOT(sendata()));
    connect(tcpsocket,SIGNAL(readyRead()),this,SLOT(recivedata()));
    
}

Widget::~Widget()
{
}


udp client

#ifndef WIDGET_H
#define WIDGET_H

#include 
#include
#include
#include
#include           //需要加入 qmake: QT += network
#include
class Widget : public QWidget
{
    Q_OBJECT

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

public slots:
    void send()
    {
        udpsock->writeDatagram(le->text().toStdString().c_str(),QHostAdderss("192.168.31.134"),8888);

    }

    void recv()
    {
        QByteArray datagram;
        datagram.resize(udpsock->pendingDatagramSize());
        QHostAddress sender;
        quint16 senderPort;
        udpsock->readDatagram(datagram.data(),datagram.size(),&sender,&senderPort);
        te->append(datagram);
    }
private:
    QPushButton *pb;
    QLineEdit *le;
    QTextEdit *te;
    QUdpsocket *udpsock;
};
#endif // WIDGET_H

udp clinet .cpp

#include "widget.h"
#include
Widget::Widget(QWidget *parent)
    : QWidget(parent)
{

    pb=new QPushButton("button");
    le=new QLineEdit();
    te=new QTextEdit();


    QVBoxLayout *vbox=new QVBoxLayout;
    vbox->addWidget(te);
    vbox->addWidget(le);
    vbox->addWidget(pb);
    udpsock =new QUdpsocket;

    this->setLayout(vbox);

    connect(pb,SIGNAL(clicked(bool)),this,SLOT(send()));
    connect(udpsocket,SIGNAL(readyRead(),this,SLOT(recv())));

}

Widget::~Widget()
{
}


udp server.h

#ifndef UDPSEVER_H
#define UDPSEVER_H

#include
#include
class udpSever : public QObject
{
    Q_OBJECT
public:
    explicit udpSever(QObject *parent = nullptr);

    void init()

   {
            udpSocket = new QUdpSocket(this);
            udpSocket->bind(QHostAddress::AnyIpv4, 8888);

            connect(udpSocket, SIGNAL(readyRead),
                    this, SLOT(readPendingDatagrams));
    }


public slots:
        void readPendingDatagrams()
        {
            while (udpSocket->hasPendingDatagrams()) {
                QByteArray datagram;
                datagram.resize(udpSocket->pendingDatagramSize());
                QHostAddress sender;
                quint16 senderPort;

                udpSocket->readDatagram(datagram.data(),datagram.size(),&sender,&senderPort);
                qDebug()<<"recv:"<writeDatagram(datagram.data(),datagram.size(),sender,senderPort);
            }
        }


    }

signals:



};

#endif // UDPSEVER_H

udp server 的 main

#include 
#include  
int main(int argc, char *argv[])
{
    QCoreApplication a(argc, argv);

    udpserver server;
    server.init();

    return a.exec();
}

你可能感兴趣的:(笔记,qt)