реклама на сайте
подробности

 
 
> Прилипание к краям экрана Qt
nice_vladi
сообщение Nov 28 2017, 03:51
Сообщение #1


Участник
*

Группа: Участник
Сообщений: 53
Регистрация: 7-09-16
Из: Томск
Пользователь №: 93 239



Всем привет. Вопрос простой: как реализовать прилипание к краям экрана основного виджета (QMainWindow) в Qt Creator? Либо каким образом можно сделать прилипание? Может быть, главная форма не имеет такой возможности? Поиски в интернатах и книжках не привели к успеху(((

Qt Creator 4.0.2
Windows 7
Go to the top of the page
 
+Quote Post
 
Start new topic
Ответов
arhiv6
сообщение Nov 28 2017, 05:18
Сообщение #2


Знающий
****

Группа: Свой
Сообщений: 633
Регистрация: 21-05-10
Из: Томск
Пользователь №: 57 423



Правильнее отдать эту работу оконному менеджеру. Но если очень хочется, можно вот так попробовать:

mainwindow.h
CODE
#ifndef MAINWINDOW_H
#define MAINWINDOW_H

#include <QMainWindow>

namespace Ui
{
class MainWindow;
}

class MainWindow : public QMainWindow
{
Q_OBJECT

public:
explicit MainWindow(QWidget *parent = 0);
~MainWindow();

protected:
void moveEvent(QMoveEvent *event);

private:
Ui::MainWindow *ui;
};

#endif // MAINWINDOW_H

mainwindow.cpp
CODE
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QDesktopWidget>
#include <QDebug>
#include <QtMath>

MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
}

MainWindow::~MainWindow()
{
delete ui;
}

void MainWindow::moveEvent(QMoveEvent *event)
{
Q_UNUSED(event);

int windowTopBorder = pos().y();
int windowBottomBorder = windowTopBorder + size().height();
int windowLeftBorder = pos().x();
int windowRightBorder = windowLeftBorder + size().width();

qDebug() << windowLeftBorder << windowRightBorder << windowTopBorder << windowBottomBorder;

int screenTopBorder = 0;
int screenBottomBorder = QApplication::desktop()->height();
int screenLeftBorder = 0;
int screenRightBorder = QApplication::desktop()->width();

qDebug() << screenLeftBorder << screenRightBorder << screenTopBorder << screenBottomBorder;

const int magnetDistance = 150;

if (abs(windowLeftBorder - screenLeftBorder) < magnetDistance)
{
move(0, pos().y());
}

if (abs(windowRightBorder - screenRightBorder) < magnetDistance)
{
move(screenRightBorder - size().width(), pos().y());
}

if (abs(windowTopBorder - screenTopBorder) < magnetDistance)
{
move(pos().x(), 0);
}

if (abs(windowBottomBorder - screenBottomBorder) < magnetDistance)
{
move(pos().x(), screenBottomBorder - size().height());
}
}



--------------------
Go to the top of the page
 
+Quote Post



Reply to this topicStart new topic
1 чел. читают эту тему (гостей: 1, скрытых пользователей: 0)
Пользователей: 0

 


RSS Текстовая версия Сейчас: 29th July 2025 - 00:37
Рейтинг@Mail.ru


Страница сгенерированна за 0.0136 секунд с 7
ELECTRONIX ©2004-2016