我想将QTreeWidget的内容存储到MySQL数据库中。
表描述的SQL是:
CREATE TABLE category (
id int not null,
parentId int,
name varchar(1024), );
parentId 是一个外键,它引用同一张表中的 id 。 (希望执行自我加入)
使用QtCreator,以下代码可以正常工作:
int FactsBuilder::findChildren( QTreeWidgetItem *node, int top )
{
for( int i = 0; i < node->childCount(); ++i )
{
QTreeWidgetItem *item = node->child(i);
qDebug() << "Child: " << top++ << " - " << item->text(0);
if(item->childCount() > 0 ) {
this->findChildren(item, top++ );
}
}
return top;
}
void FactsBuilder::updateCatSQL()
{
int count = 1;
for( int i = 0; i < ui->treeWidget->topLevelItemCount(); ++i )
{
QTreeWidgetItem *item = ui->treeWidget->topLevelItem( i );
qDebug() << "Item Number: " << count++ << " - " << item->text(0);
if(item->childCount() > 0 ) {
count = this->findChildren( item, count);
}
}
}
一些示例输出:
Top: 1 - "foo"
Top: 2 - "bar"
Child: 3 - "beer"
Child: 4 - "ice"
Top: 5 - "bla"
Top: 6 - "baz"
最上面的项目没有父项,因此在这种情况下,MySQL数据库中的 parentId 列将包含NULL。
在这种情况下,其text属性中存储有“ beer”和“ ice”的QTreeWidgetItems将导致在数据库的 parentId 列中存储“ 2”。因为它们是在QTreeWidgetItem下组织的,其文本属性中存储着“ bar”。
第一种情况
除非将带有“ ice”的QTreeWidgetItems组织在其文本属性中存储有“ beer”的QTreeWidgetItem下。
在这种情况下,我希望数据库的 parentId 列中为“ 2”和“ 3”。
第二种情况
答案 0 :(得分:0)
首先,如果您的表设计得更好,则可以简化解决方案,更好的方法是将id字段设置为autoincrement:
CREATE TABLE IF NOT EXISTS category (
id int NOT NULL AUTO_INCREMENT PRIMARY KEY,
parentId int,
name varchar(1024)
)
然后,我创建了一个从invisibleRootItem()
接收QTreeWidget
的方法,并将该方法保存在数据库中。
#include <QtWidgets>
#include <QtSql>
static bool createConnection(){
QSqlDatabase db = QSqlDatabase::addDatabase("QMYSQL");
db.setHostName("hostname");
db.setDatabaseName("databasename");
db.setUserName("username");
db.setPassword("password");
if (!db.open()) {
qDebug()<<"Cannot open database\n"
"Unable to establish a database connection.\n"
"This example needs SQLite support. Please read "
"the Qt SQL driver documentation for information how "
"to build it.\n\n"
"Click Cancel to exit.";
return false;
}
QSqlQuery query;
query.exec("DROP TABLE IF EXISTS category;");
if(!query.exec(R"(CREATE TABLE IF NOT EXISTS category (
id int NOT NULL AUTO_INCREMENT PRIMARY KEY,
parentId int,
name varchar(1024)
))"))
qDebug()<<query.lastError().text();
return true;
}
static void fill_model(QTreeWidget &tree){
QTreeWidgetItem *foo_item = new QTreeWidgetItem({"foo"});
QTreeWidgetItem *bar_item = new QTreeWidgetItem({"bar"});
QTreeWidgetItem *bla_item = new QTreeWidgetItem({"bla"});
QTreeWidgetItem *baz_item = new QTreeWidgetItem({"baz"});
for(QTreeWidgetItem *item : {foo_item, bar_item, bla_item, baz_item})
tree.addTopLevelItem(item);
QTreeWidgetItem *beer_item = new QTreeWidgetItem({"beer"});
QTreeWidgetItem *beer_child_item = new QTreeWidgetItem({"beer_child"});
QTreeWidgetItem *ice_item = new QTreeWidgetItem({"ice"});
for(QTreeWidgetItem *item : {beer_item, ice_item})
bar_item->addChild(item);
beer_item->addChild(beer_child_item);
beer_child_item->addChild(new QTreeWidgetItem({"beer_child_child"}));
}
static void save_to_db(const QString & tablename, QTreeWidgetItem* parent, int parent_id=0){
for(int i=0; i< parent->childCount(); ++i){
QTreeWidgetItem *child_item = parent->child(i);
QSqlQuery query(QString("INSERT INTO %1 (parentId, name) VALUES (?, ?)").arg(tablename));
if(parent_id != 0)
query.bindValue(0, parent_id);
query.bindValue(1, child_item->text(0));
if(!query.exec())
qDebug()<< query.lastError().text();
save_to_db(tablename, child_item, query.lastInsertId().toInt());
}
}
int main(int argc, char *argv[]){
QApplication a(argc, argv);
if(!createConnection())
return -1;
QWidget w;
QTreeWidget tree_widget;
fill_model(tree_widget);
save_to_db("category", tree_widget.invisibleRootItem());
tree_widget.expandAll();
QTableView table_view;
QSqlTableModel model;
model.setTable("category");
model.select();
table_view.setModel(&model);
QHBoxLayout *hlay = new QHBoxLayout(&w);
hlay->addWidget(&tree_widget);
hlay->addWidget(&table_view);
w.resize(640, 480);
w.show();
return a.exec();
}