qt数据显示在不同视图
来源:互联网 发布:ar制作软件 编辑:程序博客网 时间:2024/04/30 02:30
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_OBJECTpublic: 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程序
0 0
- qt数据显示在不同视图
- qt视图显示不同数据
- 如何在视图上显示不同效果的字体
- Qt 在同一个QLabel中实现不同样式的显示
- Qt 在同一个QLabel中实现不同样式的显示
- 如何在DataGrid中显示来自不同DataTable的数据
- 如何pdf文档显示在不同的福昕视图中
- OSG在同一视图中显示来自不同来源的多个模型节点文件
- Qt 之 使用 Model-View(模型视图框架)动态的加载显示数据
- JPanel作为容器,显示不同的视图
- Listview中显示不同的视图布局
- Listview中显示不同的视图布局
- Listview中显示不同的视图布局
- Listview中显示不同的视图布局
- Listview中显示不同的视图布局
- Listview中显示不同的视图布局
- Listview中显示不同的视图布局
- Listview中显示不同的视图布局
- 熟悉各种开发需求对应的框架 比如那个任务调度框架 利用框架快速开发 重点是webservice
- 用户如何准备网站建设资料?
- 新版github中如何删除repository(项目源码库)
- 类的对象不可做自身的数据成员(data member)
- Xcode6新创建项目配置pch文件
- qt数据显示在不同视图
- 开始刷leetcode day20:Generate Parentheses
- 类图及类图中的关系
- 欢迎使用CSDN-markdown编辑器
- css 行高——line-height
- [Unity基础]一些算法
- 欢迎使用CSDN-markdown编辑器
- c语言之题2
- js实现双击改变文本内容