一、介绍
该录像排程,是采用继承于QTableWidget的ScheduleTableWidget类进行实现。在ScheduleTableWidget实现类中,去除了Table原本的横向表头和纵向表头,分别采用第一行和第一列构成新的表头。新的横向表头代表一天24小时,新的纵向表头代表一周。新的横向表头是用setSpan合并了两个单元格,所以在它下面的每个单元格代表半个小时。横向表头和纵向表头都引入自定义委托,去实现字体、颜色等设置。多个选中区域可以通过按住Ctrl+鼠标左键绘制实现。在QtCreator下,录像排程运行的效果如下图所示:
二、相关接口
录像排程的接口是通过带有0与1字符串,共7*48=336个字符来设置单元格是否为选中状态来实现。
2.1 全选操作
void MainWindow::on_pushButton_allSelect_clicked()
{
QString strItem = QString("111111111111111111111111111111111111111111111111"
"111111111111111111111111111111111111111111111111"
"111111111111111111111111111111111111111111111111"
"111111111111111111111111111111111111111111111111"
"111111111111111111111111111111111111111111111111"
"111111111111111111111111111111111111111111111111"
"111111111111111111111111111111111111111111111111");
ui->tableWidget->setItemSelectByString(strItem);
}
2.2 清除所有
void MainWindow::on_pushButton_allClear_clicked()
{
QString strItem = QString("000000000000000000000000000000000000000000000000"
"000000000000000000000000000000000000000000000000"
"000000000000000000000000000000000000000000000000"
"000000000000000000000000000000000000000000000000"
"000000000000000000000000000000000000000000000000"
"000000000000000000000000000000000000000000000000"
"000000000000000000000000000000000000000000000000");
ui->tableWidget->setItemSelectByString(strItem);
}
2.3 通过特定字符串设置
void MainWindow::on_pushButton_Set_clicked()
{
QString strItem = QString("111111000000000000000000000000000000000000000000"
"111111000000000000000000000000000000000000000000"
"111111000000000000000000000000000000000000000000"
"111111000000000000000000000000000000000000000000"
"111111000000000000000000000000000000000000000000"
"111111000000000000000000000000000000000000000000"
"111111000000000000000000000000000000000000000000");
ui->tableWidget->setItemSelectByString(strItem);
}
2.4 获取排程设置字符串
void MainWindow::on_pushButton_get_clicked()
{
QString strItem;
ui->tableWidget->getItemSelectByString(strItem);
MY_DEBUG << "strItem:" << strItem;
QMessageBox::information(this, "mySchedule", strItem);
}
三、核心代码
scheduletablewidget.h
#ifndef SCHEDULETABLEWIDGET_H
#define SCHEDULETABLEWIDGET_H
#include <QObject>
#include <QTableWidget>
#include <QVector>
#include <QDebug>
#include <QMessageBox>
#define ROW_HEIGHT 20
#define COLUMN_WIDTH 20
#define MAX_ITEM_COUNT 336
#define ROW_COUNT 8
#define COLUMN_COUNT 49
#define MY_DEBUG qDebug() << "[" << __FILE__ << ":" << __FUNCTION__ << ":" << __LINE__ << "] "
typedef struct SCHEDULE_POSITION
{
int nRow;
int nColumn;
}SCHEDULE_POSITION_T;
class ScheduleTableWidget : public QTableWidget
{
public:
ScheduleTableWidget(QWidget *parent = Q_NULLPTR);
~ScheduleTableWidget(){}
void initSchedule();
void getItemSelectByString(QString& sItemValue);
void setItemSelectByString(QString sItemValue);
private:
void getItemSelectByPos(QVector<SCHEDULE_POSITION_T>& vecSelectedItems);
void setItemSelectByPos(QVector<SCHEDULE_POSITION_T> vecSelectedItems);
public:
QVector<SCHEDULE_POSITION_T> m_vecSelectedItems;
};
#endif // SCHEDULETABLEWIDGET_H
scheduletablewidget.cpp
void ScheduleTableWidget::initSchedule()
{
//表格设置
//....
//用首列和首行来作为新表头
this->setSpan(0,1,1,2);
this->setSpan(0,3,1,2);
this->setSpan(0,5,1,2);
this->setSpan(0,7,1,2);
this->setSpan(0,9,1,2);
this->setSpan(0,11,1,2);
this->setSpan(0,13,1,2);
this->setSpan(0,15,1,2);
this->setSpan(0,17,1,2);
this->setSpan(0,19,1,2);
this->setSpan(0,21,1,2);
this->setSpan(0,23,1,2);
this->setSpan(0,25,1,2);
this->setSpan(0,27,1,2);
this->setSpan(0,29,1,2);
this->setSpan(0,31,1,2);
this->setSpan(0,33,1,2);
this->setSpan(0,35,1,2);
this->setSpan(0,37,1,2);
this->setSpan(0,39,1,2);
this->setSpan(0,41,1,2);
this->setSpan(0,43,1,2);
this->setSpan(0,45,1,2);
this->setSpan(0,47,1,2);
this->setItem(0, 0, new QTableWidgetItem("(Week/Hour)"));
this->setItem(0, 1, new QTableWidgetItem("0"));
this->setItem(0, 3, new QTableWidgetItem("1"));
this->setItem(0, 5, new QTableWidgetItem("2"));
this->setItem(0, 7, new QTableWidgetItem("3"));
this->setItem(0, 9, new QTableWidgetItem("4"));
this->setItem(0, 11, new QTableWidgetItem("5"));
this->setItem(0, 13, new QTableWidgetItem("6"));
this->setItem(0, 15, new QTableWidgetItem("7"));
this->setItem(0, 17, new QTableWidgetItem("8"));
this->setItem(0, 19, new QTableWidgetItem("9"));
this->setItem(0, 21, new QTableWidgetItem("10"));
this->setItem(0, 23, new QTableWidgetItem("11"));
this->setItem(0, 25, new QTableWidgetItem("12"));
this->setItem(0, 27, new QTableWidgetItem("13"));
this->setItem(0, 29, new QTableWidgetItem("14"));
this->setItem(0, 31, new QTableWidgetItem("15"));
this->setItem(0, 33, new QTableWidgetItem("16"));
this->setItem(0, 35, new QTableWidgetItem("17"));
this->setItem(0, 37, new QTableWidgetItem("18"));
this->setItem(0, 39, new QTableWidgetItem("19"));
this->setItem(0, 41, new QTableWidgetItem("20"));
this->setItem(0, 43, new QTableWidgetItem("21"));
this->setItem(0, 45, new QTableWidgetItem("22"));
this->setItem(0, 47, new QTableWidgetItem("23"));
this->setItem(1, 0, new QTableWidgetItem("Monday"));
this->setItem(2, 0, new QTableWidgetItem("Tuesday"));
this->setItem(3, 0, new QTableWidgetItem("Wednesday"));
this->setItem(4, 0, new QTableWidgetItem("Thursday"));
this->setItem(5, 0, new QTableWidgetItem("Friday"));
this->setItem(6, 0, new QTableWidgetItem("Saturday"));
this->setItem(7, 0, new QTableWidgetItem("Sunday"));
}
void ScheduleTableWidget::getItemSelectByString(QString &sItemValue)
{
QVector<SCHEDULE_POSITION_T> vecSelectedItems;
getItemSelectByPos(vecSelectedItems);
QByteArray arr;
for(int i = 0; i < MAX_ITEM_COUNT; i++)
{
arr[i] = '0';
}
int nArrIndex = 0;
for(int i = 0; i < vecSelectedItems.size(); i++)
{
nArrIndex = (vecSelectedItems[i].nRow-1)*(COLUMN_COUNT-1) + (vecSelectedItems[i].nColumn-1);
arr[nArrIndex] = '1';
}
sItemValue = arr;
}
void ScheduleTableWidget::setItemSelectByString(QString sItemValue)
{
MY_DEBUG << "sItemValue.count():" << sItemValue.count();
if(sItemValue.count() < MAX_ITEM_COUNT)
{
QMessageBox::critical(this, "mySchedule", tr("error:Bytes less than 336"));
return;
}
QByteArray arr = sItemValue.toLatin1();
QVector<SCHEDULE_POSITION_T> vecSelectedItems;
for(int i = 0; i < MAX_ITEM_COUNT; i++)
{
if(arr[i] == '1')
{
SCHEDULE_POSITION_T stPos;
stPos.nRow = i/(COLUMN_COUNT-1) + 1;
stPos.nColumn = i%(COLUMN_COUNT-1) + 1;
MY_DEBUG << "setItemSelectByString i" << i << " nRow:" << stPos.nRow << ", nColumn:" << stPos.nColumn;
vecSelectedItems.push_back(stPos);
}
}
setItemSelectByPos(vecSelectedItems);
}
四、demo源码下载
https://download.csdn.net/download/linyibin_123/88059521