环境
Qt 5.4.2
功能
Qt QTableView插入控件,qss设计
源码
1.mainwindow.h
class MyComboBox;
class MyDoubleSpinBox;
class MainWindow : public QMainWindow
{
Q_OBJECT
public:
explicit MainWindow(QWidget *parent = 0);
~MainWindow();
private:
Ui::MainWindow *ui;
protected:
bool eventFilter(QObject *obj, QEvent *event);
private:
QStandardItemModel *tableModel;
QStringList sCmbArray;
bool isInitLoad;
private:
void InitTableView();
void LoadTableView();
void TableAddComboBox(int row, int col,QString text);
void TableAddDSpinBox(int row, int col,double value);
private slots:
void slot_currentTextChanged(int row,int col,QString text);
void slot_currentValueChanged(int row,int col,double value);
void slot_itemChanged(QStandardItem *item);
void on_tableView_clicked(const QModelIndex &index);
};
//自定义QComboBox
class MyComboBox : public QComboBox
{
Q_OBJECT
public:
explicit MyComboBox(){}
explicit MyComboBox(const QStringList &list){this->addItems(list);}
~MyComboBox(){}
public:
void SetTableID(int tableRow, int tableCol)
{
row = tableRow;
col = tableCol;
connect(this,SIGNAL(currentTextChanged(QString)),this,SLOT(slot_currentTextChanged(QString)));
}
signals:
void currentChanged(int row,int col,QString text);
private slots:
void slot_currentTextChanged(QString text){emit currentChanged(row,col,text);}
private:
int row;
int col;
};
//自定义QDoubleSpinBox
class MyDoubleSpinBox : public QDoubleSpinBox
{
Q_OBJECT
public:
explicit MyDoubleSpinBox(double value = 0)
{
this->setValue(value);
this->setDecimals(1);
this->setMinimum(0);
this->setMaximum(100);
}
~MyDoubleSpinBox(){}
public:
void SetTableID(int tableRow, int tableCol)
{
row = tableRow;
col = tableCol;
connect(this,SIGNAL(valueChanged(double)),this,SLOT(slot_valueChanged(double)));
}
signals:
void currentValueChanged(int row,int col,double value);
private slots:
void slot_valueChanged(double value){emit currentValueChanged(row,col,value);}
private:
int row;
int col;
};
2.mainwindow.cpp
#include "mainwindow.h"
#include "ui_mainwindow.h"
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
InitTableView();
LoadTableView();
}
bool MainWindow::eventFilter(QObject *obj, QEvent *event)
{
//屏蔽控件滚轮事件
if(obj->inherits("QAbstractSpinBox")||obj->inherits("QComboBox")) {
if(event->type() == QEvent::Wheel) return true;
}
return QWidget::eventFilter(obj, event);
}
void MainWindow::InitTableView()
{
ui->tableView->verticalHeader()->setVisible(true);
ui->tableView->horizontalHeader()->setVisible(true);
tableModel = new QStandardItemModel();
ui->tableView->setModel(tableModel);
tableModel->setHorizontalHeaderItem(0, new QStandardItem("学号") );
tableModel->setHorizontalHeaderItem(1, new QStandardItem("姓名"));
tableModel->setHorizontalHeaderItem(2, new QStandardItem("性别"));
tableModel->setHorizontalHeaderItem(3, new QStandardItem("成绩"));
tableModel->setHorizontalHeaderItem(4, new QStandardItem("备注"));
ui->tableView->horizontalHeader()->setMinimumHeight(35);
ui->tableView->horizontalHeader()->setSectionResizeMode(0, QHeaderView::Fixed);
ui->tableView->horizontalHeader()->setSectionResizeMode(1, QHeaderView::Fixed);
ui->tableView->horizontalHeader()->setSectionResizeMode(2, QHeaderView::Fixed);
ui->tableView->horizontalHeader()->setSectionResizeMode(3, QHeaderView::Fixed);
ui->tableView->horizontalHeader()->setSectionResizeMode(4, QHeaderView::Stretch);
ui->tableView->setColumnWidth(0, 100);
ui->tableView->setColumnWidth(1, 100);
ui->tableView->setColumnWidth(2, 80);
ui->tableView->setColumnWidth(3, 80);
connect(tableModel,SIGNAL(itemChanged(QStandardItem*)),this,SLOT(slot_itemChanged(QStandardItem*)));
}
void MainWindow::LoadTableView()
{
isInitLoad = true;
sCmbArray.clear();
sCmbArray << "男" << "女";
QString data = "";
for(int i = 0; i < 20; i++)
{
data = " " + QString::number(i) + " ";
tableModel->setVerticalHeaderItem(i, new QStandardItem(data) );
data = "2024" + QString::number(10000 + i);
tableModel->setItem(i, 0, new QStandardItem(data) );
data = "学生" + QString::number(i);
tableModel->setItem(i, 1, new QStandardItem(data) );
tableModel->setItem(i, 4, new QStandardItem("" ) );
if(i %2 == 0) TableAddComboBox(i,2,"男");
else TableAddComboBox(i,2,"女");
TableAddDSpinBox(i,3,60);
ui->tableView->setRowHeight(i,35);
}
on_tableView_clicked(tableModel->index(0,0));
isInitLoad = false;
}
void MainWindow::TableAddComboBox(int row, int col, QString text)
{
MyComboBox * myComboBox = new MyComboBox(sCmbArray);
myComboBox->SetTableID(row,col);
myComboBox->setCurrentText(text);
connect(myComboBox,SIGNAL(currentChanged(int,int,QString)),this,SLOT(slot_currentTextChanged(int,int,QString)));
myComboBox->setView(new QListView()); //QSS生效
myComboBox->installEventFilter(this); //取消鼠标滚轮事件
ui->tableView->setIndexWidget(tableModel->index(row,col),myComboBox);
}
void MainWindow::TableAddDSpinBox(int row, int col, double value)
{
MyDoubleSpinBox * mySpinBox = new MyDoubleSpinBox(value);
mySpinBox->SetTableID(row,col);
connect(mySpinBox,SIGNAL(currentValueChanged(int,int,double)),this,SLOT(slot_currentValueChanged(int,int,double)));
ui->tableView->setIndexWidget(tableModel->index(row,col),mySpinBox);
mySpinBox->installEventFilter(this); //取消鼠标滚轮事件
}
void MainWindow::slot_currentTextChanged(int row, int col, QString text)
{
if(isInitLoad) { return; }
QString showMsg = "修改: row:" + QString::number(row) + ",col:" + QString::number(col) + ",txt:" + text;
ui->plainTextEdit_Change->setPlainText(showMsg);
}
void MainWindow::slot_currentValueChanged(int row, int col, double value)
{
if(isInitLoad) { return; }
QString showMsg = "修改: row:" + QString::number(row) + ",col:" + QString::number(col) + ",txt:" + QString::number(value,'f',1);
ui->plainTextEdit_Change->setPlainText(showMsg);
}
void MainWindow::slot_itemChanged(QStandardItem *item)
{
if(isInitLoad) { return; }
QString showMsg = "修改: row:" + QString::number(item->row()) + ",col:" + QString::number(item->column()) + ",txt:" + tableModel->index(item->row(),item->column()).data().toString();
ui->plainTextEdit_Change->setPlainText(showMsg);
}
void MainWindow::on_tableView_clicked(const QModelIndex &index)
{
QString showMsg = "选中: " + tableModel->index(index.row(),0).data().toString()+ ",";
showMsg += tableModel->index(index.row(),1).data().toString()+ ",";
MyComboBox * myComboBox = (MyComboBox*)ui->tableView->indexWidget(tableModel->index(index.row(),2));
showMsg += myComboBox->currentText() + ",";
MyDoubleSpinBox * mySpinBox = (MyDoubleSpinBox*)ui->tableView->indexWidget(tableModel->index(index.row(),3));
showMsg += QString::number(mySpinBox->value(),'f',1) + "," + tableModel->index(index.row(),4).data().toString();
ui->plainTextEdit_Click->setPlainText(showMsg);
}
3.部分qss
QTableView
{
border-radius: 5px;
border:1px solid rgb(120, 120, 120);
gridline-color: rgb(120, 120, 120);
background:#d9d9d9;
}
QTableView::item::selected
{
background:#dccdc6;
color: rgb(0, 0, 0);
}
QHeaderView {
background:rgb(188, 188, 188);
font: 12pt "等线";
}
QHeaderView::section
{
background-color:transparent ;
border:1px solid rgb(120, 120, 120);
}
QHeaderView::section:hover
{
background:rgb(188, 188, 188);
border:1px solid rgb(120, 120, 120);
}
QTableCornerButton::section
{
background:rgb(188, 188, 188);
}
QTableView::item::focus
{
border:1px solid #3D7B8A;
}
QLineEdit{
selection-background-color:#3D7B8A;
selection-color: #F2F2F2;
}
QLineEdit:focus
{
border:2px solid #3D7B8A;
}
QComboBox{
font: 12pt "等线";
color: rgb(0, 0, 0);
border:1px solid rgb(120, 120, 120);
background:#cad5dc;
}
QComboBox:hover{
border:1px solid rgb(160, 160, 160);
}
QComboBox:focus{
border:1px solid rgb(200, 200, 200);
}
QComboBox:on{
border:1px solid rgb(180, 180, 180);
}
QComboBox::drop-down{
border:0px solid rgb(180, 180, 180);
}
QComboBox::down-arrow{
position: absolute;
right:3px;
width:30px;
height:20px;
image: url(:/arrow.png);
}
QComboBox QAbstractItemView
{
background:#d9d9d9;
}
QComboBox QAbstractItemView::item
{
height: 24px;
color: rgb(0, 0, 0);
}
QComboBox QAbstractItemView::item:selected
{
background:#cad5dc;
}
QComboBox:disabled{
color: rgb(150, 150, 150);
}
QDoubleSpinBox {
color: rgb(0, 0, 0);
font: 12pt "等线";
border-radius: 0px;
border:1px solid rgb(120, 120, 120);
background:#cad5dc;
selection-background-color:#3D7B8A;
selection-color: #F2F2F2;
}
QDoubleSpinBox::up-button {
subcontrol-origin:border;
subcontrol-position:top right;
border-image:url(:/sortUp.png);
border-width:0px;
}
QDoubleSpinBox::up-button:hover {
background:rgb(100,100,100);
}
QDoubleSpinBox::up-button:pressed {
background:rgb(80,80,80);
}
QDoubleSpinBox::down-button {
subcontrol-origin:border;
subcontrol-position:bottom right;
width:16px;
border-image:url(:/sortDown.png);
border-width:0px;
}
/* 向下按钮 */
QDoubleSpinBox::down-button:hover {
background:rgb(100,100,100);
}
/* 向下按钮 */
QDoubleSpinBox::down-button:pressed {
background:rgb(80,80,80);
}
/* 向下按钮里的小箭头 */
QDoubleSpinBox:disabled{
color: rgb(150, 150, 150);
}