Skip to content

Commit

Permalink
更新
Browse files Browse the repository at this point in the history
  • Loading branch information
feiyangqingyun committed Jan 8, 2020
1 parent f264be1 commit e9f140c
Show file tree
Hide file tree
Showing 2 changed files with 62 additions and 15 deletions.
73 changes: 60 additions & 13 deletions dbpage/dbpage.cpp
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
#include "dbpage.h"
#pragma execution_character_set("utf-8")
#include "dbpage.h"

SqlQueryModel::SqlQueryModel(QObject *parent) : QSqlQueryModel(parent)
{
Expand Down Expand Up @@ -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;
}

Expand Down Expand Up @@ -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();
Expand All @@ -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> DbPage::self;
DbPage *DbPage::Instance()
{
if (self.isNull()) {
QMutex mutex;
static QMutex mutex;
QMutexLocker locker(&mutex);
if (self.isNull()) {
self.reset(new DbPage);
Expand Down Expand Up @@ -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));
}

Expand Down Expand Up @@ -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
}
4 changes: 2 additions & 2 deletions dbpage/dbpage.h
Original file line number Diff line number Diff line change
Expand Up @@ -66,10 +66,10 @@ class DbCountThread : public QThread
public slots:
//设置数据库连接名称
void setConnName(const QString &connName);

//设置要执行的查询语句
void setSql(const QString &sql);

//查询行数
void select();
};

class DbPage : public QObject
Expand Down

0 comments on commit e9f140c

Please sign in to comment.