#include "GridCtrl.h"
GridCtrl::GridCtrl(QWidget *parent):QTableWidget(parent)
{
}
GridCtrl::~GridCtrl()
{
}
void GridCtrl::initTableWidget()
{
m_otableWidget=new QTableWidget (this);
m_otableWidget->setGeometry(0,0,600,600);
m_otableWidget->setColumnCount(1); //设置列数
m_otableWidget->setRowCount(100);//设置行数
m_otableWidget->horizontalHeader()->setDefaultSectionSize(150);
m_otableWidget->horizontalHeader()->setSectionsClickable(false); //设置表头不可点击(默认点击后进行排序)
//设置表头内容
QStringList header;
header<<tr("AD");
m_otableWidget->setHorizontalHeaderLabels(header);
//设置表头字体加粗
QFont font = m_otableWidget->horizontalHeader()->font();
font.setBold(true);
m_otableWidget->horizontalHeader()->setFont(font);
m_otableWidget->horizontalHeader()->setStretchLastSection(true); //设置充满表宽度
m_otableWidget->verticalHeader()->setSectionResizeMode(QHeaderView::ResizeToContents);
m_otableWidget->verticalHeader()->setDefaultSectionSize(20); //设置行高
//m_otableWidget->setFrameShape(QFrame::NoFrame); //设置无边框
m_otableWidget->setShowGrid(true); //设置显示格子线
m_otableWidget->verticalHeader()->setVisible(false); //设置垂直头不可见
m_otableWidget->setSelectionMode(QAbstractItemView::ExtendedSelection); //可多选(Ctrl、Shift、 Ctrl+A都能够)
m_otableWidget->setSelectionBehavior(QAbstractItemView::SelectRows); //设置选择行为时每次选择一行
m_otableWidget->setEditTriggers(QAbstractItemView::NoEditTriggers); //设置不可编辑
m_otableWidget->horizontalHeader()->resizeSection(0,150); //设置表头第一列的宽度为150
m_otableWidget->horizontalHeader()->setFixedHeight(25); //设置表头的高度
m_otableWidget->setStyleSheet("selection-background-color:lightblue;"); //设置选中背景色
m_otableWidget->horizontalHeader()->setStyleSheet("QHeaderView::section{background:skyblue;}"); //设置表头背景色
m_otableWidget->setStyleSheet("background-color:rgb(220,240,240);"); //设置背景色
//设置水平、垂直滚动栏样式
m_otableWidget->horizontalScrollBar()->setStyleSheet("QScrollBar{background:transparent; height:10px;}"
"QScrollBar::handle{background:lightgray; border:2px solid transparent; border-radius:5px;}"
"QScrollBar::handle:hover{background:gray;}"
"QScrollBar::sub-line{background:transparent;}"
"QScrollBar::add-line{background:transparent;}");
m_otableWidget->verticalScrollBar()->setStyleSheet("QScrollBar{background:transparent; width: 10px;}"
"QScrollBar::handle{background:lightgray; border:2px solid transparent; border-radius:5px;}"
"QScrollBar::handle:hover{background:gray;}"
"QScrollBar::sub-line{background:transparent;}"
"QScrollBar::add-line{background:transparent;}");
QTime time = QTime::currentTime();
qsrand((uint)time.msec()); // 使用当前时间作为随机数种子
// for (int row = 0; row < 10; ++row) {
// for (int col = 0; col < 4; ++col) {
// int randomNumber = qrand() % 100; // 生成 0 到 99 之间的随机数
// QTableWidgetItem *item = new QTableWidgetItem(QString::number(randomNumber)); // 创建一个新的表格项
// //设置字体样式为黑色
// QColor color("black");
// item->setTextColor(color);
// m_otableWidget->setItem(row, col, item); // 将表格项插入到表格中的指定位置
// }
// }
//去除虚线
m_otableWidget->setItemDelegate(new NoFocusDelegate);
//点击表时不正确表头行光亮(获取焦点)
m_otableWidget->horizontalHeader()->setHighlightSections(false);
//InitProgressBar();
//InitCombox();
//InitButton();
//InitChectBox();
setTableLineBackgroundColor(m_otableWidget, QColor(qRgb(200,200,200)));//设置线条颜色
//单击表头触发的事件
//connect(horizontalHeader(), SIGNAL(sectionClicked(int)), this, SLOT(onHeaderClicked(int)));
//双击选中表格获取行号
connect(m_otableWidget, SIGNAL(doubleClicked(QModelIndex)), this,SLOT(TableDoubleClicked_slots(QModelIndex)));//< 双击定位
//设置菜单
m_otableWidget->setContextMenuPolicy(Qt::CustomContextMenu);
connect(m_otableWidget, SIGNAL(customContextMenuRequested(const QPoint &)), this, SLOT(ShowContextMenu(const QPoint &)));
m_otableWidget->update();
}
void GridCtrl::InitChectBox()
{
m_oCheckBox=new QCheckBox(this);
m_oCheckBox->setObjectName(QString("CheckBox"));
m_oCheckBox->setText("CheckBox");
QWidget *wgb = new QWidget(this);
QHBoxLayout *verticalLayoutb = new QHBoxLayout(wgb);
verticalLayoutb->setContentsMargins(3,1,10,1);
verticalLayoutb->addWidget(m_oCheckBox);
m_otableWidget->setCellWidget(0,3,wgb);
}
void GridCtrl::InitProgressBar()
{
m_oProgressBar=new QProgressBar(this);
m_oProgressBar->setObjectName(QStringLiteral("Progress"));
m_oProgressBar->setMaximumHeight(20);
m_oProgressBar->setFormat(QStringLiteral("value:%1").arg(0));
m_oProgressBar->setValue(0);
m_oProgressBar->setMaximum(100);
m_oProgressBar->setMaximum(0);
m_oProgressBar->setAlignment(Qt::AlignCenter);
QFont font;
font.setPointSize(8);
m_oProgressBar->setFont(font);
m_oProgressBar->setStyleSheet(
"QProgressBar {border: 2px solid grey;"
"border-radius: 5px;"
"color:#ff6600;"
"background-color: #E9E9E9;"
"text-align: center;}"
"QProgressBar::chunk {background-color: rgb(0,250,0) ;}"
);
QWidget *wg = new QWidget(this);
QHBoxLayout *verticalLayouts = new QHBoxLayout(wg);
verticalLayouts->setObjectName(QString::fromUtf8("verticalLayouts"));
verticalLayouts->setContentsMargins(5,0,5,0);
verticalLayouts->setSpacing(0);
verticalLayouts->addWidget(m_oProgressBar);
m_otableWidget->setCellWidget(0,0,wg);
//for (int row = 0; row < table_widget->rowCount(); ++row)
//{
// table_widget->setCellWidget(row, 0, wg); // 将 Widget 添加到第0列的每一行
//}
}
void GridCtrl::InitCombox()
{
m_oComBox = new QComboBox(this);
m_oComBox->setObjectName(QString("combox"));
m_oComBox->setStyleSheet("QComboBox{border:1px solid #242424;"
"border-radius:3px;"
"padding:2px;"
"background:none;"
"border-color: rgb(255,255,255);"
"background-color: rgb(225,235, 250);"
"selection-background-color:#484848;"
"selection-color:#DCDCDC;}");
m_oComBox->addItem(QStringLiteral("A"));
m_oComBox->addItem(QStringLiteral("B"));
m_oComBox->addItem(QStringLiteral("C"));
m_oComBox->setCurrentText("A");
m_oComBox->setEditable(false); // 变为不可编辑状态
//m_oComBox->lineEdit()->setAlignment(Qt::AlignCenter); // 文字居中
//cbox->lineEdit()->setReadOnly(true); // 改为只读状态
connect(m_oComBox,SIGNAL(currentIndexChanged(const QString &)), this,SLOT(comboBoxCurrentIndexChanged(const QString &))); // 添加 下拉框 点击处理
QWidget *wg = new QWidget(this);
QHBoxLayout *verticalLayoutc = new QHBoxLayout(wg);
verticalLayoutc->setObjectName(QString::fromUtf8("verticalLayouts"));
verticalLayoutc->setContentsMargins(0,0,0,0);
verticalLayoutc->setSpacing(0);
verticalLayoutc->addWidget(m_oComBox);
m_otableWidget->setCellWidget(0,1,wg);
/*
for (int row = 0; row < table_widget->rowCount();