gpt4 book ai didi

c++ - 如何在 Qt 中创建和加载 SQLite?

转载 作者:行者123 更新时间:2023-11-28 07:14:22 28 4
gpt4 key购买 nike

我有一个包含数据的表 (QTableWidget)。我如何创建数据库 SQLite 并保存我的数据。那我下次还需要重新载入数据吗?感谢提前!

enter image description here

最佳答案

/*constructor ...*/
{

...

/* create TableView to hold our db */
pcbtv = new QPcbView(this);
pcbtv->setModel(tabsort);
pcbtv->setAlternatingRowColors(true);
pcbtv->setSortingEnabled(true);
pcbtv->setCornerButtonEnabled(false);
pcbtv->setSelectionMode(QAbstractItemView::SingleSelection);
pcbtv->setSelectionBehavior(QAbstractItemView::SelectRows);
pcbtv->setEditTriggers(QTableView::NoEditTriggers); /* start read-only */
pcbtv->resizeColumnsToContents();

/* Header titles must be set on the model */
pcbtv->model()->setHeaderData(PartNo_PartNo, Qt::Horizontal, "Part No");
pcbtv->model()->setHeaderData(PartNo_Name, Qt::Horizontal, "Name");
pcbtv->model()->setHeaderData(PartNo_GteNo, Qt::Horizontal, "GT-E Part No");
pcbtv->model()->setHeaderData(PartNo_GteName, Qt::Horizontal, "GT-Electronics Name");

QHeaderView* header = pcbtv->horizontalHeader();
//header->setToolTip("bliblabloo");
header->setAlternatingRowColors(true);
header->setSectionsMovable(true);
header->setStretchLastSection(true);
pcbtv->setHorizontalHeader(header);

...

}

void PcbList::loadDb(QString filepath)
{
openSqliteDb(filepath, "PARTNO");
}

void PcbList::openSqliteDb(QString dbname, QString table)
{
QSqlDatabase db = QSqlDatabase::addDatabase("QSQLITE");
db.setDatabaseName(dbname);
db.open();
qDebug() << "connected to db " << dbname;

tabmod = new QSqlTableModel(this, db);
tabmod->setTable(table);
tabmod->setSort(PartNo_GteNo, Qt::AscendingOrder);
tabmod->setEditStrategy(QSqlTableModel::OnFieldChange);
tabmod->select();
}

关于c++ - 如何在 Qt 中创建和加载 SQLite?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20459540/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com