欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

迅为4412——QT4开发手册

程序员文章站 2022-06-24 18:46:13
中文显示# main.cpp------------------------#include QTextCodec::setCodecForTr(QTextCodec::codecForName("UTF-8"));QTextCodec::setCodecForLocale(QTextCodec::codecForName("UTF-8"));QTextCodec::setCodecForCStrings(QTextCodec::codecForName("U...

中文显示

# main.cpp
------------------------
#include <QTextCodec>
QTextCodec::setCodecForTr(QTextCodec::codecForName("UTF-8"));
QTextCodec::setCodecForLocale(QTextCodec::codecForName("UTF-8"));
QTextCodec::setCodecForCStrings(QTextCodec::codecForName("UTF-8"));

QFont font;
font.setPointSize(16);
font.setFamily(("wenquanyi"));
font.setBold(false);
a.setFont(font);

无标题栏显示

setWindowFlags(Qt::FramelessWindowHint);
show();

串口使用的第三方的库:Posix_QextSerialPort

Posix_QextSerialPort *myCom;
myCom = new Posix_QextSerialPort("/dev/ttyUSB0",QextSerialBase::Polling);
myCom ->open(QIODevice::ReadWrite);
myCom->setBaudRate(BAUD38400);
myCom->setDataBits(DATA_8);
myCom->setParity(PAR_NONE);
myCom->setStopBits(STOP_1);
myCom->setFlowControl(FLOW_OFF);
myCom->setTimeout(100);
readTimer = new QTimer(this);
readTimer->start(100);

connect(readTimer,SIGNAL(timeout()),this,SLOT(readMyCom()));

void TestThread::readMyCom() 
{
	QByteArray temp = myCom->readAll();
	myCom->write(temp);
}

json使用第三方库(QJson和CJson)

https://github.com/croxs-power/QJson
#include <QJson.h>
objtemp.insert("ProductKey","123");
objtemp.insert("ProductSecret","2222");
objtemp.insert("Address","1234");
qDebug()<<objtemp.format_string();#格式化为字符串
#include <cJSON.h>
QString ss="{"address":"1234","deviceState":1}"
jsonRoot = cJSON_Parse(ss.toLocal8Bit().data());
Conrtrol_address = cJSON_GetObjectItem(jsonRoot, "address");
Conrtrol_deviceState = cJSON_GetObjectItem(jsonRoot, "deviceState");
Control_Device_Address=Conrtrol_address->valuestring;
Control_Device_deviceState_number=Conrtrol_deviceState->valueint;

网络请求

QNetworkAccessManager *m_network;
QNetworkRequest request;
QNetworkReply* reply;

m_network = new QNetworkAccessManager(this);
QObject::connect(m_network, SIGNAL(finished(QNetworkReply*)),this, SLOT(replyFinished(QNetworkReply*)));
request.setUrl(QUrl("http://121.199.57.139:8080/iot/UploadData"));
request.setHeader(QNetworkRequest::ContentTypeHeader,QVariant("application/json"));
SHT_Issue_reply=SHT_Issue_network->post(SHT_Issue_request,SHT_Issue_address.json_string());

socket请求

QTcpSocket *pTcpSocketClient;
QString qStrIP = "121.199.57.139";
quint16 netport = 8989;

QObject::connect(this,SIGNAL(started()),this,SLOT(slotConnectServer()));
void TestThread::slotConnectServer()    //连接服务器
{
    pTcpSocketClient = new QTcpSocket(this);
    pTcpSocketClient->abort();
    pTcpSocketClient->connectToHost(qStrIP,netport);
    connect(pTcpSocketClient,SIGNAL(readyRead()),this,SLOT(slotReadMesg()));
}
void TestThread::slotReadMesg() //读取信息
{
        qba= pTcpSocketClient->readAll(); 
}

TableView使用

QStandardItemModel *model_history;
model_history= new QStandardItemModel();
model_history->setColumnCount(4);
model_history->setHorizontalHeaderItem(0, new QStandardItem("DeviceID"));
model_history->setHorizontalHeaderItem(1, new QStandardItem("DeviceType"));
model_history->setHorizontalHeaderItem(2, new QStandardItem("SensorValues"));
model_history->setHorizontalHeaderItem(3, new QStandardItem("acceptTime"));

this->ui->tableView->setModel(model_history);
this->ui->tableView->setColumnWidth(0,200);
this->ui->tableView->setColumnWidth(1,150);
this->ui->tableView->setColumnWidth(2,150);
this->ui->tableView->setColumnWidth(3,150);

model_history->setItem(i,0,new QStandardItem(DeviceID->valuestring));
model_history->setItem(i,1,new QStandardItem(DeviceType_result));
model_history->setItem(i,2,new QStandardItem(QString::number(SensorValues->valuedouble)));
model_history->setItem(i,3,new QStandardItem(QString::number(acceptTime->valueint)));

本文地址:https://blog.csdn.net/JoshRong/article/details/109262836