master
feiyangqingyun 2020-01-08 17:39:26 +08:00
parent fa99991ec4
commit 8859645642
2 changed files with 62 additions and 15 deletions

View File

@ -1,4 +1,5 @@
#include "dbpage.h" #pragma execution_character_set("utf-8")
#include "dbpage.h"
SqlQueryModel::SqlQueryModel(QObject *parent) : QSqlQueryModel(parent) 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; return value;
} }
@ -60,6 +97,21 @@ DbCountThread::DbCountThread(QObject *parent) : QThread(parent)
} }
void DbCountThread::run() 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(); QDateTime dtStart = QDateTime::currentDateTime();
@ -74,22 +126,12 @@ void DbCountThread::run()
emit receiveCount(count, msec); emit receiveCount(count, msec);
} }
void DbCountThread::setConnName(const QString &connName)
{
this->connName = connName;
}
void DbCountThread::setSql(const QString &sql)
{
this->sql = sql;
}
QScopedPointer<DbPage> DbPage::self; QScopedPointer<DbPage> DbPage::self;
DbPage *DbPage::Instance() DbPage *DbPage::Instance()
{ {
if (self.isNull()) { if (self.isNull()) {
QMutex mutex; static QMutex mutex;
QMutexLocker locker(&mutex); QMutexLocker locker(&mutex);
if (self.isNull()) { if (self.isNull()) {
self.reset(new DbPage); self.reset(new DbPage);
@ -483,5 +525,10 @@ void DbPage::select()
//设置数据库连接名称和查询语句,并启动线程 //设置数据库连接名称和查询语句,并启动线程
dbCountThread->setConnName(connName); dbCountThread->setConnName(connName);
dbCountThread->setSql(tempSql); dbCountThread->setSql(tempSql);
//从5.10开始不支持数据库在线程中执行
#if (QT_VERSION <= QT_VERSION_CHECK(5,10,0))
dbCountThread->start(); dbCountThread->start();
#else
dbCountThread->select();
#endif
} }

View File

@ -66,10 +66,10 @@ signals:
public slots: public slots:
//设置数据库连接名称 //设置数据库连接名称
void setConnName(const QString &connName); void setConnName(const QString &connName);
//设置要执行的查询语句 //设置要执行的查询语句
void setSql(const QString &sql); void setSql(const QString &sql);
//查询行数
void select();
}; };
class DbPage : public QObject class DbPage : public QObject