qt使用详解
我会持续更新,如果不全请各位大哥补充一下,我学习一下
1.1常用的控件
1.1.1QPushButton
属于
qmake : QT += widgets
QPushButton*btn=new QPushButton(this);//设置父亲位this
btn->setFixedSize(QSize(100,50));//设置固定大小
//btn->resize(100,50);//可拖拽按钮大小
btn->move(QPoint(100,100));//在窗口移动位置
//QPushButton的点击事件
connect(btn,&QPushButton::clicked,[=]()->void{
btn->setIcon(QIcon(":/Image/01.png"));//设置图片
btn->setIconSize(btn->size());//设置图片大小
});
1.1.2QrXml
属于
qmake : QT += xml
读操作:
void Widget::on_btn_1_clicked()
{
QString filename="./test.xml";//路径相对
QFile file(filename);
if(!file.open(QIODevice::ReadOnly)){
qDebug()<<"file open error";
return;
}
QString inf="";
QDomDocument doc;doc.setContent(&file);
QDomElement root=doc.firstChildElement("root");
QDomElement person=root.firstChildElement("person");
while(!person.isNull()){
QDomElement infmation=person.firstChildElement("information");
inf+="name: "+root.attribute("name");
inf+="Atk: "+root.attribute("atk");
inf+="Def: "+root.attribute("def");
inf+="Hp: "+root.attribute("hp");
inf+="text "+infmation.text()+"\n";
person=person.nextSiblingElement();
}
ui->textEdit->setText(inf);
file.close();
}
写操作:
void Widget::on_pushButton_clicked()
{
QDomDocument root;
QDomElement person=root.createElement("person");
person.setAttribute("name","youzicha");
person.setAttribute("atk","1000");
root.appendChild(person);
QString filename="./write.xml";
QFile file(filename);
if(!file.open(QIODevice::WriteOnly)){
qDebug()<<"fileopen error!";
return;
}
QTextStream out(&file);
root.save(out,4);
file.close();
}