gpt4 book ai didi

qt - 如何像 Skype 一样将 QDialog 粘贴到屏幕边框?

转载 作者:行者123 更新时间:2023-12-04 23:48:29 24 4
gpt4 key购买 nike

很久以前,我试图为我的小项目(如 Skype windows)找到将 QDialog 窗口粘贴到屏幕边框的方法,但我失败了。可能是我在看这段代码的地方不对,所以现在我在堆栈上寻找解决方案! :)

那么,有人处理过某种此类代码、链接、示例吗?

在我看来,我们必须重新实现 QDialog moveEvent 函数,如下所示,但该代码不起作用:

void    CDialog::moveEvent(QMoveEvent * event) {

QRect wndRect;
int leftTaskbar = 0, rightTaskbar = 0, topTaskbar = 0, bottomTaskbar = 0;
// int top = 0, left = 0, right = 0, bottom = 0;

wndRect = this->frameGeometry();

// Screen resolution
int screenWidth = QApplication::desktop()->width();
int screenHeight = QApplication::desktop()->height();

int wndWidth = wndRect.right() - wndRect.left();
int wndHeight = wndRect.bottom() - wndRect.top();

int posX = event->pos().x();
int posY = event->pos().y();

// Snap to screen border
// Left border
if (posX >= -m_nXOffset + leftTaskbar &&
posX <= leftTaskbar + m_nXOffset) {
//left = leftTaskbar;
this->move(leftTaskbar, posY);
return;
}

// Top border
if (posY >= -m_nYOffset &&
posY <= topTaskbar + m_nYOffset) {
//top = topTaskbar;
this->move(posX, topTaskbar);
return;
}

// Right border
if (posX + wndWidth <= screenWidth - rightTaskbar + m_nXOffset &&
posX + wndWidth >= screenWidth - rightTaskbar - m_nXOffset) {
//right = screenWidth - rightTaskbar - wndWidth;
this->move(screenWidth - rightTaskbar - wndWidth, posY);
return;
}

// Bottom border
if (posY + wndHeight <= screenHeight - bottomTaskbar + m_nYOffset &&
posY + wndHeight >= screenHeight - bottomTaskbar - m_nYOffset) {
//bottom = screenHeight - bottomTaskbar - wndHeight;
this->move(posX, screenHeight - bottomTaskbar - wndHeight);
return;
}

QDialog::moveEvent(event);
}

谢谢。

最佳答案

如您所想,您可以在 moveEvent 函数中实现这一点。我想下面的代码可以解决问题,但由于我在这里没有要测试的东西,所以我将编写一些伪代码:

首先获取可用的屏幕区域:

const QRect screen = QApplication::availableGeometry(this);
// This get the screen rect where you can drag a dialog

然后获取对话框相对于桌面的位置(如果您的对话框是其他小部件的子项,则需要将坐标从小部件相对于桌面相对转换):

const QRect dialog = geometry();
// Do here transformation

现在测试对话框是否靠近屏幕边框

if( abs(dialog.left()-screen.left() < OFFSET )
move(screen.left(), dialog.top();
else if( abs(dialog.top()-screen.top() < OFFSET )
move(dialog.left(), screen.top() )
// etc. for the 2 other cases

如果有用请告诉我

关于qt - 如何像 Skype 一样将 QDialog 粘贴到屏幕边框?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2712911/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com