Qt控件拖拽分割线
功能
- 控件拖拽排序
- 水平,垂直拖拽位置分割线
- 图片透明度设置
效果图
部分代码
//设置图片透明度
void setImageTransparency(QImage &image, int alpha)
{
const int height = image.height();
for (int l = 0; l < height; l++) {
QRgb *line = reinterpret_cast<QRgb *>(image.scanLine(l));
QRgb *lineEnd = line + image.width();
for (; line < lineEnd; line++) {
const QRgb rgba = *line;
*line = qRgba(qRed(rgba), qGreen(rgba), qBlue(rgba), alpha);
}
}
}
DragBaseBoxLayout::DragBaseBoxLayout(QWidget *parent)
: QWidget(parent)
, m_isDragout(false)
{
setAcceptDrops(true);
this->installEventFilter(this);
}
DragBaseBoxLayout::~DragBaseBoxLayout()
{
}
void DragBaseBoxLayout::addWidget(QWidget* widget)
{
this->layout()->addWidget(widget);
}
void DragBaseBoxLayout::hideIndicator(Indicator i)
{
if (m_indicators[i])
m_indicators[i]->hide();
}
void DragBaseBoxLayout::showIndicator(Indicator i, const QRect &geometry, const QPalette &p)
{
if (!m_indicators[i])
m_indicators[i] = new InvisibleWidget(this);
QWidget *indicator = m_indicators[i];
indicator->setAutoFillBackground(true);
indicator->setPalette(p);
indicator->setGeometry(geometry);
indicator->show();
indicator->raise();
}
int DragBaseBoxLayout::findItemAt(const QPoint &pos) const
{
if (!layout())
return -1;
const QLayout *lt = layout();
const int count = lt->count();
if (count == 0)
return -1;
int best = -1;
int bestIndex = -1;
for (int index = 0; index < count; index++) {
QLayoutItem *item = lt->itemAt(index);
bool visible = true;
if (const QWidget *w = item->widget())
visible = w->isVisible();
if (visible) {
const QRect g = item->geometry();
const int dist = (g.center() - pos).manhattanLength();
if (best == -1 || dist < best) {
best = dist;
bestIndex = index;
}
}
}
return bestIndex;
}
bool DragBaseBoxLayout::eventFilter(QObject *watched, QEvent *event)
{
if (watched == this){
if (event->type() == QEvent::ChildRemoved && m_isDragout){
QBoxLayout* layout = dynamic_cast<QBoxLayout*>(this->layout());
layout->insertWidget(m_srcIndex, m_widget);
m_widget->show();
}
}
return QWidget::eventFilter(watched, event);
}
工程文件
Qt交流大会 853086607 免费群中
结尾
不定期上传新作品,解答群中作品相关问题。相关外,能解答则解答。欢迎大家一起探索Qt世界!