QT学习笔记

1,声明一个信号要使用signals关键字,在signals前面不能使用public,private和protected


使用信号和槽还必须在生命的最开始处添加添加Q_OBJECT宏


bool QObject::connect(const QObject *sender,const char *signal,const QObject *
receiver,const char *method,Qt::ConnectionType = Qt::AutoConnection)

QIODevice 类是 Qt 种所有 I/O 设备的基础接口类,为诸如QFile,QBuffer和QTcpSocket 等
支持读写数据块的设备提供了一个抽象的接口


访问一个设备以前需要使用open()函数打开设备 ,而且必须指定正确的打开模式


write(),putChar()来进行写入,使用read(),readLine(),readAll()进行读取,使用close()关闭设备


QIODevice 会区别两种类型的设备,:随机存取设备和顺序存储设备。


QFile类提供了一个用于读写文件的接口,是一个可以用来读写文本文件,二进制文件和Qt资源的I/O
设备,QFile可以单独使用,也可以和QTextStream或者




串口操作:
1,读取数据:
void MainWindow::readMyCom()
{
QByteArray temp = myCom->readAll();
unsigned short int crc_value;
crc_value = crc((unsigned char *)temp.data(),6);

qDebug() << crc_value <<endl;
QString buf;

if(!temp.isEmpty()){
ui->textBrowser->setTextColor(Qt::black);
if(ui->ccradioButton->isChecked()){
buf = temp;
}else if(ui->chradioButton->isChecked()){
QString str;
for(int i = 0;i < temp.count();i++){
QString s;
s.sprintf("0x%02x,"(unsigned char)temp.at(i));
buf += s;
}
}
ui->textBrowser->setText(ui->textBrowser->doucument()->toPlainText() + buf);
QTextCursor curosr = ui->textBrowser->textCursor();
curosr.movePosition(QTextCursor::End);
ui->textBrowser->setTextCursor(cursor);

ui->recvbyteslcdNumber->display(ui->recvbyteslcdNumber->value() + temp.size());
ui->statusBar->showMessage(tr("成功读取%1字节数").arg(temp.size()));
}
}




example:
QString str = "a,,b,c";
QStringList list1 = str.split(","); // list1:["a","","b","c"]
QStringList list2 = str.split(",",QString::SlipEmptyParts);//list2:["a","b","c"]
//发送数据
viod MainWindow::sendMsg(){
QByteArray buf;
if(ui->sendAsHexcheckBox->isChecked()){
QString str;
bool ok;
char data;
QStringList list;
str = ui->sendMsgLineEdit->text();
list = str.split(" ")

for(int i = 0;i < list.count(); i++){
if(list.at(i) == " ")
continue;
if(list.at(i).isEmpty())
continue;
data = (char)list.at(i).toInt(&ok,16);
if(!ok){
QMessageBox::information(this,tr("提示消息"),tr("输入的数据格式错误"),QMessageBox::Ok);
if(obotimer != NULL)
obotimer->stop();
ui->sendmsgBtn->setText(tr("发送"));
ui->sendmsgBtn->setIcon(Qicon(":new/prefix1/src/send.png"));
return;
}
buf.append(data);
}
}else{
#if QT_VERSION < 0x050000
buf = ui->sendMsgLineEdit->text().toAscii();
#else
buf = ui->sendMsgLineEdit->text().toLocal8Bit();
#endif
}
//发送数据
myCom->write(buf);
ui->statusBar->showMessage(tr("发送数据成功"));
//界面控制
ui->textBrowser->setTextColor(Qt::lightGrary);
}


/
设置连续发送计时器
obotimer = new QTimer(this);
connect(obotimer,SIGNAL(timeout()),this,SLOT(sendMsg()));
/
//发送数据按钮
void MainWindow::on_sendmsgBtn_clicked()
{
//如果当前正在连续发送数据,暂停发送
if(ui->sendmsgBtn->Text()== tr("暂停")){
obotimer->stop();
ui->sendmsgBtn->setText(tr("发送"));
ui->sendmsgBtn->setTcon(QIcon(":new/prefix1/src/send.png"));
return;
}
//如果发送数据为空,给出提示并返回
if(ui->sendMsgLineEdit->text().isEmpty()){
QMessageBox::information(this,tr("提示消息"),tr("没有需要发送的数据"),QMessageBox::ok);
return;
}

//如果不是连续发送
if(!ui->obocheckBox->isChecked()){
ui->sendMsgLineEdit->setFocus();
sendMsg();
}else{//连续发送
obotimer->start(obotimerdly);
ui->sendmsgBtn->setText(tr("暂停"));
ui->sendmsgBtn->setTcon(QIcon(":new/prefix1/src/pause.png"));
}
}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

沐白001

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值