#include "mysql.h"
#include <QSqlError>
#include <QSqlQuery>
#include <QScrollBar>
mysql::mysql(QWidget *parent)
: QMainWindow(parent)
{
ui.setupUi(this);
m_goodsmodel.setQuery("select * from tb_goods", QSqlDatabase::database("file", true));
m_goodsmodel.setHeaderData(0, Qt::Horizontal, QStringLiteral("id"));
m_goodsmodel.setHeaderData(1, Qt::Horizontal, QStringLiteral("名称"));
m_barcodemodel.setQuery("select * from tb_barcode", QSqlDatabase::database("file", true));
m_barcodemodel.setHeaderData(0, Qt::Horizontal, QStringLiteral("id"));
m_barcodemodel.setHeaderData(1, Qt::Horizontal, QStringLiteral("扫描码"));
m_barcodemodel.setHeaderData(2, Qt::Horizontal, QStringLiteral("rfid"));
ui.goodView->setModel(&m_goodsmodel);
ui.skuidView->setModel(&m_barcodemodel);
setAttibutes(ui.goodView);
setAttibutes(ui.skuidView);
}
mysql::~mysql()
{
}
void mysql::on_insertButton_clicked()
{
QSqlQuery setquery(QSqlDatabase::database("file", true));
setquery.exec("insert into tb_goods values('123456789', 'aaa')");
setquery.exec("insert into tb_goods values('23456789', 'aaeffdda')");
setquery.exec("insert into tb_barcode(barcode,rfid) values('adddaeee', '123456789')");
reflushModel();
}
//这里应该问题不是很大
void mysql::on_deleteButton_clicked()
{
QSqlQuery setquery(QSqlDatabase::database("file", true));
setquery.exec("delete from tb_goods where rfid='123456789'");
reflushModel();
}
void mysql::on_updateButton_clicked()
{
QSqlQuery setquery(QSqlDatabase::database("file", true));
setquery.exec("update tb_goods set name='redddd' where rfid='123456789'");
reflushModel();
}
void mysql::reflushModel()
{
m_goodsmodel.setQuery("select * from tb_goods", QSqlDatabase::database("file", true));
m_barcodemodel.setQuery("select * from tb_barcode", QSqlDatabase::database("file", true));
}
void mysql::setAttibutes(QTableView* pView, int nHeaderHeight, int nColumnHeight)
{
if ( pView == NULL )
{
QString strTemp = QStringLiteral("pView== NULL!设置表格属性失败!");
return;
}
pView->horizontalHeader()->setFixedHeight(nHeaderHeight); //设置表头的高度
pView->horizontalHeader()->setSectionsClickable(false); //设置表头不可点击(默认点击后进行排序)
pView->horizontalHeader()->setStretchLastSection(true); //最后一列占满剩余空白
pView->setSelectionBehavior(QAbstractItemView::SelectRows); //设置选择行为时每次选择一行
pView->setEditTriggers(QAbstractItemView::NoEditTriggers); //使表视图只读
pView->verticalHeader()->setDefaultSectionSize(nColumnHeight); //设置行高
pView->setAlternatingRowColors(true); //可以交替颜色显示
pView->setShowGrid(false); //设置不显示格子线
pView->setCornerButtonEnabled(false); //左上角的按钮不可用,此按钮功能,一点击,全选
pView->horizontalScrollBar()->setStyleSheet(
"QScrollBar:horizontal{height:10px;background:transparent;background-color:rgb(248, 248, 248);margin:0px,0px,0px,0px;padding-left:10px;padding-right:10px;}"
"QScrollBar::handle:horizontal{height:10px;background:lightgray;border-radius:5px;/*min-height:20;*/}"
"QScrollBar::handle:horizontal:hover{height:10px;background:gray;border-radius:5px;/*min-height:20;*/}"
"QScrollBar::add-line:horizontal{/*height:10px;width:10px;*/border-image:url(:/button/images/button/right.png);/*subcontrol-position:right;*/}"
"QScrollBar::sub-line:horizontal{/*height:10px;width:10px;*/border-image:url(:/button/images/button/left.png);/*subcontrol-position:left;*/}"
"QScrollBar::add-line:horizontal:hover{/*height:10px;width:10px;*/border-image:url(:/button/images/button/right_mouseDown.png);/*subcontrol-position:right;*/}"
"QScrollBar::sub-line:horizontal:hover{/*height:10px;width:10px;*/border-image:url(:/button/images/button/left_mouseDown.png);/*subcontrol-position:left;*/}"
"QScrollBar::add-page:horizontal,QScrollBar::sub-page:horizontal{background:transparent;border-radius:5px;}"
);
pView->verticalScrollBar()->setStyleSheet(
"QScrollBar:vertical{width:10px;background:transparent;background-color:rgb(248, 248, 248);margin:0px,0px,0px,0px;padding-top:10px;padding-bottom:10px;}"
"QScrollBar::handle:vertical{width:10px;background:lightgray ;border-radius:5px;min-height:20;}"
"QScrollBar::handle:vertical:hover{width:10px;background:gray;border-radius:5px;min-height:20;}"
"QScrollBar::add-line:vertical{height:10px;width:10px;border-image:url(:/button/images/button/down.png);subcontrol-position:bottom;}"
"QScrollBar::sub-line:vertical{height:10px;width:10px;border-image:url(:/button/images/button/up.png);subcontrol-position:top;}"
"QScrollBar::add-line:vertical:hover{height:10px;width:10px;border-image:url(:/button/images/button/down_mouseDown.png);subcontrol-position:bottom;}"
"QScrollBar::sub-line:vertical:hover{height:10px;width:10px;border-image:url(:/button/images/button/up_mouseDown.png);subcontrol-position:top;}"
"QScrollBar::add-page:vertical,QScrollBar::sub-page:vertical{background:transparent;border-radius:5px;}"
);
}