当前位置: 首页 > news >正文

QT-Mysql数据库图形化接口

QT += sql

mysqloper.h

qsqlrelationaltablemodelview.h

/*************************************************************************
接口描述:Mysql数据库图形化接口
拟制:
接口版本:V1.0
时间:20230727
说明:支持是否创建界面类QTableView,默认QTableView不允许增删改查功能,支持创建外键表
*************************************************************************/
#ifndef QSQLRELATIONALTABLEMODELVIEW_H
#define QSQLRELATIONALTABLEMODELVIEW_H#include <qsqlrelationaltablemodel.h>
#include <qsqlrelationaldelegate.h>
#include <QTableView>
#include <QMenu>
#include <QHeaderView>
#include <QMessageBox>
#include "mysqloper.h"//源码在其他博文中
#include <QDateTimeEdit>
#include <QStyledItemDelegate>class QDateTimeItemDelegate : public QStyledItemDelegate
{Q_OBJECTpublic:explicit QDateTimeItemDelegate(QString qsDataFormat = "yyyy/MM/dd HH:mm:ss", QObject *parent = nullptr): QStyledItemDelegate(parent){m_qsDataFormat = qsDataFormat;};private:QString m_qsDataFormat;// QStyledItemDelegate interface
public:virtual QString displayText(const QVariant &value, const QLocale &locale) const override{if(value.type() == QVariant::DateTime){return value.toDateTime().toString(m_qsDataFormat);}return QStyledItemDelegate::displayText(value, locale);};virtual QWidget *createEditor(QWidget *parent, const QStyleOptionViewItem &option, const QModelIndex &index) const override{const QSqlRelationalTableModel *sqlModel = qobject_cast<const QSqlRelationalTableModel *>(index.model());QVariant variant = sqlModel->data(index, Qt::DisplayRole);if(variant.type() == QVariant::DateTime){QDateTimeEdit* pDateTimeEdit = new QDateTimeEdit(parent);pDateTimeEdit->setDateTime(variant.toDateTime());pDateTimeEdit->setCalendarPopup(true);pDateTimeEdit->setDisplayFormat(m_qsDataFormat);return pDateTimeEdit;}return QStyledItemDelegate::createEditor(parent, option, index);};
};class QSqlRelationalTableModelEx : public QSqlRelationalTableModel
{Q_OBJECTpublic:explicit QSqlRelationalTableModelEx(QObject *parent = nullptr,QSqlDatabase db = QSqlDatabase()):QSqlRelationalTableModel(parent, db){};protected:QMap<int, QColor> m_relationTextColorMap;QList<QColor> m_colTextColor;public:void InsertRelationTextColorMap(int column, QColor color){m_relationTextColorMap.insert(column, color);};void AppendcolTextColorList(QColor color){m_colTextColor.append(color);};// QAbstractItemModel interface
public:virtual QVariant data(const QModelIndex &index, int role) const override{if(role == Qt::TextAlignmentRole){return Qt::AlignCenter;}else if(role == Qt::TextColorRole){if(relation(index.column()).isValid()){QColor color = m_relationTextColorMap.value(index.column());if(color.isValid()){return color;}}if(m_colTextColor.size() > index.column()){QColor color = m_colTextColor.at(index.column());if(color.isValid()){return color;}}}return QSqlRelationalTableModel::data(index, role);};
};class QViewObject : public QObject
{Q_OBJECTpublic:QViewObject(QTableView* pTableView, QSqlRelationalTableModelEx* pSqlRelationalTableModel):m_pTableView(pTableView), m_pSqlRelationalTableModel(pSqlRelationalTableModel),m_bAdd(false), m_bDel(false), m_bUpdate(false){};private:QMenu m_menu;QTableView* m_pTableView;QSqlRelationalTableModelEx* m_pSqlRelationalTableModel;bool m_bAdd;bool m_bDel;bool m_bUpdate;QList<QAction *> m_ActionList;public:void AddAction(QAction* pAction){m_ActionList.append(pAction);this->connectViewClick();}void SetMode(bool bAdd = false, bool bDel=false, bool bUpdate = false){m_bAdd = bAdd;m_bDel = bDel;m_bUpdate = bUpdate;if(m_bUpdate){m_pTableView->setEditTriggers(QAbstractItemView::DoubleClicked);}else{m_pTableView->setEditTriggers(QAbstractItemView::NoEditTriggers);}this->connectViewClick();}void connectViewClick(){m_menu.clear();QAction *pActionRefresh= new QAction("刷新", this);m_menu.addAction(pActionRefresh);connect(pActionRefresh, &QAction::triggered, this, [&] {m_pSqlRelationalTableModel->select();});if(m_bAdd){QAction *pActionAdd= new QAction("增加", this);m_menu.addAction(pActionAdd);connect(pActionAdd, &QAction::triggered, this, [&] {int rowCount = m_pSqlRelationalTableModel->rowCount();m_pSqlRelationalTableModel->insertRow(rowCount);//m_pSqlRelationalTableModel->setData(m_pSqlRelationalTableModel->index(rowCount, 0), "");m_pTableView->setEditTriggers(QTableView::DoubleClicked);m_pTableView->scrollToBottom();});}if(m_bDel){QAction *pActionDel= new QAction("删除", this);m_menu.addAction(pActionDel);connect(pActionDel, &QAction::triggered, this, [&] {if (QMessageBox::warning(m_pTableView, "提示", "确定删除选中行?", QMessageBox::Yes, QMessageBox::No) == QMessageBox::No) {m_pSqlRelationalTableModel->revertAll();return;}QModelIndexList modelIndexList = m_pTableView->selectionModel()->selectedRows();foreach(QModelIndex modelIndex, modelIndexList){m_pSqlRelationalTableModel->removeRow(modelIndex.row());}m_pSqlRelationalTableModel->submitAll();});}if(m_bUpdate){QAction *pActionUpdate= new QAction("保存", this);m_menu.addAction(pActionUpdate);connect(pActionUpdate, &QAction::triggered, this, [&] {m_pSqlRelationalTableModel->submitAll();});}if(m_bAdd || m_bDel || m_bUpdate){QAction *pActionRevert= new QAction("撤销", this);m_menu.addAction(pActionRevert);connect(pActionRevert, &QAction::triggered, this, [&] {m_pSqlRelationalTableModel->revertAll();});}m_menu.addSeparator();foreach(QAction* pAction, m_ActionList){m_menu.addAction(pAction);}connect(m_pTableView, &QTableView::customContextMenuRequested, this, [&]{m_menu.move(m_pTableView->cursor().pos());m_menu.show();});};
};template<typename TEMP>
class QSqlRelationalTableModelView
{public:QSqlRelationalTableModelView(QString qsTableName/*关联的表名*/, bool bCreatView = false/*是否创建界面类QTableView*/);~QSqlRelationalTableModelView();private:QTableView* m_pTableView;QViewObject* m_pViewObject;QSqlRelationalTableModelEx* m_pSqlRelationalTableModel;bool m_bView;//存在ViewQString m_qsTableName;MySqlOper* m_pMySqlOper;QList<TSerialisation> m_tSerialisationList;QList<QString> m_headList;QList<QString> m_headListTitle;QMap<int, QSqlRelationalTableModelView*> m_relationModelViewMap;QString m_qsDateFormat;private:QSqlRelationalTableModelView*  findAndCreatRelationalTableModelView(int column);protected:QList<TSerialisation> getSerialisationList(){return m_tSerialisationList;};QList<QString> getHeadList(){return m_headList;};QList<QString> getHeadListTitle(){return m_headListTitle;};QString getTableName(){return m_qsTableName;};QString getDateFormat(){return m_qsDateFormat;};public:QTableView* GetTableView();//获得列表void SetRelation(int column/*当前表列号*/,const QString &aTableName/*关联外键的父表名*/,const QString &indexCol/*关联父表的字段名*/,const QString &displayCol/*显示父表的字段名*/,const QColor &colorCol = QColor()/*显示父表颜色,优先级高于显示颜色*/);//设置外键bool SQL_Init(QString csUser, QString csPasswd, QString csDB, QString csHost = "127.0.0.1", int nPort = 3306);//数据库初始化void SetSerialisationHead(const QString qsHead/*数据库表字段名*/, const QString qsHeadTitle/*列表头标题*/, TSerialisation tSerialisation, const QColor &colorCol = QColor()/*显示颜色*/);//设置序列化表头QList<TEMP> SQL_QueryData(QString csSql);//前提SetSerialisationHead必须按照表结构严格输入,否则请使用SQL_QuerySerialisation方法,Sql查询本表数据,返回本表字段必须是本表所有字段且不包含其他表字段char* SQL_QuerySerialisation(QString csSql, int& nQuerySize/*结果集个数*/, QList<TSerialisation> tSerialisationList = QList<TSerialisation>());//序列化查询数据库 需删除返回值void InitView();//初始化列表,如果不创建界面类QTableView无需调用QSqlRelationalTableModelEx* GetModel();//获得外键QSqlRelationalTableModelEx*void SetMode(bool bAdd = false, bool bDel=false, bool bUpdate = false);//设置列表的增删改是否可用void SetSortingEnabled(bool enable); //启用排序 默认是禁用排序的void AddAction(QAction* pAction);//添加自定义右键菜单QSqlTableModel *RelationModel(int column);//获得外键表模型void SetDateTimeFormat(QString qsDateFormat);//设置时间格式化格式/*外键列表函数*/QTableView* GetRelationTableView(int column);//获得外键列表QSqlRelationalTableModelEx* GetRelationModel(int column);//获得外键QSqlRelationalTableModelEx*void SetRelationModelHead(int column, const QString qsHead/*数据库表字段名*/, const QString qsHeadTitle/*列表头标题*/, const QColor &colorCol = QColor()/*显示颜色*/);//设置外键表表头void InitRelationView(int column);//初始化外键表列表void SetRelationMode(int column, bool bAdd = false, bool bDel=false, bool bUpdate = false);//设置列表的增删改是否可用void SetRelationSortingEnabled(int column, bool enable); //启用外键表排序 默认是禁用排序的void AddRelationAction(int column, QAction* pAction);//添加外键表自定义右键菜单
};template <typename TEMP>
QSqlRelationalTableModelView<TEMP>::QSqlRelationalTableModelView(QString qsTableName, bool bCreatView):m_bView(bCreatView), m_qsTableName(qsTableName)
{m_pMySqlOper = MySqlOper::GetInstance();m_pSqlRelationalTableModel = new QSqlRelationalTableModelEx(nullptr, *m_pMySqlOper->SQL_DataBase());if(m_bView){m_pTableView = new QTableView;m_pViewObject = new QViewObject(m_pTableView, m_pSqlRelationalTableModel);m_pViewObject->connectViewClick();}m_qsDateFormat = "yyyy/MM/dd HH:mm:ss";
}template<typename TEMP>
QSqlRelationalTableModelView<TEMP>::~QSqlRelationalTableModelView()
{if(m_pSqlRelationalTableModel != nullptr){delete m_pSqlRelationalTableModel;}MySqlOper::ReleaseInstance();
}template<typename TEMP>
QSqlRelationalTableModelView<TEMP> *QSqlRelationalTableModelView<TEMP>::findAndCreatRelationalTableModelView(int column)
{QSqlRelationalTableModelView* pSqlRelationalTableModelView = m_relationModelViewMap.value(column, nullptr);if(pSqlRelationalTableModelView == nullptr){pSqlRelationalTableModelView = new QSqlRelationalTableModelView(RelationModel(column)->tableName(), true);pSqlRelationalTableModelView->SetDateTimeFormat(m_qsDateFormat);}m_relationModelViewMap.insert(column, pSqlRelationalTableModelView);return pSqlRelationalTableModelView;
}template<typename TEMP>
void QSqlRelationalTableModelView<TEMP>::InitView()
{if(m_pSqlRelationalTableModel != nullptr && m_pTableView != nullptr){m_pTableView->setAlternatingRowColors(true);m_pTableView->setContextMenuPolicy(Qt::CustomContextMenu);m_pTableView->setSelectionBehavior(QAbstractItemView::SelectRows);//        m_pTableView->horizontalHeader()->setSectionResizeMode(QHeaderView::ResizeToContents);//自适应宽度m_pTableView->horizontalHeader()->setMinimumSectionSize(100);m_pTableView->horizontalHeader()->setDefaultAlignment(Qt::AlignCenter);m_pTableView->verticalHeader()->setDefaultAlignment(Qt::AlignCenter);m_pTableView->setModel(m_pSqlRelationalTableModel);for(int i = 0; i < m_tSerialisationList.size(); i++){if(m_tSerialisationList.at(i).eType == eDateTime){m_pTableView->setItemDelegateForColumn(i, new QDateTimeItemDelegate(m_qsDateFormat));}}m_pTableView->setItemDelegate(new QSqlRelationalDelegate(m_pTableView));m_pSqlRelationalTableModel->setTable(m_qsTableName);m_pSqlRelationalTableModel->setEditStrategy(QSqlTableModel::OnManualSubmit);//编辑后需要Submit才能更改for(int i = 0; i < m_headList.size(); i++){m_pSqlRelationalTableModel->setHeaderData(m_pSqlRelationalTableModel->fieldIndex(m_headList.at(i)), Qt::Horizontal, m_headListTitle.at(i));}m_pSqlRelationalTableModel->select();}
}template<typename TEMP>
QSqlRelationalTableModelEx *QSqlRelationalTableModelView<TEMP>::GetModel()
{return m_pSqlRelationalTableModel;
}template<typename TEMP>
void QSqlRelationalTableModelView<TEMP>::SetMode(bool bAdd, bool bDel, bool bUpdate)
{if(m_pViewObject != nullptr){m_pViewObject->SetMode(bAdd, bDel, bUpdate);}
}template<typename TEMP>
void QSqlRelationalTableModelView<TEMP>::SetSortingEnabled(bool enable)
{if (m_pTableView != nullptr) {m_pTableView->setSortingEnabled(enable);m_pTableView->horizontalHeader()->setSortIndicator(0, Qt::AscendingOrder);}
}template<typename TEMP>
void QSqlRelationalTableModelView<TEMP>::AddAction(QAction *pAction)
{if(m_pViewObject != nullptr){m_pViewObject->AddAction(pAction);}
}template<typename TEMP>
QSqlTableModel *QSqlRelationalTableModelView<TEMP>::RelationModel(int column)
{if(m_pSqlRelationalTableModel != nullptr){return m_pSqlRelationalTableModel->relationModel(column);}return nullptr;
}template<typename TEMP>
QTableView *QSqlRelationalTableModelView<TEMP>::GetRelationTableView(int column)
{QSqlRelationalTableModelView* pSqlRelationalTableModelView = findAndCreatRelationalTableModelView(column);if(pSqlRelationalTableModelView != nullptr){return pSqlRelationalTableModelView->GetTableView();}return nullptr;
}template<typename TEMP>
QSqlRelationalTableModelEx *QSqlRelationalTableModelView<TEMP>::GetRelationModel(int column)
{QSqlRelationalTableModelView* pSqlRelationalTableModelView = findAndCreatRelationalTableModelView(column);if(pSqlRelationalTableModelView != nullptr){return pSqlRelationalTableModelView->GetModel();}return nullptr;
}template<typename TEMP>
void QSqlRelationalTableModelView<TEMP>::SetRelationModelHead(int column, const QString qsHead, const QString qsHeadTitle, const QColor &colorCol)
{QSqlRelationalTableModelView* pSqlRelationalTableModelView = findAndCreatRelationalTableModelView(column);if(pSqlRelationalTableModelView != nullptr){pSqlRelationalTableModelView->SetSerialisationHead(qsHead, qsHeadTitle, TSerialisation(eInt, sizeof(int))/*序列化无效,做SQL查询用*/, colorCol);}
}template<typename TEMP>
void QSqlRelationalTableModelView<TEMP>::InitRelationView(int column)
{QSqlRelationalTableModelView* pSqlRelationalTableModelView = findAndCreatRelationalTableModelView(column);if(pSqlRelationalTableModelView != nullptr && pSqlRelationalTableModelView->GetModel() != nullptr && pSqlRelationalTableModelView->GetTableView() != nullptr){pSqlRelationalTableModelView->GetTableView()->setAlternatingRowColors(true);pSqlRelationalTableModelView->GetTableView()->setContextMenuPolicy(Qt::CustomContextMenu);pSqlRelationalTableModelView->GetTableView()->setSelectionBehavior(QAbstractItemView::SelectRows);//        pSqlRelationalTableModelView->GetTableView()->horizontalHeader()->setSectionResizeMode(QHeaderView::ResizeToContents);//自适应宽度pSqlRelationalTableModelView->GetTableView()->horizontalHeader()->setMinimumSectionSize(100);pSqlRelationalTableModelView->GetTableView()->horizontalHeader()->setDefaultAlignment(Qt::AlignCenter);pSqlRelationalTableModelView->GetTableView()->verticalHeader()->setDefaultAlignment(Qt::AlignCenter);pSqlRelationalTableModelView->GetTableView()->setModel(pSqlRelationalTableModelView->GetModel());for(int i = 0; i < pSqlRelationalTableModelView->getSerialisationList().size(); i++){if(pSqlRelationalTableModelView->getSerialisationList().at(i).eType == eDateTime){pSqlRelationalTableModelView->GetTableView()->setItemDelegateForColumn(i, new QDateTimeItemDelegate(pSqlRelationalTableModelView->getDateFormat()));}}pSqlRelationalTableModelView->GetTableView()->setItemDelegate(new QSqlRelationalDelegate(pSqlRelationalTableModelView->GetTableView()));pSqlRelationalTableModelView->GetModel()->setTable(pSqlRelationalTableModelView->getTableName());pSqlRelationalTableModelView->GetModel()->setEditStrategy(QSqlTableModel::OnManualSubmit);//编辑后需要Submit才能更改for(int i = 0; i < pSqlRelationalTableModelView->getHeadList().size(); i++){pSqlRelationalTableModelView->GetModel()->setHeaderData(pSqlRelationalTableModelView->GetModel()->fieldIndex(pSqlRelationalTableModelView->getHeadList().at(i)), Qt::Horizontal, pSqlRelationalTableModelView->getHeadListTitle().at(i));}pSqlRelationalTableModelView->GetModel()->select();}
}template<typename TEMP>
void QSqlRelationalTableModelView<TEMP>::SetRelationMode(int column, bool bAdd, bool bDel, bool bUpdate)
{QSqlRelationalTableModelView* pSqlRelationalTableModelView = findAndCreatRelationalTableModelView(column);if(pSqlRelationalTableModelView != nullptr){pSqlRelationalTableModelView->SetMode(bAdd, bDel, bUpdate);}
}template<typename TEMP>
void QSqlRelationalTableModelView<TEMP>::SetRelationSortingEnabled(int column, bool enable)
{QSqlRelationalTableModelView* pSqlRelationalTableModelView = findAndCreatRelationalTableModelView(column);if(pSqlRelationalTableModelView != nullptr){pSqlRelationalTableModelView->SetSortingEnabled(enable);}
}template<typename TEMP>
void QSqlRelationalTableModelView<TEMP>::AddRelationAction(int column, QAction *pAction)
{QSqlRelationalTableModelView* pSqlRelationalTableModelView = findAndCreatRelationalTableModelView(column);if(pSqlRelationalTableModelView != nullptr){pSqlRelationalTableModelView->AddAction(pAction);}
}template<typename TEMP>
QTableView *QSqlRelationalTableModelView<TEMP>::GetTableView()
{return m_pTableView;
}template<typename TEMP>
void QSqlRelationalTableModelView<TEMP>::SetRelation(int column, const QString &aTableName, const QString &indexCol,const QString &displayColconst, const QColor &colorCol)
{if(m_pSqlRelationalTableModel != nullptr){m_pSqlRelationalTableModel->setRelation(column, QSqlRelation(aTableName, indexCol, displayColconst));m_pSqlRelationalTableModel->InsertRelationTextColorMap(column, colorCol);}
}template<typename TEMP>
bool QSqlRelationalTableModelView<TEMP>::SQL_Init(QString csUser, QString csPasswd, QString csDB, QString csHost, int nPort)
{m_pMySqlOper->SQL_SetPro(csUser, csPasswd, csDB, csHost, nPort);return m_pMySqlOper->SQL_Connect();
}template<typename TEMP>
void QSqlRelationalTableModelView<TEMP>::SetSerialisationHead(const QString qsHead, const QString qsHeadTitle, TSerialisation tSerialisation, const QColor &colorCol)
{m_headList.append(qsHead);m_headListTitle.append(qsHeadTitle);m_tSerialisationList.append(tSerialisation);GetModel()->AppendcolTextColorList(colorCol);
}template<typename TEMP>
char *QSqlRelationalTableModelView<TEMP>::SQL_QuerySerialisation(QString csSql, int& nQuerySize, QList<TSerialisation> tSerialisationList)
{return m_pMySqlOper->SQL_QuerySerialisation(csSql, nQuerySize, tSerialisationList);
}template<typename TEMP>
QList<TEMP> QSqlRelationalTableModelView<TEMP>::SQL_QueryData(QString csSql)
{int nQuerySize = 0;char* p = m_pMySqlOper->SQL_QuerySerialisation(csSql, nQuerySize, m_tSerialisationList);QList<TEMP> resultList;if(p != nullptr && nQuerySize > 0){TEMP* pTemp = (TEMP*)p;for(int i = 0; i < nQuerySize; i++){TEMP temp;memcpy(&temp, &pTemp[i], sizeof (TEMP));resultList.append(temp);}delete [] p;}return resultList;
}template<typename TEMP>
void QSqlRelationalTableModelView<TEMP>::SetDateTimeFormat(QString qsDateFormat)
{m_qsDateFormat  = qsDateFormat;
}
#endif // QSQLRELATIONALTABLEMODELVIEW_H
//使用方法
//xxx.h
#pragma pack(push,1)//按字节对齐beginstruct tTemp{int id;int EInt;float EFloat;double EDouble;qint64 EDateTime;
};struct tTempJoin{int id;char EString1[255];char EString2[255];double EDouble;qint64 EDateTime;
};
#pragma pack(pop)//按字节对齐endprivate:QSqlRelationalTableModelView<tTemp>* m_pSqlRelationalTableModelView;//xxx.cpp
static bool bCreat = false;if(!bCreat){/*表结构CREATE TABLE `ModelTest` (`id` int NOT NULL AUTO_INCREMENT,`EInt` int NOT NULL,`EFloat` float NOT NULL,`EDouble` double NOT NULL,`EDateTime` datetime NOT NULL,PRIMARY KEY (`id`),KEY `name` (`EInt`) USING BTREE,KEY `name2` (`EFloat`),CONSTRAINT `name` FOREIGN KEY (`EInt`) REFERENCES `DICT` (`id`) ON DELETE RESTRICT ON UPDATE RESTRICT,CONSTRAINT `name2` FOREIGN KEY (`EFloat`) REFERENCES `DICT2` (`id`));CREATE TABLE `DICT` (`id` int NOT NULL,`name` varchar(255) NOT NULL,KEY `id` (`id`));CREATE TABLE `DICT2` (`id` float NOT NULL,`name` varchar(255) NOT NULL,KEY `id` (`id`))*/m_pSqlRelationalTableModelView = new QSqlRelationalTableModelView<tTemp>("ModelTest", true);m_pSqlRelationalTableModelView->SQL_Init("root", "0000", "RadioMonitor");m_pSqlRelationalTableModelView->SetSerialisationHead("id", "序号", TSerialisation(eInt, sizeof(int)));m_pSqlRelationalTableModelView->SetSerialisationHead("EInt", "Int值", TSerialisation(eInt, sizeof(int)));m_pSqlRelationalTableModelView->SetSerialisationHead("EFloat", "Float值", TSerialisation(eFloat, sizeof(float)));m_pSqlRelationalTableModelView->SetSerialisationHead("EDouble", "Double值", TSerialisation(eDouble, sizeof(double)));m_pSqlRelationalTableModelView->SetSerialisationHead("EDateTime", "DateTime值", TSerialisation(eDateTime, sizeof(qint64)));m_pSqlRelationalTableModelView->InitView();m_pSqlRelationalTableModelView->SetRelation(1, "DICT","id","name", QColor(0, 255, 0));m_pSqlRelationalTableModelView->SetRelation(2, "DICT2","id","name", QColor(255, 255, 0));m_pSqlRelationalTableModelView->GetTableView()->resize(600, 300);m_pSqlRelationalTableModelView->SetMode(true, true, true);m_pSqlRelationalTableModelView->SetSortingEnabled(true);m_pSqlRelationalTableModelView->GetTableView()->setWindowTitle("数据库示例");//创建外键表1m_pSqlRelationalTableModelView->SetRelationModelHead(1, "id", "序号");m_pSqlRelationalTableModelView->SetRelationModelHead(1, "name", "名称");m_pSqlRelationalTableModelView->InitRelationView(1);m_pSqlRelationalTableModelView->GetRelationTableView(1)->resize(300, 150);m_pSqlRelationalTableModelView->SetRelationMode(1, true, true, true);m_pSqlRelationalTableModelView->SetRelationSortingEnabled(1, true);m_pSqlRelationalTableModelView->GetRelationTableView(1)->setWindowTitle("外键表1示例");//m_pSqlRelationalTableModelView->GetRelationModel(1)->setFilter("id=1");//设置过滤//创建外键表2m_pSqlRelationalTableModelView->SetRelationModelHead(2, "id", "序号");m_pSqlRelationalTableModelView->SetRelationModelHead(2, "name", "名称");m_pSqlRelationalTableModelView->InitRelationView(2);m_pSqlRelationalTableModelView->GetRelationTableView(2)->resize(300, 150);m_pSqlRelationalTableModelView->SetRelationMode(2, true, true, true);m_pSqlRelationalTableModelView->SetRelationSortingEnabled(2, true);m_pSqlRelationalTableModelView->GetRelationTableView(2)->setWindowTitle("外键表2示例");//m_pSqlRelationalTableModelView->GetRelationModel(2)->setFilter("id=1");//设置过滤//自定义右键菜单QAction *pAction1= new QAction("外键列表1示例", this);connect(pAction1, &QAction::triggered, this, [&] {const QRect screen = QGuiApplication::screens().at(0)->geometry();m_pSqlRelationalTableModelView->GetRelationTableView(1)->move(screen.center() - m_pSqlRelationalTableModelView->GetRelationTableView(1)->rect().center());m_pSqlRelationalTableModelView->GetRelationTableView(1)->show();});m_pSqlRelationalTableModelView->AddAction(pAction1);QAction *pAction2= new QAction("外键列表2示例", this);connect(pAction2, &QAction::triggered, this, [&] {const QRect screen = QGuiApplication::screens().at(0)->geometry();m_pSqlRelationalTableModelView->GetRelationTableView(2)->move(screen.center() - m_pSqlRelationalTableModelView->GetRelationTableView(1)->rect().center());m_pSqlRelationalTableModelView->GetRelationTableView(2)->show();});m_pSqlRelationalTableModelView->AddAction(pAction2);const QRect screen = QGuiApplication::screens().at(0)->geometry();m_pSqlRelationalTableModelView->GetTableView()->move(screen.center() - m_pSqlRelationalTableModelView->GetTableView()->rect().center());//默认查询,SQL语句需返回本表所有字段且不含其他表字段QList<tTemp> list= m_pSqlRelationalTableModelView->SQL_QueryData("SELECT * FROM ModelTest LIMIT 0,100");qDebug() << list.size();//INNER JOIN查询 或者 LEFT JOIN查询QList<TSerialisation> tSerialisationList;tSerialisationList << TSerialisation(eInt, sizeof(int));tSerialisationList << TSerialisation(eString, 255);tSerialisationList << TSerialisation(eString, 255);tSerialisationList << TSerialisation(eDouble, sizeof(double));tSerialisationList << TSerialisation(eDateTime, sizeof(qint64));int nQuerySize = 0;tTempJoin* ptTempJoin= (tTempJoin*)m_pSqlRelationalTableModelView->SQL_QuerySerialisation("SELECT a.id, b.name, c.name, a.EDouble, a.EDateTime FROM ModelTest a INNER JOIN DICT b, DICT2 c LIMIT 0,100", nQuerySize, tSerialisationList);if(nQuerySize > 1)qDebug() << nQuerySize << ptTempJoin[0].EString1 << ptTempJoin[0].EString2;elseqDebug() << nQuerySize;}m_pSqlRelationalTableModelView->GetTableView()->show();bCreat = true;

相关文章:

QT-Mysql数据库图形化接口

QT sql mysqloper.h qsqlrelationaltablemodelview.h /************************************************************************* 接口描述&#xff1a;Mysql数据库图形化接口 拟制&#xff1a; 接口版本&#xff1a;V1.0 时间&#xff1a;20230727 说明&#xff1a;支…...

LeetCode150道面试经典题-- 合并两个有序链表(简单)

1.题目 将两个升序链表合并为一个新的 升序 链表并返回。新链表是通过拼接给定的两个链表的所有节点组成的。 2.示例 示例 1&#xff1a; 输入&#xff1a;l1 [1,2,4], l2 [1,3,4] 输出&#xff1a;[1,1,2,3,4,4] 示例 2&#xff1a; 输入&#xff1a;l1 [], l2 [] 输…...

GitHub 如何部署写好的H5静态页面

感谢粉皮zu的私信&#xff0c;又有素材写笔记了。(●’◡’●) 刚好记录一下我示例代码的GitHub部署配置&#xff0c;以便于后期追加仓库。 效果 环境 gitwin 步骤 第一步 新建仓库 第二步 拉取代码 将仓库clone到本地 git clone 地址第三步 部署文件 新建.github\workflo…...

SharkTeam:Worldcoin运营数据及业务安全分析

Worldcoin的白皮书中声明&#xff0c;Worldcoin旨在构建一个连接全球人类的新型数字经济系统&#xff0c;由OpenAI创始人Sam Altman于2020年发起。通过区块链技术在Web3世界中实现更加公平、开放和包容的经济体系&#xff0c;并将所有权赋予每个人。并且希望让全世界每一个人都…...

C语言编程练习

考点&#xff1a;【字符串】【数组】 题目1. 打印X 题目描述 输入一个正整数N&#xff0c; 你需要按样例的方式返回一个字符串列表。 1≤N≤15。 样例 1&#xff1a; 输入&#xff1a;1 输出&#xff1a;[“X”] X样例 2&#xff1a; 输入&#xff1a;2 [“XX”, “XX”] …...

vue入门(增查改!)

<template><div><!-- 搜索栏 --><el-card id"search"><el-row><el-col :span"20"><el-input v-model"searchModel.name" placeholder"根据名字查询"></el-input><el-input v-mode…...

移动端身份证识别技术的应用,告别手动录入证件信息

随着移动互联网的的发展&#xff0c;越来越多的公司都推出了自己的移动APP&#xff0c;这些APP多数都涉及到个人身份证信息的输入认证&#xff08;即实名认证&#xff09;&#xff0c;如果手动去输入身份证号码和姓名&#xff0c;速度非常慢&#xff0c;且用户体验非常差。为了…...

网络通信原理TCP字段解析(第四十七课)

字段含义Source Port(源端口号)源端口,标识哪...

uniapp微信小程序消息订阅快速上手

一、微信公众平台小程序开通消息订阅并设置模板 这边的模板id和详细内容后续前后端需要使用 二、uniapp前端 需要是一个button触发 js&#xff1a; wx.getSetting({success(res){console.log(res)if(res.authSetting[scope.subscribeMessage]){// 业务逻辑}else{uni.request…...

MySQL 根据多字段查询重复数据

MySQL 根据多字段查询重复数据 在实际的数据库应用中&#xff0c;我们经常需要根据多个字段来查询重复的数据。MySQL 提供了一些方法来实现这个功能&#xff0c;让我们能够快速准确地找到和处理重复数据。本文将介绍如何使用 MySQL 来根据多字段查询重复数据&#xff0c;并提供…...

Markdown编辑器 Mac版Typora功能介绍

Typora mac是一款跨平台的Markdown编辑器&#xff0c;支持Windows、MacOS和Linux操作系统。它具有实时预览功能&#xff0c;能够自动将Markdown文本转换为漂亮的排版效果&#xff0c;让用户专注于写作内容而不必关心格式调整。 Typora Mac版除了支持常见的Markdown语法外&#…...

el-form自定义校验规则

Vue 的 el-form 组件可以使用自定义校验规则进行表单验证。自定义校验规则可以通过传递一个函数来实现&#xff0c;该函数接受要校验的字段的值作为参数&#xff0c;并返回一个布尔值或一个 Promise 对象。 下面是一个示例&#xff0c;演示如何在 el-form 中使用自定义校验规则…...

xml对象与字符串互换

很多老系统&#xff0c;特别是C的系统&#xff0c;可能数据结构采用的xml。xml对java来说没有什么&#xff0c;但是C来说&#xff0c;可能还有个顺序问题&#xff0c;毕竟c没有那么多通用类库。 2 xstream 先说依赖&#xff0c;我本来不想升级&#xff0c;但是有个问题卡者就给…...

单例模式和多例模式和工厂模式

1单例设计模式 学习目标 能够使用单例设计模式设计代码 内容讲解 正常情况下一个类可以创建多个对象 public static void main(String[] args) {// 正常情况下一个类可以创建多个对象Person p1 new Person();Person p2 new Person();Person p3 new Person(); }如果说有…...

【网络架构】华为hw交换机网络高可用网络架构拓扑图以及配置

一、网络拓扑 1.网络架构 核心层:接入网络----路由器 汇聚层:vlan间通信 创建vlan ---什么是vlan:虚拟局域网,在大型平面网络中,为了实现广播控制引入了vlan,可以根据功能或者部门等创建vlan,再把相关的端口加入到vlan.为了实现不用交换机上的相同vlan通信,需要配置中继,为了…...

信也科技一面凉经

1.在项目经历里挑一个详细介绍一下 项目的应用场景 2.项目里用到多线程是怎么用的&#xff1f;回答&#xff1a;线程池 用通过 ThreadPoolExecutor 构造函数的方式创建的线程池 3.线程池有哪些重要参数&#xff1f;回答&#xff1a;核心线程数、最大线程数、阻塞队列类型、…...

AI商业化如何落地?看设计师如何利用AI细化工作流

自从AI爆火之后&#xff0c;人类是否会被AI取代一直都是打工人格外关注的问题。 而最近&#xff0c;在小编深入探索到我们用户的使用情况后&#xff0c;发现已经有人拿神采PromeAI直接实现了商业应用&#xff0c;将AI的设计创意应用得淋漓尽致&#xff0c;并且直接进军房地产及…...

论文阅读 - Understanding Diffusion Models: A Unified Perspective

文章目录 1 概述2 背景知识2.1 直观的例子2.2 Evidence Lower Bound(ELBO)2.3 Variational Autoencoders(VAE)2.4 Hierachical Variational Autoencoders(HVAE) 3 Variational Diffusion Models(VDM)4 三个等价的解释4.1 预测图片4.2 预测噪声4.3 预测分数 5 Guidance5.1 Class…...

[Python进阶] 定制类:模拟篇

4.10.5 模拟篇 4.10.5.1 call 通过__call__魔法方法可以像使用函数一样使用对象。通过括号的方式调用&#xff0c;也可以像函数一样传入参数&#xff1a; from icecream import icclass Multiplier:def __init__(self, mul):self.mul muldef __call__(self, arg):return se…...

HTML5 游戏开发实战 | 五子棋

01、五子棋游戏设计的思路 在下棋过程中&#xff0c;为了保存下过的棋子的信息&#xff0c;使用数组 chessData。chessData&#xff3b;x&#xff3d;&#xff3b;y&#xff3d;存储棋盘(x&#xff0c;y)处棋子信息&#xff0c;1 代表黑子&#xff0c;2 代表白子&#xff0c;0…...

网络编程(Modbus进阶)

思维导图 Modbus RTU&#xff08;先学一点理论&#xff09; 概念 Modbus RTU 是工业自动化领域 最广泛应用的串行通信协议&#xff0c;由 Modicon 公司&#xff08;现施耐德电气&#xff09;于 1979 年推出。它以 高效率、强健性、易实现的特点成为工业控制系统的通信标准。 包…...

微信小程序之bind和catch

这两个呢&#xff0c;都是绑定事件用的&#xff0c;具体使用有些小区别。 官方文档&#xff1a; 事件冒泡处理不同 bind&#xff1a;绑定的事件会向上冒泡&#xff0c;即触发当前组件的事件后&#xff0c;还会继续触发父组件的相同事件。例如&#xff0c;有一个子视图绑定了b…...

云计算——弹性云计算器(ECS)

弹性云服务器&#xff1a;ECS 概述 云计算重构了ICT系统&#xff0c;云计算平台厂商推出使得厂家能够主要关注应用管理而非平台管理的云平台&#xff0c;包含如下主要概念。 ECS&#xff08;Elastic Cloud Server&#xff09;&#xff1a;即弹性云服务器&#xff0c;是云计算…...

Debian系统简介

目录 Debian系统介绍 Debian版本介绍 Debian软件源介绍 软件包管理工具dpkg dpkg核心指令详解 安装软件包 卸载软件包 查询软件包状态 验证软件包完整性 手动处理依赖关系 dpkg vs apt Debian系统介绍 Debian 和 Ubuntu 都是基于 Debian内核 的 Linux 发行版&#xff…...

大型活动交通拥堵治理的视觉算法应用

大型活动下智慧交通的视觉分析应用 一、背景与挑战 大型活动&#xff08;如演唱会、马拉松赛事、高考中考等&#xff09;期间&#xff0c;城市交通面临瞬时人流车流激增、传统摄像头模糊、交通拥堵识别滞后等问题。以演唱会为例&#xff0c;暖城商圈曾因观众集中离场导致周边…...

相机Camera日志分析之三十一:高通Camx HAL十种流程基础分析关键字汇总(后续持续更新中)

【关注我,后续持续新增专题博文,谢谢!!!】 上一篇我们讲了:有对最普通的场景进行各个日志注释讲解,但相机场景太多,日志差异也巨大。后面将展示各种场景下的日志。 通过notepad++打开场景下的日志,通过下列分类关键字搜索,即可清晰的分析不同场景的相机运行流程差异…...

C++ 求圆面积的程序(Program to find area of a circle)

给定半径r&#xff0c;求圆的面积。圆的面积应精确到小数点后5位。 例子&#xff1a; 输入&#xff1a;r 5 输出&#xff1a;78.53982 解释&#xff1a;由于面积 PI * r * r 3.14159265358979323846 * 5 * 5 78.53982&#xff0c;因为我们只保留小数点后 5 位数字。 输…...

【数据分析】R版IntelliGenes用于生物标志物发现的可解释机器学习

禁止商业或二改转载&#xff0c;仅供自学使用&#xff0c;侵权必究&#xff0c;如需截取部分内容请后台联系作者! 文章目录 介绍流程步骤1. 输入数据2. 特征选择3. 模型训练4. I-Genes 评分计算5. 输出结果 IntelliGenesR 安装包1. 特征选择2. 模型训练和评估3. I-Genes 评分计…...

C#学习第29天:表达式树(Expression Trees)

目录 什么是表达式树&#xff1f; 核心概念 1.表达式树的构建 2. 表达式树与Lambda表达式 3.解析和访问表达式树 4.动态条件查询 表达式树的优势 1.动态构建查询 2.LINQ 提供程序支持&#xff1a; 3.性能优化 4.元数据处理 5.代码转换和重写 适用场景 代码复杂性…...

Vue 模板语句的数据来源

&#x1f9e9; Vue 模板语句的数据来源&#xff1a;全方位解析 Vue 模板&#xff08;<template> 部分&#xff09;中的表达式、指令绑定&#xff08;如 v-bind, v-on&#xff09;和插值&#xff08;{{ }}&#xff09;都在一个特定的作用域内求值。这个作用域由当前 组件…...