qt数据显示在不同视图
2015-05-23 09:25
176 查看
1.需求:
根据用户选择,将数据库中查询到的产品信息显示在三个视图中,第一个视图显示基本信息商品名称,品牌等,第二个视图显示相应商品的颜色,第三个视图显示相应商品颜色下的尺码,库存等信息
2.解决:
打算使用内存数据库+数据库查询model+tableview实现这个
3.代码:
数据库文件
//connection.h /**************************************************************************** ** ** Copyright (C) 2013 Digia Plc and/or its subsidiary(-ies). ** Contact: http://www.qt-project.org/legal ** ** This file is part of the examples of the Qt Toolkit. ** ** $QT_BEGIN_LICENSE:BSD$ ** You may use this file under the terms of the BSD license as follows: ** ** "Redistribution and use in source and binary forms, with or without ** modification, are permitted provided that the following conditions are ** met: ** * Redistributions of source code must retain the above copyright ** notice, this list of conditions and the following disclaimer. ** * Redistributions in binary form must reproduce the above copyright ** notice, this list of conditions and the following disclaimer in ** the documentation and/or other materials provided with the ** distribution. ** * Neither the name of Digia Plc and its Subsidiary(-ies) nor the names ** of its contributors may be used to endorse or promote products derived ** from this software without specific prior written permission. ** ** ** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS ** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT ** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR ** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT ** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, ** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT ** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, ** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY ** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT ** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE ** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE." ** ** $QT_END_LICENSE$ ** ****************************************************************************/ #ifndef CONNECTION_H #define CONNECTION_H #include <QMessageBox> #include <QSqlDatabase> #include <QSqlError> #include <QSqlQuery> #include <QtGlobal> /* This file defines a helper function to open a connection to an in-memory SQLITE database and to create a test table. If you want to use another database, simply modify the code below. All the examples in this directory use this function to connect to a database. */ //! [0] static bool createConnection() { QSqlDatabase db = QSqlDatabase::addDatabase("QSQLITE"); db.setDatabaseName(":memory:"); if (!db.open()) { QMessageBox::critical(0, qApp->tr("Cannot open database"), qApp->tr("Unable to establish a database connection.\n" "This example needs SQLite support. Please read " "the Qt SQL driver documentation for information how " "to build it.\n\n" "Click Cancel to exit."), QMessageBox::Cancel); return false; } QTextCodec *gbk = QTextCodec::codecForName("GB18030"); if (gbk) { QSqlQuery query; query.exec("create table goods (id varchar(33) primary key, " "name varchar(200), color varchar(20), " "size varchar(20), retail varchar(20), " "storage int, productid varchar(33), " "unitname varchar(20), no varchar(200))"); std::string strgb18030("insert into goods values('7A7BA4B902C147E6A37BAF325DB14D50', '七匹狼', '红色', 'L', '100.50', 100, '7A7BA4B902C147E6A37BAF325DB14D50', '件', '20151020')"); QString strUnicode = gbk->toUnicode(strgb18030.c_str()); Q_ASSERT(query.exec(strUnicode)); strgb18030 = "insert into goods values('7A7BA4B902C147E6A37BAF325DB14D51', '七匹狼', '红色', 'S', '100.50', 100, '7A7BA4B902C147E6A37BAF325DB14D50', '件', '20151021')"; strUnicode = gbk->toUnicode(strgb18030.c_str()); query.exec(strUnicode); strgb18030 = "insert into goods values('7A7BA4B902C147E6A37BAF325DB14D52', '八匹马', '绿色', 'L', '100.50', 100, '7A7BA4B902C147E6A37BAF325DB14D50', '件', '20151022')"; strUnicode = gbk->toUnicode(strgb18030.c_str()); query.exec(strUnicode); strgb18030 = "insert into goods values('7A7BA4B902C147E6A37BAF325DB14D53', '八匹马', '绿色', , 'S', '100.50', 100, '7A7BA4B902C147E6A37BAF325DB14D50', '件', '20151023')"; strUnicode = gbk->toUnicode(strgb18030.c_str()); query.exec(strUnicode); strgb18030 = "insert into goods values('7A7BA4B902C147E6A37BAF325DB14D54', '七匹狼', '蓝色', 'XL', '100.50', 100, '7A7BA4B902C147E6A37BAF325DB14D50', '件', '20151024')"; strUnicode = gbk->toUnicode(strgb18030.c_str()); query.exec(strUnicode); } return true; } //! [0] #endif
声明文件
//mysel.h #ifndef MYSQL_H #define MYSQL_H #include <QtWidgets/QMainWindow> #include "ui_mysql.h" #include <QtSql> class mysql : public QMainWindow { Q_OBJECT public: mysql(QWidget *parent = 0); ~mysql(); private slots: void goodSelect(QModelIndex current); void colorSelect(QModelIndex current); void leftview(); void rightview(); private: void initializeModel(); void changeview(bool bleft); private: Ui::mysqlClass ui; QSqlQueryModel goodmodel; QSqlQueryModel colormodel; QSqlQueryModel sizemodel; }; #endif // MYSQL_H
实现文件
//mysel.cpp #include "mysql.h" #include <QSqlDatabase> #include <QSqlError> #include <QSqlQuery> #include <QtGui/QKeySequence> #include <QtWidgets/QShortcut> #include <QMessageBox> mysql::mysql(QWidget *parent) : QMainWindow(parent) { ui.setupUi(this); initializeModel(); ui.goodView->setModel(&goodmodel); ui.colorView->setModel(&colormodel); ui.sizeView->setModel(&sizemodel); ui.goodView->horizontalHeader()->setSectionResizeMode(QHeaderView::Stretch); ui.goodView->setSelectionBehavior(QAbstractItemView::SelectRows); ui.goodView->setEditTriggers(QAbstractItemView::NoEditTriggers); ui.colorView->horizontalHeader()->setSectionResizeMode(QHeaderView::Stretch); ui.colorView->setSelectionBehavior(QAbstractItemView::SelectRows); ui.colorView->setEditTriggers(QAbstractItemView::NoEditTriggers); ui.sizeView->horizontalHeader()->setSectionResizeMode(QHeaderView::Stretch); ui.sizeView->setSelectionBehavior(QAbstractItemView::SelectRows); ui.sizeView->setEditTriggers(QAbstractItemView::NoEditTriggers); //当model的选中状态改变时需要搞定的东西 connect(ui.goodView->selectionModel(), SIGNAL(currentRowChanged(QModelIndex,QModelIndex)), this, SLOT(goodSelect(QModelIndex))); connect(ui.colorView->selectionModel(), SIGNAL(currentRowChanged(QModelIndex,QModelIndex)), this, SLOT(colorSelect(QModelIndex))); ui.goodView->installEventFilter(this); ui.colorView->installEventFilter(this); ui.sizeView->installEventFilter(this); QShortcut *pshort = new QShortcut(Qt::Key_Left, this); connect(pshort, SIGNAL(activated()), this, SLOT(leftview())); pshort = new QShortcut(Qt::Key_Right, this); connect(pshort, SIGNAL(activated()), this, SLOT(rightview())); //默认选中第一条 QModelIndex index = goodmodel.index(0, 0); if (index.isValid()) ui.goodView->setCurrentIndex(index); } mysql::~mysql() { } void mysql::initializeModel() { goodmodel.setQuery("select distinct name, no from goods group by name"); goodmodel.setHeaderData(0, Qt::Horizontal, QStringLiteral("品牌")); goodmodel.setHeaderData(1, Qt::Horizontal, QStringLiteral("编号")); colormodel.setQuery("select distinct color from goods group by color"); colormodel.setHeaderData(0, Qt::Horizontal, QStringLiteral("颜色")); sizemodel.setQuery("select distinct size, storage from goods group by size"); sizemodel.setHeaderData(0, Qt::Horizontal, QStringLiteral("尺码")); sizemodel.setHeaderData(1, Qt::Horizontal, QStringLiteral("库存")); } void mysql::goodSelect(QModelIndex current) { //这里要更新下颜色和尺码部分 //首先得到名称和编号 QString strName(""), strColor(""); QModelIndex nameindex = goodmodel.index(current.row(), 0); if (nameindex.isValid()) strName = goodmodel.data(nameindex, Qt::DisplayRole).toString(); if (!strName.isEmpty()) { //这里实现拼字符串 QString strSel(""); strSel = QString("select distinct color from goods where name = '%1' group by color").arg(strName); colormodel.setQuery(strSel); QModelIndex colorindex; colorindex = colormodel.index(0, 0); if (colorindex.isValid()) { ui.colorView->setCurrentIndex(colorindex); strColor = colormodel.data(colorindex, Qt::DisplayRole).toString(); QString strSel(""); strSel = QString("select distinct size, storage from goods where name = '%1' and color = '%2' group by size").arg(strName).arg(strColor); sizemodel.setQuery(strSel); QModelIndex index; index = sizemodel.index(0, 0); if (index.isValid()) ui.sizeView->setCurrentIndex(index); } } } void mysql::colorSelect(QModelIndex current) { //这个要修改尺寸部分 QString strName(""), strColor(""); QModelIndex goodindex = ui.goodView->currentIndex(); if (goodindex.isValid()) { QModelIndex nameindex = goodmodel.index(goodindex.row(), 0); if (nameindex.isValid()) strName = goodmodel.data(nameindex, Qt::DisplayRole).toString(); } if (current.isValid()) { QModelIndex colorindex = colormodel.index(current.row(), 0); if (colorindex.isValid()) strColor = colormodel.data(colorindex, Qt::DisplayRole).toString(); } if (!strName.isEmpty() && !strColor.isEmpty()) { //这里实现拼字符串 QString strSel(""); strSel = QString("select distinct size, storage from goods where name = '%1' and color = '%2' group by size").arg(strName).arg(strColor); sizemodel.setQuery(strSel); QModelIndex index; index = sizemodel.index(0, 0); if (index.isValid()) ui.sizeView->setCurrentIndex(index); } } //改变view显示 void mysql::changeview(bool bleft) { QWidget *allview[3] = {ui.goodView, ui.colorView, ui.sizeView}; QWidget *pfocus = focusWidget(); int ncurrent = 0; for (ncurrent = 0; ncurrent < 3; ++ncurrent) { if (pfocus == allview[ncurrent]) break; } if (ncurrent >= 0 && ncurrent < 3) { if (bleft) ncurrent -= 1; else ncurrent += 1; if (ncurrent < 0) allview[2]->setFocus(); else if (ncurrent >= 3) allview[0]->setFocus(); else allview[ncurrent]->setFocus(); } } void mysql::leftview() { changeview(true); } void mysql::rightview() { changeview(false); }
主文件
//main.cpp #include "mysql.h" #include <QtWidgets/QApplication> #include "connection.h" int main(int argc, char *argv[]) { QApplication a(argc, argv); if (!createConnection()) return 0; mysql w; w.show(); return a.exec(); }
备注:vs2010 + qt5.40 +win7上编译通过
3.参考
qt自带例子数据库部分的querymodel程序
相关文章推荐
- qt视图显示不同数据
- Qt 之 使用 Model-View(模型视图框架)动态的加载显示数据
- ireport中 根据表中栏位获得的值来判断显示不同数据。
- 解决qt在相同分辨率不同宽度的屏幕下字体显示位置不一样的问题
- Qt中用QLabel显示OpenCV中Mat图像数据出现扭曲现象的解决
- 代理---视图间数据的传递:标签显示输入的内容【多个视图中】
- ffmpeg开发:视频数据在qt显示
- Openerp设置树形视图(tree view)的数据显示个数
- c#运用TreeView控件的树形视图显示数据库中数据
- android开发-Listview中显示不同的视图布局
- Android Listview中显示不同的视图布局
- datalist 的用法。也是增删改查,但是比较智能。用数据绑定的方式,可以有不同的显示方法,下面是对一个表的增删改查的参考代码
- View与Model绑定注意事项 (视图无数据显示)
- Asp.Net MVC 视图(显示数据库数据表格) - Part.3
- echart柱形图--一条柱子显示三个数据,每条柱子显示不同颜色
- qt折线图显示数据
- MFC建立一个文档视图的工程,然后在窗口上显示数据
- Listview中显示不同的视图布局
- 通过物化视图方式同步两服务器中数据表的两种不同方式的比较
- 客户端出错:无法在数据表视图中显示该列表: 没有安装与 Windows SharePoint Services 兼容的数据表组件。