@
目录一、父子窗口互相传值
①父向子窗口传递值
父.h
private slots:
void on_btnMore_clicked();
signals:
void sendStr(QTableWidgetItem*,QTableWidgetItem*,QTableWidgetItem*);
父.cpp
connect(btnMore, SIGNAL(clicked()), this, SLOT(on_btnMore_clicked()), Qt::UniqueConnection);//防止重复调用
void Test::on_btnMore_clicked()
{
OKFlag = 0;
testDialog->setModal(true);
if(OKFlag == 0)
connect(paramDialog,SIGNAL(sendNewStr(QString)),this,SLOT(getNewStr(QString)));
QObject* obj = sender();
if (obj != nullptr)
{
// 获取该单元格对应的数据
item1 = ui->tableWidget->item(row, 1);
item2 = ui->tableWidget->item(row, 2);
item3 = ui->tableWidget->item(row, 3);
QObject::connect(this,SIGNAL(sendStr(QTableWidgetItem*,QTableWidgetItem*,QTableWidgetItem*)),paramDialog,SLOT(getStr(QTableWidgetItem*,QTableWidgetItem*,QTableWidgetItem*)));
if (item1 != nullptr && item2 != nullptr && item3 != nullptr)
{
emit sendStr(item1,item2,item3);
disconnect(this,SIGNAL(sendStr(QTableWidgetItem*,QTableWidgetItem*,QTableWidgetItem*)),paramDialog,SLOT(getStr(QTableWidgetItem*,QTableWidgetItem*,QTableWidgetItem*)));
}
}
paramDialog->show();
}
子.h
private slots:
void getStr(QTableWidgetItem*,QTableWidgetItem*,QTableWidgetItem*);
子.cpp
void TestDialog::getStr(QTableWidgetItem* item1,QTableWidgetItem* item2,QTableWidgetItem* item3)
{
ui->tableWidget->setColumnWidth(0, 200);//设置固定宽度
ui->Text->setText(item1->text());
ui->describeText->setText(item2->text());
QStringList show0 = item3->text().split('_');
……
}
②子向父窗口传递值
子.h
signals:
void sendNewStr(QString);
private slots:
void on_OKButton_clicked();
子.cpp
void TestDialog::on_OKButton_clicked()
{
OKFlag = 1;
rowNum0 = 0, rowNum1 = 0;
QString newstr = ui->currentNum->text();
emit sendNewStr(newstr);
ui->tableWidget->clearContents();
}
父.h
private slots:
void getNewStr(QString);
父.cpp
void Test::getNewStr(QString newstr)
{
int row = ui->tableWidget->currentItem()->row();//当前选中行
QTableWidgetItem *GetNewStr = new QTableWidgetItem(newstr);
ui->tableWidget->setItem(row,4,GetNewStr);
if(OKFlag == 1)
disconnect(paramDialog,SIGNAL(sendNewStr(QString)),this,SLOT(getNewStr(QString)));
this->show();
testDialog->hide();
}
相关解决方法可借鉴网址如下:
Qt 主窗口与子窗口之间传值
QT窗体之间值的传递,多种方法实现
二、数据类型转换
①int转QString
QString str1 = QString("%1").arg(12, 4, 16, QChar('0'));
QString str2 = QString("%1").arg(12, 4, 10, QChar('0'));
②int转16进制
int value =12;
QString str = QString::number(value, 16);// 最小位数
QString str2 = QString("%1").arg(value,4,16,QLatin1Char('0'));//value为int型或char型都可,固定位数
③QString转unsigned char
unsigned char slaveAddChange = ui->spinBoxStation->text().toUtf8().toUInt();
QString IDSplit0 = ID.split('-').at(0);
unsigned char IDHSend = IDSplit0.toInt(nullptr,16);
④ unchar转QString
unsigned char buf[8];
uncharToQstring(buf,8);
void ParameterPanel::uncharToQstring(unsigned char * id,int len)
{
QString temp,msg;
int j = 0;
while (j<len)
{
temp = QString("%1").arg((int)id[j], 2, 16, QLatin1Char('0'));
msg.append(temp);
j++;
}
}
⑤char转int
char charFour[4];
int charInt= CharToIntTable(charFour,4);
int CharToIntTable(char * bytesCharFour,int length)
{
int b[20] = { 0 };//将十六进制数转成int类型
int c = 0;//最终的十进制数
for(int i = 0; i < length; i++)//将其从char类型转成int类型,并存入数组b[]中
{
if ((*(bytesCharFour+i) >= 'a') && (*(bytesCharFour+i) <= 'f'))
{
b[i] = *(bytesCharFour+i) - 'a' + 10;
}
if ((*(bytesCharFour+i) >= '0') && (*(bytesCharFour+i) <= '9'))
{
b[i] = *(bytesCharFour+i) - '0';
}
}
//将每一位都转成16进制
for(int i = 0; i < length; i++)//十进制转十六进制和十六进制转十进制是互逆的
{
b[i] = b[i] * pow( 16 , (length -1 - i));
}
for (int j = 0;j<length;j++)//直接累加
{
c += b[j];
}
return c;
}
⑥QByteArray转16进制
QByteArray bytes;
bytes = port1->readAll();
QByteArray bytesHEX = bytes.toHex();
相关解决方法可借鉴网址如下:
关于QByteArray存储十六进制 与十六进制互转
Qt中QString与int,char等互转