How to enable QDialog for screens like Skype?

Once upon a time, I tried to find a way to bind the QDialog window to the border screens for my small projects like Skype windows, but I failed. Maybe I was looking for this code in the wrong place, so now I'm looking for a solution here on the stack! :)

So, does anyone have a deal with some such code, links, samples?

In my opinion, we need to override the QDialog moveEvent function, as shown below, but this code does not work:

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);
}

Thank.

+3
source share
2 answers

As you thought, you can achieve this in the moveEvent function. I assume the following code does the trick, but since I have nothing to test, I will write some pseudocode:

:

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

,

+1

pos description QWidget .

: move() setGeometry() moveEvent() .

, .

: , KDE, . , Window Manager - , (, ), . KDE ( ) .

+1

Source: https://habr.com/ru/post/1742802/


All Articles