不选用GUI project
new project as following :
一.new qt4 project select empty project
二,add new ->select general ->text file->main.cpp
三,add new ->C++ ->select header file ->client
四,add new ->C++ ->select source file ->client
//over
//client.h
#ifndef CLIENT_H
#define CLIENT_H
#include <QDialog>
#include <QUdpSocket>
//add
#include <QUdpSocket>
#include <QTextEdit>
#include <QPushButton>
#include <QGridLayout>
namespace Ui {
class client;
}
class client : public QDialog {
Q_OBJECT
public:
client(QWidget *parent = 0);
~client();
public slots:
void recieve_message();
private:
// Ui::client *ui;
QUdpSocket *udpsocket;
// QLineEdit *lineedit;
QTextEdit *textEdit_recieve;
QPushButton *pushbutton_send;
QGridLayout *layout;
};
#endif // CLIENT_H
//client.cpp
#include "client.h"
#include <QDebug>
#include <QUdpSocket>
client::client(QWidget *parent) :
QDialog(parent)//,
//ui(new Ui::client)
{
// ui->setupUi(this);
//add
//window set
textEdit_recieve= new QTextEdit;
pushbutton_send=new QPushButton("send") ;
layout= new QGridLayout;
layout->addWidget(this->textEdit_recieve,0,0,4,6);
layout->addWidget(this->pushbutton_send,5,0,1,2);
this->setLayout(layout);
this->setWindowTitle("UDP_Client");
//UDP set
udpsocket=new QUdpSocket;
// udpsocket->bind(QHostAddress::LocalHost,8888);
udpsocket->bind(8888);
//connect(this->pushbutton_send,SIGNAL(clicked()),this,SLOT(send_message()));
connect(this->udpsocket,SIGNAL(readyRead()),this,SLOT(recieve_message()));
}
client::~client()
{
//delete ui;
}
void client::recieve_message()
{
qDebug()<<"in client ";
//receive message .................
}
//main.cpp
#include <QtGui/QApplication>
#include "client.h"
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
client w;
// w.resize(320/2,200);
w.show();
return a.exec();
}