I want to present a widget which can be moved and at which it is possible to change the sizes. This widget can serve as the container for other widgets, such as to QLabel, QTableWidget and so on. This widget is used by me already in several projects. I hope it we will to you it is useful
mainwindow.h
- /*
- Programmer Aleksey Osipov
- email: aliks-os@yandex.ru
- */
- #ifndef MAINWINDOW_H
- #define MAINWINDOW_H
- #include <QMainWindow>
- #include "tcontainer.h"
- namespace Ui {
- class MainWindow;
- }
- {
- Q_OBJECT
- public:
- ~MainWindow();
- private:
- Ui::MainWindow *ui;
- };
- #endif // MAINWINDOW_H
mainwindow.cpp
- /*
- Programmer Aleksey Osipov
- email: aliks-os@yandex.ru
- */
- #include "mainwindow.h"
- #include "ui_mainwindow.h"
- ui->setupUi(this);
- this->showMaximized();
- }
- MainWindow::~MainWindow() {
- delete ui;
- }
tcontainer.h
- /*
- Programmer Aleksey Osipov
- email: aliks-os@yandex.ru
- */
- #ifndef TCONTAINER_H
- #define TCONTAINER_H
- #include <QWidget>
- #include <QMouseEvent>
- #include <QtGui>
- enum modes{
- NONE = 0,
- MOVE = 1,
- RESIZETL = 2,
- RESIZET = 3,
- RESIZETR = 4,
- RESIZER = 5,
- RESIZEBR = 6,
- RESIZEB = 7,
- RESIZEBL = 8,
- RESIZEL = 9
- };
- Q_OBJECT
- public:
- ~TContainer();
- protected:
- int mode;
- QPoint position;
- bool m_infocus;
- bool m_showMenu;
- bool m_isEditing;
- private:
- signals:
- void inFocus(bool mode);
- void outFocus(bool mode);
- public slots:
- };
- #endif // TCONTAINER_H
tcontainer.cpp
- /*
- Programmer Aleksey Osipov
- email: aliks-os@yandex.ru
- */
- #include "tcontainer.h"
- mode = NONE;
- childWidget = cWidget;
- this->setVisible(true);
- this->setAutoFillBackground(false);
- this->setMouseTracking(true);
- this->setFocus();
- this->move(p);
- if (cWidget != 0) {
- cWidget->setParent(this);
- cWidget->releaseMouse();
- vLayout->addWidget(cWidget);
- vLayout->setContentsMargins(0,0,0,0);
- }
- this->setLayout(vLayout);
- m_infocus = true;
- m_showMenu = false;
- m_isEditing = true;
- this->installEventFilter(parent);
- }
- TContainer::~TContainer() {
- delete vLayout;
- }
- if (cWidget != 0) {
- childWidget = cWidget;
- childWidget->setParent(this);
- vLayout->addWidget(cWidget);
- vLayout->setContentsMargins(0,0,0,0);
- }
- }
- if (menu->isEmpty()) return;
- m_showMenu = true;
- menu->exec(global);
- m_showMenu = false;
- }
- m_infocus = true;
- this->parentWidget()->installEventFilter(this);
- this->parentWidget()->repaint();
- emit inFocus(true);
- }
- if (!m_isEditing) return;
- if (m_showMenu) return;
- mode = NONE;
- emit outFocus(false);
- m_infocus = false;
- }
- //return QWidget::eventFilter(obj, evt);
- if (m_infocus) {
- //Рисуем выделение контейнара
- }
- }
- if (!m_isEditing) return;
- if (!m_infocus) return;
- //QWidget::mouseMoveEvent(e);
- setCursorShape(e->pos());
- return;
- }
- popupShow(e->pos());
- e->accept();
- }
- }
- if (!m_isEditing) return;
- this->deleteLater();
- }
- //Двигаем контайнер при помощи клавиш
- move(newPos);
- }
- }
- emit newGeometry(this->geometry());
- }
- const int diff = 3;
- if (
- //Left-Bottom
- ((e_pos.y() > y() + height() - diff) && //Bottom
- (e_pos.x() < x()+diff)) || //Left
- //Right-Bottom
- ((e_pos.y() > y() + height() - diff) && //Bottom
- (e_pos.x() > x() + width() - diff)) || //Right
- //Left-Top
- ((e_pos.y() < y() + diff) && //Top
- (e_pos.x() < x() + diff)) || //Left
- //Right-Top
- ((e_pos.y() < y() + diff) && //Top
- (e_pos.x() > x() + width() - diff)) //Right
- )
- {
- //Left-Bottom
- if ((e_pos.y() > y() + height() - diff) && //Bottom
- (e_pos.x() < x() + diff)) { //Left
- mode = RESIZEBL;
- }
- //Right-Bottom
- if ((e_pos.y() > y() + height() - diff) && //Bottom
- (e_pos.x() > x() + width() - diff)) { //Right
- mode = RESIZEBR;
- }
- //Left-Top
- if ((e_pos.y() < y() + diff) && //Top
- (e_pos.x() < x() + diff)) { //Left
- mode = RESIZETL;
- }
- //Right-Top
- if ((e_pos.y() < y() + diff) && //Top
- (e_pos.x() > x() + width() - diff)) { //Right
- mode = RESIZETR;
- }
- }
- // проверка положения курсора по горизонтали
- else if ((e_pos.x() < x() + diff) || //Left
- ((e_pos.x() > x() + width() - diff))) { //Right
- if (e_pos.x() < x() + diff) { //Left
- mode = RESIZEL;
- } else { //Right
- mode = RESIZER;
- }
- }
- // проверка положения курсора по вертикали
- else if (((e_pos.y() > y() + height() - diff)) || //Bottom
- (e_pos.y() < y() + diff)) { //Top
- if (e_pos.y() < y() + diff) { //Top
- mode = RESIZET;
- } else { //Bottom
- mode = RESIZEB;
- }
- } else {
- mode = MOVE;
- }
- }
- }
- if (!m_isEditing) return;
- if (!m_infocus) return;
- setCursorShape(p);
- return;
- }
- if (toMove.x() < 0) return;
- if (toMove.y() < 0) return;
- if (toMove.x() > this->parentWidget()->width()-this->width()) return;
- move(toMove);
- emit newGeometry(this->geometry());
- this->parentWidget()->repaint();
- return;
- }
- switch (mode){
- case RESIZETL: { //Left-Top
- int newwidth = e->globalX() - position.x() - geometry().x();
- int newheight = e->globalY() - position.y() - geometry().y();
- resize(this->geometry().width()-newwidth,this->geometry().height()-newheight);
- move(toMove.x(),toMove.y());
- break;
- }
- case RESIZETR: { //Right-Top
- int newheight = e->globalY() - position.y() - geometry().y();
- resize(e->x(),this->geometry().height()-newheight);
- move(this->x(),toMove.y());
- break;
- }
- case RESIZEBL: { //Left-Bottom
- int newwidth = e->globalX() - position.x() - geometry().x();
- resize(this->geometry().width()-newwidth,e->y());
- move(toMove.x(),this->y());
- break;
- }
- case RESIZEB: { //Bottom
- resize(width(),e->y()); break;}
- case RESIZEL: { //Left
- int newwidth = e->globalX() - position.x() - geometry().x();
- resize(this->geometry().width()-newwidth,height());
- move(toMove.x(),this->y());
- break;
- }
- case RESIZET: { //Top
- int newheight = e->globalY() - position.y() - geometry().y();
- resize(width(),this->geometry().height()-newheight);
- move(this->x(),toMove.y());
- break;
- }
- case RESIZER: { //Right
- resize(e->x(),height()); break;}
- case RESIZEBR: { //Right-Bottom
- resize(e->x(),e->y()); break;}
- }
- this->parentWidget()->repaint();
- }
- emit newGeometry(this->geometry());
- //QWidget::mouseMoveEvent(e);
- }

