当前位置: 首页 > 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…...

第19节 Node.js Express 框架

Express 是一个为Node.js设计的web开发框架&#xff0c;它基于nodejs平台。 Express 简介 Express是一个简洁而灵活的node.js Web应用框架, 提供了一系列强大特性帮助你创建各种Web应用&#xff0c;和丰富的HTTP工具。 使用Express可以快速地搭建一个完整功能的网站。 Expre…...

手游刚开服就被攻击怎么办?如何防御DDoS?

开服初期是手游最脆弱的阶段&#xff0c;极易成为DDoS攻击的目标。一旦遭遇攻击&#xff0c;可能导致服务器瘫痪、玩家流失&#xff0c;甚至造成巨大经济损失。本文为开发者提供一套简洁有效的应急与防御方案&#xff0c;帮助快速应对并构建长期防护体系。 一、遭遇攻击的紧急应…...

DockerHub与私有镜像仓库在容器化中的应用与管理

哈喽&#xff0c;大家好&#xff0c;我是左手python&#xff01; Docker Hub的应用与管理 Docker Hub的基本概念与使用方法 Docker Hub是Docker官方提供的一个公共镜像仓库&#xff0c;用户可以在其中找到各种操作系统、软件和应用的镜像。开发者可以通过Docker Hub轻松获取所…...

Auto-Coder使用GPT-4o完成:在用TabPFN这个模型构建一个预测未来3天涨跌的分类任务

通过akshare库&#xff0c;获取股票数据&#xff0c;并生成TabPFN这个模型 可以识别、处理的格式&#xff0c;写一个完整的预处理示例&#xff0c;并构建一个预测未来 3 天股价涨跌的分类任务 用TabPFN这个模型构建一个预测未来 3 天股价涨跌的分类任务&#xff0c;进行预测并输…...

1.3 VSCode安装与环境配置

进入网址Visual Studio Code - Code Editing. Redefined下载.deb文件&#xff0c;然后打开终端&#xff0c;进入下载文件夹&#xff0c;键入命令 sudo dpkg -i code_1.100.3-1748872405_amd64.deb 在终端键入命令code即启动vscode 需要安装插件列表 1.Chinese简化 2.ros …...

新能源汽车智慧充电桩管理方案:新能源充电桩散热问题及消防安全监管方案

随着新能源汽车的快速普及&#xff0c;充电桩作为核心配套设施&#xff0c;其安全性与可靠性备受关注。然而&#xff0c;在高温、高负荷运行环境下&#xff0c;充电桩的散热问题与消防安全隐患日益凸显&#xff0c;成为制约行业发展的关键瓶颈。 如何通过智慧化管理手段优化散…...

【JavaSE】绘图与事件入门学习笔记

-Java绘图坐标体系 坐标体系-介绍 坐标原点位于左上角&#xff0c;以像素为单位。 在Java坐标系中,第一个是x坐标,表示当前位置为水平方向&#xff0c;距离坐标原点x个像素;第二个是y坐标&#xff0c;表示当前位置为垂直方向&#xff0c;距离坐标原点y个像素。 坐标体系-像素 …...

06 Deep learning神经网络编程基础 激活函数 --吴恩达

深度学习激活函数详解 一、核心作用 引入非线性:使神经网络可学习复杂模式控制输出范围:如Sigmoid将输出限制在(0,1)梯度传递:影响反向传播的稳定性二、常见类型及数学表达 Sigmoid σ ( x ) = 1 1 +...

Web 架构之 CDN 加速原理与落地实践

文章目录 一、思维导图二、正文内容&#xff08;一&#xff09;CDN 基础概念1. 定义2. 组成部分 &#xff08;二&#xff09;CDN 加速原理1. 请求路由2. 内容缓存3. 内容更新 &#xff08;三&#xff09;CDN 落地实践1. 选择 CDN 服务商2. 配置 CDN3. 集成到 Web 架构 &#xf…...

脑机新手指南(七):OpenBCI_GUI:从环境搭建到数据可视化(上)

一、OpenBCI_GUI 项目概述 &#xff08;一&#xff09;项目背景与目标 OpenBCI 是一个开源的脑电信号采集硬件平台&#xff0c;其配套的 OpenBCI_GUI 则是专为该硬件设计的图形化界面工具。对于研究人员、开发者和学生而言&#xff0c;首次接触 OpenBCI 设备时&#xff0c;往…...