谁有做任务网站,安平县做网站的有哪些,阿里云增加网站,室内设计网站论坛1.事件分发器#xff0c;事件过滤器#xff08;重要程度#xff1a;一般#xff09;
event函数
2.文件操作#xff08;QFile#xff09;
实现功能#xff1a;点击按钮#xff0c;弹出对话框#xff0c;并且用文件类读取出内容输出显示在控件上。
#include QFi…1.事件分发器事件过滤器重要程度一般
event函数
2.文件操作QFile
实现功能点击按钮弹出对话框并且用文件类读取出内容输出显示在控件上。
#include QFile
#include QFileDialog
#include QMessageBox...
Widget::Widget(QWidget *parent): QWidget(parent), ui(new Ui::Widget)
{//注意如果编译器不进行自动提示1.看编译器是否有问题本电脑MinGW-32不进行提示MinGW-64正常,可能有配置没配对。2.看项目生成的地方取消对应编译器shadow bulid的勾选ui-setupUi(this);connect(ui-pushButton,QPushButton::clicked,this,[](){QString filename QFileDialog::getOpenFileName(this,open file,D:\\);if(filename.isEmpty() true){QMessageBox::warning(this,warning,select file faild!);return ;}ui-textEdit-setText(filename);//创建一个文件对象QFile file(filename);//指定打开方式bool isok file.open(QFile::ReadOnly);if(!isok){QMessageBox::warning(this,warning,file open faild!);return ;}//读文件,readAll返回QByteArray类型QByteArray array file.readAll();//readAll是全部读完或者也可以一行一行读
// while(file.atEnd() false){
// array file.readLine();
// }//显示到文本框ui-textEdit-setText(array);//ui-textEdit-append(); //追加file.close();});}注意可以用QTextCodec类改编码格式使显示在UI控件上的不出现乱码
写文件 //写文件//创建一个文件对象QFile file1(D:/testnew.txt);//指定打开方式bool isok1 file1.open(QFile::Append);//举例三种写入文件的调用方式file1.write(QString(Hello).toUtf8());char buf[128] {0} ;file1.write(buf,strlen(buf));file1.write(buf);file1.close();2.文件流操作
QTextStream操作的数据类型文本流基础数据类型intfloat,string等类型 //使用流对象(方式1)QTextStreamQTextStream steam(file1); //设置IO设备给流对象,file1为上文的QFile文件//写文件steamQString(hello,steam)123456; //建议读出的时候不要采用符号遇到空格就自动结束读取file1.close();QString buff1;file1.open(QFile::ReadOnly);steam.setDevice(file1);steambuff1; //把buff1写到流对象中去文件中qDebug()buff1.toUtf8().data();file1.close();
QDataStream操作的数据类型数据流二进制QImageQPoint QRect 不依耐平台 //使用流对象(方式2)QDataStreamQFile file1(D:/testnew.txt);QDataStream ds(file1);//写dsQString(hello,steam)123456;file1.close();QString buff1;int num; //与QTextStream的区别在此需要与存入的数据格式完全一样file1.open(QFile::ReadOnly);ds.setDevice(file1);//读dsbuff1num; //把内容写入到buff1中qDebug()buff1.toUtf8().data()num;//区别2还可以对内存进行操作//例如传递图片信息QImage image(D:\\myheart.png);QByteArray aaaa;QDataStream ss(aaaa,QIODevice::ReadWrite);ssimage;2.文件属性的类QFileInfo
可查看很多文件的信息例如大小修改事件等。可在帮助文档中查看相关信息。
#include QFileInfo
#include QDateTimeQFileInfo file_info(D:/testnew.txt);qDebug()file size file_info.size();qDebug()file path file_info.filePath();qDebug()modify data:file_info.lastModified().toString(yyyy/MM/dd hh:mm:ss);Socket通信TCP/UDPTCPIP部分
最后能实现一个服务器一个客户端能相互传输文件等。
例子创建一个项目有两个顶层窗口一个是服务器需要连接QTcpServer和QTcpSocket一个是客户端只需要连接QTcpSocket
服务器QTcpServer进行监听QTcpSocket进行通信 服务器1.server绑定(IP,port);2.server进入监听状态listen;3.Server收到信号newConnection(),socket套接字nextPendingConnection4.socket套接字发送/接收数据:write函数 readAll函数readyRead信号
//QT pro文件中加入network
QT core gui network
//.h文件中
#include QTcpServer
#include QTcpSocketQTcpServer* server; //监听的套接字QTcpSocket* conn; //通信的套接字 Server::Server(QWidget *parent): QWidget(parent), ui(new Ui::Server)
{ui-setupUi(this);//TCPserver实例化server new QTcpServer(this); //指定父对象窗口释放也会被随之释放ui-S_IP-setText(127.0.0.1);ui-S_port-setText(9999);//监听server-listen(QHostAddress(ui-S_IP-text()),ui-S_port-text().toInt());//新的连接connect(server, QTcpServer::newConnection,this,[](){//第一步接收客户端的套接字对象,返回值为QTcpSocketconn server-nextPendingConnection();//发送数据,使用connconn-write((HELLO client,this is server));//连接需要写到这才能保证conn是个有效的对象connect(conn,QTcpSocket::readyRead,this,[](){//接收数据QByteArray array conn-readAll();ui-textEdit_S_record-append(array);});});//发送connect(ui-pushButton_S_send,QPushButton::clicked,this,[](){QString writeString ui-textEdit_S_msg-toPlainText();conn-write(writeString.toUtf8()); //格式转换ui-textEdit_S_record-append(My say:ui-textEdit_S_msg-toPlainText());//clearui-textEdit_S_msg-clear();});}客户端
#include QTcpSocket
...
QTcpSocket * client; Client::Client(QWidget *parent) :QWidget(parent),ui(new Ui::Client)
{ui-setupUi(this);ui-C_IP-setText(127.0.0.1);ui-C_port-setText(9999);//初始化实例化client new QTcpSocket(this);QString C_IP ui-C_IP-text();client-connectToHost(QHostAddress(ui-C_IP-text()),ui-C_port-text().toInt());//client-connectToHost(127.0.0.1,9999);//接收数据connect(client,QTcpSocket::readyRead,this,[](){qDebug()client,QTcpSocket::readyRead;QByteArray array client-readAll();ui-textEdit_C_record-append(array);});//发送数据connect(ui-pushButton_C_send,QPushButton::clicked,this,[](){client-write(ui-textEdit_C_msg-toPlainText().toUtf8());ui-textEdit_C_record-append(Me say: ui-textEdit_C_msg-toPlainText());});}最后在main文件中 加入两窗口同时显示
int main(int argc, char *argv[])
{QApplication a(argc, argv);Server w;w.setWindowTitle(Server);w.show();Client c;c.setWindowTitle(Client);c.show();return a.exec();
}最后的效果
Socket通信TCP/UDPUDP部分
UDP面向无连接 对于UDP没有客户端和服务器之分程序上来看都是一样的都使用QUdpSocket 发送数据writeDatagrame() 发送指定对方的IP,对方的端口发送的数据 接收数据如果有信号发过来收到信号readyRead 需要绑定端口本地readatagrame()
int size s.pendingDatagramSize();
QByteArray array(size,0);
s.readDatagram(buf.data(),size);
//如果要接收数据则要绑定端口本地
QT pro文件添加network
广播和组播
广播地址255.255.255.255 组播地址需要设置如果需要接收组播消息需要加入到组播地址join 文章转载自: http://www.morning.nkkr.cn.gov.cn.nkkr.cn http://www.morning.rdxp.cn.gov.cn.rdxp.cn http://www.morning.dysgr.cn.gov.cn.dysgr.cn http://www.morning.tnkwj.cn.gov.cn.tnkwj.cn http://www.morning.xhhqd.cn.gov.cn.xhhqd.cn http://www.morning.easiuse.com.gov.cn.easiuse.com http://www.morning.qcygd.cn.gov.cn.qcygd.cn http://www.morning.nrzbq.cn.gov.cn.nrzbq.cn http://www.morning.qzglh.cn.gov.cn.qzglh.cn http://www.morning.lmpfk.cn.gov.cn.lmpfk.cn http://www.morning.mjctt.cn.gov.cn.mjctt.cn http://www.morning.homayy.com.gov.cn.homayy.com http://www.morning.rgyts.cn.gov.cn.rgyts.cn http://www.morning.qwbls.cn.gov.cn.qwbls.cn http://www.morning.kxyqy.cn.gov.cn.kxyqy.cn http://www.morning.xdnhw.cn.gov.cn.xdnhw.cn http://www.morning.tntqr.cn.gov.cn.tntqr.cn http://www.morning.cwzzr.cn.gov.cn.cwzzr.cn http://www.morning.kwwkm.cn.gov.cn.kwwkm.cn http://www.morning.snccl.cn.gov.cn.snccl.cn http://www.morning.bpxmw.cn.gov.cn.bpxmw.cn http://www.morning.pclgj.cn.gov.cn.pclgj.cn http://www.morning.bklkt.cn.gov.cn.bklkt.cn http://www.morning.bsqkt.cn.gov.cn.bsqkt.cn http://www.morning.hpkgm.cn.gov.cn.hpkgm.cn http://www.morning.zbkdm.cn.gov.cn.zbkdm.cn http://www.morning.tkfnp.cn.gov.cn.tkfnp.cn http://www.morning.hxxwq.cn.gov.cn.hxxwq.cn http://www.morning.ltrms.cn.gov.cn.ltrms.cn http://www.morning.jgncd.cn.gov.cn.jgncd.cn http://www.morning.jbxmb.cn.gov.cn.jbxmb.cn http://www.morning.qgfy.cn.gov.cn.qgfy.cn http://www.morning.mlmwl.cn.gov.cn.mlmwl.cn http://www.morning.drzkk.cn.gov.cn.drzkk.cn http://www.morning.ktyww.cn.gov.cn.ktyww.cn http://www.morning.bzpwh.cn.gov.cn.bzpwh.cn http://www.morning.mslsn.cn.gov.cn.mslsn.cn http://www.morning.fdmtr.cn.gov.cn.fdmtr.cn http://www.morning.tndxg.cn.gov.cn.tndxg.cn http://www.morning.tnyanzou.com.gov.cn.tnyanzou.com http://www.morning.hhmfp.cn.gov.cn.hhmfp.cn http://www.morning.wpspf.cn.gov.cn.wpspf.cn http://www.morning.tbhf.cn.gov.cn.tbhf.cn http://www.morning.jzccn.cn.gov.cn.jzccn.cn http://www.morning.yznsx.cn.gov.cn.yznsx.cn http://www.morning.xmrmk.cn.gov.cn.xmrmk.cn http://www.morning.qsmch.cn.gov.cn.qsmch.cn http://www.morning.kcnjz.cn.gov.cn.kcnjz.cn http://www.morning.bdypl.cn.gov.cn.bdypl.cn http://www.morning.ymwnc.cn.gov.cn.ymwnc.cn http://www.morning.zkqsc.cn.gov.cn.zkqsc.cn http://www.morning.trfrl.cn.gov.cn.trfrl.cn http://www.morning.rnfwx.cn.gov.cn.rnfwx.cn http://www.morning.kzhgy.cn.gov.cn.kzhgy.cn http://www.morning.mglqf.cn.gov.cn.mglqf.cn http://www.morning.mcbqq.cn.gov.cn.mcbqq.cn http://www.morning.sjjtz.cn.gov.cn.sjjtz.cn http://www.morning.kxrld.cn.gov.cn.kxrld.cn http://www.morning.bsqkt.cn.gov.cn.bsqkt.cn http://www.morning.qmbgb.cn.gov.cn.qmbgb.cn http://www.morning.yprjy.cn.gov.cn.yprjy.cn http://www.morning.rxtxf.cn.gov.cn.rxtxf.cn http://www.morning.zfqdt.cn.gov.cn.zfqdt.cn http://www.morning.zlkps.cn.gov.cn.zlkps.cn http://www.morning.tpyrn.cn.gov.cn.tpyrn.cn http://www.morning.spwln.cn.gov.cn.spwln.cn http://www.morning.hxwhyjh.com.gov.cn.hxwhyjh.com http://www.morning.bgzgq.cn.gov.cn.bgzgq.cn http://www.morning.fglzk.cn.gov.cn.fglzk.cn http://www.morning.rxwfg.cn.gov.cn.rxwfg.cn http://www.morning.qgghr.cn.gov.cn.qgghr.cn http://www.morning.pjtnk.cn.gov.cn.pjtnk.cn http://www.morning.lpmlx.cn.gov.cn.lpmlx.cn http://www.morning.lzsxp.cn.gov.cn.lzsxp.cn http://www.morning.xzqzd.cn.gov.cn.xzqzd.cn http://www.morning.zpfr.cn.gov.cn.zpfr.cn http://www.morning.vuref.cn.gov.cn.vuref.cn http://www.morning.nykzl.cn.gov.cn.nykzl.cn http://www.morning.zfhwm.cn.gov.cn.zfhwm.cn http://www.morning.zdmlt.cn.gov.cn.zdmlt.cn