From 88596456425f679c9110443ccd7a78cefd86f46d Mon Sep 17 00:00:00 2001 From: feiyangqingyun Date: Wed, 8 Jan 2020 17:39:26 +0800 Subject: [PATCH] =?UTF-8?q?=E6=9B=B4=E6=96=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- dbpage/dbpage.cpp | 73 ++++++++++++++++++++++++++++++++++++++--------- dbpage/dbpage.h | 4 +-- 2 files changed, 62 insertions(+), 15 deletions(-) diff --git a/dbpage/dbpage.cpp b/dbpage/dbpage.cpp index ec4ab64..cd6237c 100644 --- a/dbpage/dbpage.cpp +++ b/dbpage/dbpage.cpp @@ -1,4 +1,5 @@ -#include "dbpage.h" +#pragma execution_character_set("utf-8") +#include "dbpage.h" SqlQueryModel::SqlQueryModel(QObject *parent) : QSqlQueryModel(parent) { @@ -32,6 +33,42 @@ QVariant SqlQueryModel::data(const QModelIndex &index, int role) const } } + //实现鼠标经过整行换色,如果设置了hoverRow才需要处理 + if (property("hoverRow").isValid()) { + int row = property("hoverRow").toInt(); + if (row == index.row()) { + if(role == Qt::BackgroundRole) { + value = QColor(property("hoverBgColor").toString()); + } else if(role == Qt::TextColorRole) { + value = QColor(property("hoverTextColor").toString()); + } + } + } + + //实现隐藏部分显示,指定列和替换字符 + if (property("hideColumn").isValid()) { + int column = property("hideColumn").toInt(); + if (column == index.column()) { + if(role == Qt::DisplayRole) { + QString letter = property("hideLetter").toString(); + int start = property("hideStart").toInt(); + int end = property("hideEnd").toInt(); + QString str = value.toString(); + + QStringList list; + for (int i = 0; i < str.length(); i++) { + if (i >= start && i <= end) { + list << letter; + } else { + list << str.at(i); + } + } + + value = list.join(""); + } + } + } + return value; } @@ -60,6 +97,21 @@ DbCountThread::DbCountThread(QObject *parent) : QThread(parent) } void DbCountThread::run() +{ + select(); +} + +void DbCountThread::setConnName(const QString &connName) +{ + this->connName = connName; +} + +void DbCountThread::setSql(const QString &sql) +{ + this->sql = sql; +} + +void DbCountThread::select() { //计算用时 QDateTime dtStart = QDateTime::currentDateTime(); @@ -74,22 +126,12 @@ void DbCountThread::run() emit receiveCount(count, msec); } -void DbCountThread::setConnName(const QString &connName) -{ - this->connName = connName; -} - -void DbCountThread::setSql(const QString &sql) -{ - this->sql = sql; -} - QScopedPointer DbPage::self; DbPage *DbPage::Instance() { if (self.isNull()) { - QMutex mutex; + static QMutex mutex; QMutexLocker locker(&mutex); if (self.isNull()) { self.reset(new DbPage); @@ -190,7 +232,7 @@ void DbPage::bindData(const QString &sql) //设置列标题和列宽度 for (int i = 0; i < columnCount; i++) { - queryModel->setHeaderData(i, Qt::Horizontal, columnNames.at(i)); + queryModel->setHeaderData(i, Qt::Horizontal, columnNames.at(i)); tableView->setColumnWidth(i, columnWidths.at(i)); } @@ -483,5 +525,10 @@ void DbPage::select() //设置数据库连接名称和查询语句,并启动线程 dbCountThread->setConnName(connName); dbCountThread->setSql(tempSql); + //从5.10开始不支持数据库在线程中执行 +#if (QT_VERSION <= QT_VERSION_CHECK(5,10,0)) dbCountThread->start(); +#else + dbCountThread->select(); +#endif } diff --git a/dbpage/dbpage.h b/dbpage/dbpage.h index 0e43e3b..4a26207 100644 --- a/dbpage/dbpage.h +++ b/dbpage/dbpage.h @@ -66,10 +66,10 @@ signals: public slots: //设置数据库连接名称 void setConnName(const QString &connName); - //设置要执行的查询语句 void setSql(const QString &sql); - + //查询行数 + void select(); }; class DbPage : public QObject