数据库相关
QLite数据库
1 2 3 4 5 6 7 8 9
| db = QSqlDatabase::addDatabase("QSQLITE");
QString currentDir = QDir::currentPath();
QString dbName = "mydatabase.db";
QString dbFilePath = currentDir + "/" + dbName;
db.setDatabaseName(dbFilePath);
|
MySQL数据库
1 2 3 4 5 6
| db = QSqlDatabase::addDatabase("QMYSQL"); db.setDatabaseName("mydatabase"); db.setHostName("localhost"); db.setPort(3306); db.setUserName("username"); db.setPassword("password");
|
打开与检查数据库连接
1 2 3 4 5
| if (!db.open()) { qDebug() << "数据库连接失败"; } else { qDebug() << "数据库连接成功"; }
|
SQL语句执行与查询
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52
| QSqlQuery query; query.prepare("SELECT note FROM notes WHERE id = :id"); query.bindValue(":id", selectedId);
query.prepare("CREATE TABLE IF NOT EXISTS notes(" "date TIMESTAMP DEFAULT CURRENT_TIMESTAMP, " "title VARCHAR(255) NOT NULL, " "note TEXT, " "id INTEGER PRIMARY KEY AUTOINCREMENT, " "sort_order INTEGER)");
if (query.next()) { int idOrder = query.value(0).toInt(); QString title = query.value(1).toString(); QString note = query.value(2).toString(); int sortOrder = query.value(3).toInt(); }
QSqlQuery query2; query2.prepare("SELECT id, title, note, sort_order FROM notes ORDER BY sort_order DESC");
QSqlQuery updateQuery; updateQuery.prepare("UPDATE notes SET note = :newContent WHERE id = :id"); updateQuery.bindValue(":newContent", newText); updateQuery.bindValue(":id", currentId);
QSqlQuery maxSortOrderQuery; maxSortOrderQuery.prepare("SELECT MAX(sort_order) FROM notes");
QSqlQuery insertQuery; insertQuery.prepare("INSERT INTO notes (title, note, sort_order,id) VALUES (:title, :note, :sortOrder,:id)"); insertQuery.bindValue(":title", title); insertQuery.bindValue(":note", ""); insertQuery.bindValue(":sortOrder", newSortOrder); insertQuery.bindValue(":id", newIdOrder);
QSqlQuery deleteQuery; deleteQuery.prepare("DELETE FROM notes WHERE id = :id"); deleteQuery.bindValue(":id", idToDelete);
QSqlQuery checkEmptyQuery; checkEmptyQuery.exec("SELECT COUNT(*) FROM notes"); checkEmptyQuery.next(); int rowCount = checkEmptyQuery.value(0).toInt();
|
心跳检查
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
| void MainWindow::ask_check() { QTimer* heartbeatTimer = new QTimer(this);
connect(heartbeatTimer, &QTimer::timeout, this, [this]() { QSqlQuery query(db); if (query.exec("SELECT 1")) { if (query.next()) { ui->info_label->setStyleSheet("color: blue;"); qDebug() << "心跳检测 - 数据库连接正常"; } } else { ui->info_label->setStyleSheet("color: red;"); qDebug() << "心跳检测失败:" << query.lastError().text(); } }); heartbeatTimer->start(60000); }
|
事件相关
概念
通过重写eventFilter方法,自定义对特定对象和事件类型的响应逻辑。例子:处理右键菜单和文本编辑器焦点变化,在合适的位置注册事件过滤器。通常在构造函数或初始化方法中完成此操作。
1 2
| ui->listWidget->installEventFilter(this); ui->textEdit->installEventFilter(this);
|
重写eventFilter方法
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38
| bool MainWindow::eventFilter(QObject *watched, QEvent *event) { if (watched == ui->listWidget && event->type() == QEvent::ContextMenu) { QContextMenuEvent *contextEvent = static_cast<QContextMenuEvent*>(event);
QModelIndex index = ui->listWidget->indexAt(contextEvent->pos());
if (index.isValid()) {
rightClickMenu->exec(contextEvent->globalPos());
return true; } }
if (watched == ui->textEdit) { if (event->type() == QEvent::FocusIn) { isTextEditFocused = true; connect(ui->textEdit, &QTextEdit::textChanged, this, &MainWindow::onTextEditContentChanged); } else if (event->type() == QEvent::FocusOut) { isTextEditFocused = false; disconnect(ui->textEdit, &QTextEdit::textChanged, this, &MainWindow::onTextEditContentChanged); } else if (event->type() & Qt::ControlModifier) { } } return QMainWindow::eventFilter(watched, event); }
|