记录QT解决SQLite数据库、tablewidget相关问题
@
目录
一、解决SQLite数据库问题
(一)数据库连接
XXX.pro
QT += sql
Main.cpp
#include <QSqlDatabase>
#include <QSqlError>
#include <QSqlQuery>//头文件
bool OpenDatabase()
{
//打开数据库
QSqlDatabase db;
if(QSqlDatabase::contains("qt_sql_default_connection"))
db = QSqlDatabase::database("qt_sql_default_connection");
else
db = QSqlDatabase::addDatabase("QSQLITE");//将某种类型的数据库加入到数据库连接列表中
db.setDatabaseName(".\\KKK\\KKK.db");//设置数据库名
db.open();
if(!db.open())
{
qDebug()<<"open the file fail";
QMessageBox::critical(0, QObject::tr("Database error"), db.lastError().text());
return false;
}
else
qDebug()<<"database open success!";
return true;
}
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
……
……
……
if(!OpenDatabase())
return 1;
w.show();
return a.exec();
}
相关解决方法可借鉴网址如下:
QT数据库连接的几个重要函数的使用及注意事项(原创)
(二)数据库操作
①数据库查询
QSqlQuery query;
QString strSplit = F.split(' ').at(0);
query.exec(QString("select * from zzz where ID like '%%0%'").arg(strSplit));
while(query.next())
{
……
……
ui->tableWidget->setRowCount(row);
for(int column = 0; column < query.record().count(); column++)
{
QTableWidgetItem *item = new QTableWidgetItem(query.value(column).toString());
ui->tableWidget->setItem(row,column,item);
……
……
}
}
②数据库更新
QString queryString;
QSqlQuery query;//执行sql语句
queryString = QString("UPDATE KKK SET 当前值 = '%1' WHERE ID = '%2-%3'; ").arg(Int12).arg(IDH).arg(IntNum);
if(query.exec(queryString))
{
qDebug()<<"update data Successful!";
}
else
{
qDebug()<<"update data Failed!";
}
③数据库查找
QSqlQuery query;
if(ui->FindEdit->text() == "")
query.exec(QString("select * from KKK"));
else
query.exec(QString("select * from KKK where ID like '%%0%' or 详情 like '%%0%'").arg(ui->FindEdit->text()));//仅查找ID或者详情
while(query.next())
{
for(int column = 0; column < query.record().count(); column++)
{
……
}
}
相关解决方法可借鉴网址如下:
QT实现把SQLite3数据库中表的数据加载到QTablewidget中
二、解决tablewidget相关问题
(一)表格基本设置
void Test::setTableWidgetData()
{
QString qssTV = "QTableWidget::item:hover{background-color:rgb(92,188,227,200)}"
"QTableWidget::item:selected{background-color:#1B89A1}"
"QHeaderView::section,QTableCornerButton:section{ \
padding:3px; margin:0px; color:#DCDCDC; border:1px solid #242424; \
border-left-width:0px; border-right-width:1px; border-top-width:0px; border-bottom-width:1px; \
background:qlineargradient(spread:pad,x1:0,y1:0,x2:0,y2:1,stop:0 #646464,stop:1 #525252); }"
"QTableWidget{background-color:#f5f0db;border:none;}";
ui->tableWidget->setShowGrid(true); //设置显示格子线
ui->tableWidget->setSelectionBehavior(QAbstractItemView::SelectRows); //整行选中的方式
ui->tableWidget->horizontalHeader()->setStretchLastSection(true); //行头自适应表格
ui->tableWidget->resizeColumnsToContents(); //自适应宽度
ui->tableWidget->horizontalHeader()->setFont(QFont("song", 10));
ui->tableWidget->horizontalHeader()->setHighlightSections(false); //点击表时不对表头行光亮(获取焦点)
QFont font = ui->tableWidget->horizontalHeader()->font();//设置表头字体加粗
font.setBold(true);
ui->tableWidget->horizontalHeader()->setFont(font);
ui->tableWidget->horizontalHeader()->setStyleSheet("QHeaderView::section{background:lightblue;}"); //skyblue设置表头背景色
ui->tableWidget->setStyleSheet("selection-background-color:lightblue;"); //设置选中背景色
ui->tableWidget->setStyleSheet(qssTV);
ui->tableWidget->horizontalHeader()->setHighlightSections(false); //点击表头时不对表头光亮
ui->tableWidget->setSelectionMode(QAbstractItemView::ContiguousSelection);//连续选择
ui->tableWidget->setSelectionBehavior(QAbstractItemView::SelectRows); //选中一行
ui->tableWidget->setFont(QFont("song", 9)); //所有单元格的字体 设置成一样
//去掉默认行号 可以用horizontalHeader() ->setVisible(false)隐藏横向表头
QHeaderView *header1 = ui->tableWidget->verticalHeader();
header1->setHidden(true);
ui->tableWidget->setAlternatingRowColors(true);//隔行变色
ui->tableWidget->setColumnHidden(3, true);//隐藏某列
}
……
ui->tableWidget->setColumnWidth(4, 200);//设置固定宽度
ui->tableWidget->setEditTriggers(QAbstractItemView::CurrentChanged); //设置全部可编辑
Test::setEditTriggersForColumn(ui->tableWidget,row,0); //再一列列禁编辑
……
void Test::setEditTriggersForColumn(QTableWidget *tableWidget, int rowCount, int column)
{
for(int i=0;i<=rowCount;i++)
{
QTableWidgetItem *item = tableWidget->item(i,column);
item->setFlags(item->flags() & ~Qt::ItemIsEditable);
}
}
相关解决方法可借鉴网址如下:
QT学习笔记9:QTableWidget的用法总结
(二)表格零七零八的东西
①插入复选框
QTableWidgetItem *check=new QTableWidgetItem(QString::number(row));
check->setCheckState(Qt::Unchecked); //是否设置选中框
ui->tableWidget->setItem(row,0,check); //插入复选框
②判断当前行复选框是否选中
if(ui->tableWidget->item(rowNum, 0)->checkState() == Qt::Checked)
③插入下拉框
QTableWidgetItem *show = ui->tableWidget->item(row,3);
TesttreeWidget_ComboxItem(show,row);
void Test:: TesttreeWidget _ComboxItem(QTableWidgetItem *show, int row)
{
QComboBox *comBox_ = new QComboBox();
QStringList show1 = show->text().split(';');
for(int i = 0;i < show1.size();i ++)
{
QString _str = show1.at(i);
comBox_->addItem(_str);
ui->tableWidget->setCellWidget(row, 4 ,comBox_);
}
}
④读取下拉框内数据并计算
QString msg;
QWidget *widget;
QComboBox *combox;
//读取数据
widget=ui->tableWidget->cellWidget(rowNum,4);
combox=(QComboBox*)widget;
msg=msg.append(combox->currentText());
//计算数据,例如数据形式为 0:xxx 1:yyy
int selectIndex = 0;
QString selectOption = "";
QString msgSplit;
int msgSplitInt[20] = {0}
widget=ui->tableWidget->cellWidget(i,1);
combox=(QComboBox*)widget;
selectIndex = combox->currentIndex();
selectOption = combox->itemText(selectIndex);
msgSplit = selectOption.split(':').at(0);
msgSplitInt[i] = msgSplit.toUInt();
if(rowNum0 != 16)//所需结果为0~100
{
for(int i = 0; i < rowNum0; i++)
{
msgSplitIntAll += msgSplitInt[i] * (pow( 10 , (rowNum0 -1 - i)));
ui->currentNum->setText(QString::number(msgSplitIntAll));
}
}
else if(rowNum0 == 16)//所需计算结果为0000~FFFF
{
unsigned int sum = 0, num;//初始化变量。num是每一位的数值,sum是每一位数值最后的加和。
num = 1;
for(int i = 0; i <rowNum0; i++)
{
if(msgSplitInt[i] == 1)
{
num = msgSplitInt[i] * (pow( 2 , (rowNum0 -1 - i)));
sum = sum + num;//sum就是最后的十进制的值。
ui->currentNum->setText(QString::number(sum));
}
}
}
相关解决方法可借鉴网址如下:
QTableWidget插入QcomboBox后,如何获取数据
⑤插入按键
QPushButton *btnMore = new QPushButton();
btnMore->setText(tr("……"));
ui->tableWidget->setCellWidget(row,5,btnMore);
相关解决方法可借鉴网址如下:
Qt中如何在QTableWidget中一个单元格插入多个按钮,如何正确获取插入的按钮的行列数
⑥合并单元格
ui->tableWidget->setSpan(row, 4, 1, 2); //以(row,4)为起点,合并1行2两列,即4、5列
⑦int类型数据插入表格中
int bytesCharInt[charIntNum] = 0;
QString bytesCharIntTable;
QTableWidgetItem *bytesCharIntTableItem;
bytesCharIntTable = QString::number(bytesCharInt[charIntNum]);
bytesCharIntTableItem = new QTableWidgetItem(bytesCharIntTable);
ui->tableWidget->setItem(rowNum, 6 ,bytesCharIntTableItem);
⑧表格内数据转为int类型化为16进制数据并固定数据长度
int textCell3Int = 0;
QTableWidgetItem *cell3Item;
cell3Item = ui->tableWidget->item(rowNum,3);
textCell3Int = cell3Item->text().toUInt();
//获取所需数据,例如1234获取12和23
QString textCell3IntHex;
textCell3IntHex = QString("%1").arg(textCell3Int,4,16,QLatin1Char('0'));//长度为4位
// QString hexstr = QString::number(hexnum,16);//转换成16进制存入字符串不固定长度
QString y = textCell3IntHex.mid(0, 2);
QString y1 = textCell3IntHex.mid(2, 2);
TextHSend = y.toInt(nullptr,16);
TextLSend = y1.toInt(nullptr,16);
⑨QString类型数据加入表格中
QTableWidgetItem *GwStr = new QTableWidgetItem(ner);
ui->tableWidget->setItem(row,4, GwStr);
⑩判断是否有选中一行
QList<QTableWidgetItem*> ifChooseLine = ui->tableWidget->selectedItems();
if(!ifChooseLine.empty())
{
……
}
else
QMessageBox::warning(this,"警告","请先选择一行再进行操作");