本文展示的功能是类似与Photoshop的通道选择,具体展示如下:
具体功能有:
1.第一列显示眼睛图标,第二列显示各通道的缩略图,第三列显示文字;
2.点击眼睛图标切换当前行的选中状态,点击后两列显示当前图像
3.RGB选中时R、G、B通道均选中
4.R、G、B通道均选中时选中RGB
...
QLabel m_eyeLabels[4]; //表格的第一列图标,眼睛
QLabel m_imgLabels[4]; //表格的第二列图标,各通道缩略图
QStandardItemModel *m_channelsModel; //分通道的数据模型
m_channelsModel = new QStandardItemModel(4, 3, this);
ui->tableView_channels->setModel(m_channelsModel); //设置数据模型
ui->tableView_channels->setEditTriggers(QTableView::NoEditTriggers);//数据不可编辑
ui->tableView_channels->setSelectionBehavior(QAbstractItemView::SelectRows);//选择行
ui->tableView_channels->setSelectionMode(QAbstractItemView::NoSelection);//不可选
ui->tableView_channels->verticalHeader()->hide(); //隐藏表头
ui->tableView_channels->horizontalHeader()->hide();
ui->tableView_channels->setFocusPolicy(Qt::NoFocus); //无焦点,不会显示虚线框
ui->tableView_channels->setShowGrid(false); //隐藏边框
ui->tableView_channels->setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff); //隐藏滚动条
ui->tableView_channels->setStyleSheet("selection-background-color:rgb(100,200,200)"); //选中项的背景色
ui->tableView_channels->setRowHeight(0, 30); //行高列宽
ui->tableView_channels->setColumnWidth(0, 30);
ui->tableView_channels->setColumnWidth(1, 30);
//第一列图标(眼睛)
for (int i = 0; i < 4; i++)
{
m_eyeLabels[i].setMaximumSize(30, 30);
m_eyeLabels[i].setAlignment(Qt::AlignVCenter | Qt::AlignHCenter);
ui->tableView_channels->setIndexWidget(m_channelsModel->index(i, 0), &m_eyeLabels[i]);
setShowChannels(i, true);
}
//第二列图标(各通道缩略图)
for (int i = 0; i < 4; i++)
{
m_imgLabels[i].setMaximumSize(30, 30);
m_imgLabels[i].setAlignment(Qt::AlignVCenter | Qt::AlignHCenter);
m_imgLabels[i].setPixmap(pixmap); //pixmap为对应的单通道图像
ui->tableView_channels->setIndexWidget(m_channelsModel->index(i, 1), &m_imgLabels[i]);
}
//第三列文字
m_channelsModel->setData(m_channelsModel->index(0, 2), "RGB");
m_channelsModel->setData(m_channelsModel->index(1, 2), "R");
m_channelsModel->setData(m_channelsModel->index(2, 2), "G");
m_channelsModel->setData(m_channelsModel->index(3, 2), "B");
...
//选中表格的某一项,用于切换通道
void PhotoShop::on_tableView_channels_clicked(QModelIndex index)
{
//获取选中的行列
int row = index.row();
int col = index.column();
//当前是否正在显示各通道图像
bool isShowRGB[4];
isShowRGB[0] = m_channelsModel->data(m_channelsModel->index(0, 0, QModelIndex()), Qt::UserRole).toBool();
isShowRGB[1] = m_channelsModel->data(m_channelsModel->index(1, 0, QModelIndex()), Qt::UserRole).toBool();
isShowRGB[2] = m_channelsModel->data(m_channelsModel->index(2, 0, QModelIndex()), Qt::UserRole).toBool();
isShowRGB[3] = m_channelsModel->data(m_channelsModel->index(3, 0, QModelIndex()), Qt::UserRole).toBool();
switch (row)
{
case 0:
if (0 == col) //RGB图的可视状态切换,打开-选中所有,显示所有,关闭-取消所有选中,取消显示
{
if (isShowRGB[0]) //关闭
{
setShowChannels(0, false);
setShowChannels(1, false);
setShowChannels(2, false);
setShowChannels(3, false);
}
else //打开RGB
{
setShowChannels(0, true);
setShowChannels(1, true);
setShowChannels(2, true);
setShowChannels(3, true);
}
}
else //选中图标/文字--选中所有,显示所有
{
setShowChannels(0, true);
setShowChannels(1, true);
setShowChannels(2, true);
setShowChannels(3, true);
}
break;
case 1:case 2:case 3:
{
int channelReset1 = (row + 1) > 3 ? (row + 1) % 3 : (row + 1);
int channelReset2 = (row + 2) > 3 ? (row + 2) % 3 : (row + 2);
if (0 == col)
{
setShowChannels(0, ((!isShowRGB[row]) && (isShowRGB[channelReset1] && isShowRGB[channelReset2])));
setShowChannels(row, !isShowRGB[row]);
}
else
{
setShowChannels(row, true);
setShowChannels(0, false);
setShowChannels(channelReset1, false);
setShowChannels(channelReset2, false);
}
break;
}
default:
break;
}
}
//设置某个通道是否显示,0--RGB,1--R,2--G,3--B
void PhotoShop::setShowChannels(int channel, bool isShow)
{
QPixmap pixmap;
if (isShow)
{
pixmap = QPixmap(":/PhotoShop/Resources/eye.png");
}
else
{
pixmap = QPixmap(":/PhotoShop/Resources/closeEye.png");
}
m_channelsModel->setData(m_channelsModel->index(channel, 0), isShow, Qt::UserRole);
pixmap = pixmap.scaled(15, 15, Qt::KeepAspectRatio, Qt::SmoothTransformation);
m_eyeLabels[channel].setPixmap(pixmap);
ui->tableView_channels->clearSelection();
if (0 == channel&&isShow)
{
ui->tableView_channels->selectAll();
}
ui->tableView_channels->setSelectionMode(QAbstractItemView::MultiSelection); //多选
for (int i = 0; i < 4; i++)
{
if (m_channelsModel->data(m_channelsModel->index(i, 0, QModelIndex()), Qt::UserRole).toBool())
{
ui->tableView_channels->selectRow(i);
}
}
ui->tableView_channels->setSelectionMode(QAbstractItemView::ContiguousSelection); //多选
}