1、打开文件对话框
//打开文件对话框
QString fileName = QFileDialog::getOpenFileName(this, tr("打开"));
QFile file(fileName);
if (!file.open(QIODevice::ReadOnly | QIODevice::Text))
QMessageBox::StandardButton btnValue = QMessageBox::information(this, tr("提示"), tr("打开失败!"));
2、读取文本文件
//创建文本流
QTextStream stream(&file);
QString str=stream.readLine();
while (!stream.atEnd())
{
//逐行读取
QString str=stream.readLine();
}
3、按指定符号分割字符串
QStringList list = str.split(",");
4、消息提示对话框
QMessageBox::StandardButton btnValue = QMessageBox::information(this, tr("提示"), tr("打开失败!"));
QMessageBox msgBox;
msgBox.setText("账号或密码错误");
msgBox.exec();
5、中文字符无法正常显示问题,在头文件中添加如下代码:
#pragma execution_character_set("UTF-8")
6、QT数据库创建表语句
QSqlQuery query;
QString str = "create table student(userID int primary key,userPwd varchar(50));";
if (query.exec(str) == false)
qDebug() << "model::CreateTable():" << query.lastError();
7、QT数据库插入数据语句
QSqlQuery query;
QString str = QString("INSERT INTO student VALUES('%1','%2')").arg(ID).arg(Pwd);
if (!query.exec(str))
{
qDebug() << "model::insertTb():" << query.lastError().text();
}
8、vector定义
std::vector<Pointp> p;
本文介绍了如何在Qt应用中使用QFileDialog打开文件,读取文本文件,处理字符串,处理中文字符,以及执行数据库操作(包括创建表和插入数据),展示了基本的文件I/O和数据库操作技巧。
&spm=1001.2101.3001.5002&articleId=135599137&d=1&t=3&u=230d7370cc0748ce9ffda30efb7d0df6)
1084

被折叠的 条评论
为什么被折叠?



