Qt实现一个窗口在另一个窗口上移动

一个窗口在另一个窗口上移动,可左移亦可右移,效果如下:
在这里插入图片描述
实现机制:通过定时器控制,上层窗口不停的在下层窗口上 move N个像素,窗口关系可以是父子关系。

核心代码`

m_MoveOut.setTimerType(Qt::PreciseTimer);
m_MoveOut.setInterval(8);
connect(&m_MoveOut, &QTimer::timeout, this, [this] {
	QPoint pos = QPoint(m_posX, m_posY);
	move(pos);
	if (m_Direction) { //右移
		m_posX -= 1; //移动速度
	}
	else { //左移
		m_posX += 1;
	}
	//处理边界问题,不让窗口移动出下一层窗口
	if (m_posX < 0)
	{
		if (m_posX < -1)
			move(QPoint(0, m_posY));
		m_posX = 0;
		m_MoveOut.stop();
	}
});

void TxtMoveTest::toStartMoveTimer(bool s, int x, int y)
{
	m_Direction = s;
	if (x != 0 && y != 0) {
		m_posX = x;
		m_posY = y;
	}
	m_MoveOut.start();
}

如果窗口涉及到排队处理,建议通过线程去启动定时器,防止出现不同步的问题(部分代码如下)

connect(&m_threadFloat, SIGNAL(started()), &m_MoveOut, SLOT(start()), Qt::UniqueConnection);
connect(&m_threadFloat, SIGNAL(finished()), &m_MoveOut, SLOT(stop()), Qt::UniqueConnection);
m_MoveOut.moveToThread(&m_threadFloat);
m_threadFloat.start();

测试代码

connect(ui.pushButton_test, &QPushButton::clicked, this, [=]() {
	if (s_first) {
		m_txt->toStartMoveTimer(s, 120, 140);
		s_first = !s_first;
	}
	else {
		m_txt->toStartMoveTimer(s);
	}
	s = !s;
	m_txt->show();
});
  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

离歌漠

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值