Widget-moveable-and-resizeable
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
<br />/*<br />Programmer Aleksey Osipov<br />email: aliks-os
yandex.ru
*/
- ifndef MAINWINDOW_H
#define MAINWINDOW_H
- include <QMainWindow>
#include "tcontainer.h"
namespace Ui {
class MainWindow;
}
class MainWindow : public QMainWindow
{
Q_OBJECT
public:
explicit MainWindow(QWidget *parent = 0);
~MainWindow();
private:
Ui::MainWindow ui;
};
#endif // MAINWINDOW_H
<br />mainwindow.cpp
<br />
/
Programmer Aleksey Osipov
email: aliks-os
yandex.ru<br />*/<br />#include "mainwindow.h&quot;<br />#include "ui_mainwindow.h&quot;
MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent), ui(new Ui::MainWindow) {<br /> ui->setupUi(this);<br /> this->showMaximized();<br /> QLabel *lab1 = new QLabel("Label1&quot;);<br /> QLabel *lab2 = new QLabel("Label2&quot;);<br /> TContainer *con1 = new TContainer(this,QPoint(10,10),lab1);<br /> TContainer '''con2 = new TContainer(this,QPoint(20,50),lab2);<br />}
<br />MainWindow::~MainWindow() {<br /> delete ui;<br />}<br />
tcontainer.h
<br />/'''<br />Programmer Aleksey Osipov<br />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
};
class TContainer : public QWidget {
Q_OBJECT
public:
TContainer(QWidget *parent, QPoint p, QWidget *cWidget = 0);
~TContainer();
QWidget *childWidget;
QMenu *menu;
void setChildWidget(QWidget cWidget);
protected:
int mode;
QPoint position;
QVBoxLayout vLayout;
void setCursorShape(const QPoint &e_pos);
bool eventFilter( QObject *obj, QEvent evt );
void keyPressEvent(QKeyEvent);
void focusInEvent(QFocusEvent );
void focusOutEvent(QFocusEvent);
void mousePressEvent(QMouseEvent );
void mouseReleaseEvent(QMouseEvent);
void mouseMoveEvent(QMouseEvent *);
bool m_infocus;
bool m_showMenu;
bool m_isEditing;
void popupShow(const QPoint &pt);
QWidget clone();
private:
signals:
void inFocus(bool mode);
void outFocus(bool mode);
void newGeometry(QRect rect);
public slots:
};
#endif // TCONTAINER_H
<br />tcontainer.cpp
<br />
/
Programmer Aleksey Osipov
email: aliks-os
yandex.ru<br />*/
#include "tcontainer.h&quot;
TContainer::TContainer(QWidget *parent, QPoint p, QWidget *cWidget) : QWidget(parent) {<br /> mode = NONE;<br /> childWidget = cWidget;<br /> setAttribute(Qt::WA_DeleteOnClose);<br /> this->setVisible(true);<br /> this->setAutoFillBackground(false);<br /> this->setMouseTracking(true);<br /> this->setFocusPolicy(Qt::ClickFocus);<br /> this->setFocus();<br /> this->move(p);
vLayout = new QVBoxLayout(this);<br /> if (cWidget != 0) {<br /> cWidget->setParent(this);<br /> cWidget->releaseMouse();<br /> cWidget->setAttribute(Qt::WA_TransparentForMouseEvents, true);<br /> vLayout->addWidget(cWidget);<br /> vLayout->setContentsMargins(0,0,0,0);<br /> }<br /> this->setLayout(vLayout);
m_infocus = true;<br /> m_showMenu = false;<br /> m_isEditing = true;<br /> this->installEventFilter(parent);<br />}
TContainer::~TContainer() {<br /> delete vLayout;<br />}
void TContainer::setChildWidget(QWidget *cWidget) {<br /> if (cWidget != 0) {<br /> childWidget = cWidget;<br /> childWidget->setAttribute(Qt::WA_TransparentForMouseEvents, true);<br /> childWidget->setParent(this);<br /> vLayout->addWidget(cWidget);<br /> vLayout->setContentsMargins(0,0,0,0);<br /> }<br />}
void TContainer::popupShow(const QPoint &pt) {<br /> if (menu->isEmpty()) return;<br /> QPoint global = this->mapToGlobal(pt);<br /> m_showMenu = true;<br /> menu->exec&amp;#40;global&amp;#41;;<br /> m_showMenu = false;<br />}
void TContainer::focusInEvent(QFocusEvent *e) {<br /> m_infocus = true;<br /> this->parentWidget()<s>>installEventFilter(this);<br /> this</s>>parentWidget()<s>>repaint();<br /> emit inFocus(true);<br />}
<br />void TContainer::focusOutEvent(QFocusEvent *e) {<br /> if (!m_isEditing) return;<br /> if (m_showMenu) return;<br /> mode = NONE;<br /> emit outFocus(false);<br /> m_infocus = false;<br />}
<br />bool TContainer::eventFilter( QObject *obj, QEvent *evt ) {<br /> //return QWidget::eventFilter(obj, evt);<br /> if (m_infocus) {<br /> QWidget *w = this</s>>parentWidget();<br /> if (w obj &amp;&amp; evt-&gt;type()QEvent::Paint) {<br /> //Рисуем выделение контейнара<br /> QPainter painter(w);<br /> QPoint p = this->mapTo(w,QPoint(–3,<s>3));<br /> QPoint LT = w</s>>mapFrom(w,p);<br /> QPoint LB = w->mapFrom(w,QPoint(p.x(),p.y()+this->height()));<br /> QPoint RB = w->mapFrom(w,QPoint(p.x()+this->width(),p.y()+this->height()));<br /> QPoint RT = w->mapFrom(w,QPoint(p.x()+this->width(),p.y()));
painter.fillRect(LT.x(),LT.y(),6,6,QColor("black&quot;));<br /> painter.fillRect(LB.x(),LB.y(),6,6,QColor("black&quot;));<br /> painter.fillRect(RB.x(),RB.y(),6,6,QColor("black&quot;));<br /> painter.fillRect(RT.x(),RT.y(),6,6,QColor("black&quot;));<br /> return QWidget::eventFilter(obj,evt);<br /> }<br /> }<br /> return QWidget::eventFilter(obj, evt);}
void TContainer::mousePressEvent(QMouseEvent *e) {<br /> position = QPoint(e->globalX()<s>geometry().x(), e</s>>globalY()<s>geometry().y());<br /> if (!m_isEditing) return;<br /> if (!m_infocus) return;<br /> //QWidget::mouseMoveEvent(e);<br /> if (!e</s>>buttons() & Qt::LeftButton) {<br /> setCursorShape(e->pos());<br /> return;<br /> }<br /> if(e->button()==Qt::RightButton) {<br /> popupShow(e->pos());<br /> e->accept();<br /> }<br />}
void TContainer::keyPressEvent(QKeyEvent *e) {<br /> if (!m_isEditing) return;<br /> if (e->key() Qt::Key_Delete) &#123;
this-&gt;deleteLater();
&#125;
//Двигаем контайнер при помощи клавиш
if (QApplication::keyboardModifiers() Qt::ControlModifier) {<br /> QPoint newPos(this->x(),this->y());<br /> if (e->key() Qt::Key_Up) newPos.setY(newPos.y()-1);
if (e-&gt;key() Qt::Key_Down) newPos.setY(newPos.y()+1);<br /> if (e->key() Qt::Key_Left) newPos.setX(newPos.x()-1);
if (e-&gt;key() Qt::Key_Right) newPos.setX(newPos.x()+1);<br /> move(newPos);<br /> }<br /> if (QApplication::keyboardModifiers() Qt::ShiftModifier) &#123;
if (e-&gt;key() Qt::Key_Up) resize(width(),height()<s>1);<br /> if (e</s>>key() Qt::Key_Down) resize(width(),height()+1);
if (e-&gt;key() Qt::Key_Left) resize(width()<s>1,height());<br /> if (e</s>>key() == Qt::Key_Right) resize(width()''1,height());<br /> }<br /> emit newGeometry(this->geometry());<br />}
<br />void TContainer::setCursorShape(const QPoint &e_pos) {<br /> const int diff = 3;<br /> if (<br /> //Left-Bottom<br /> ((e_pos.y() > y()'' height() - diff) && //Bottom<br /> (e_pos.x() < x()''diff)) || //Left<br /> //Right-Bottom<br /> ((e_pos.y() > y()'' height() - diff) && //Bottom<br /> (e_pos.x() > x() + width() - diff)) || //Right<br /> //Left-Top<br /> ((e_pos.y() < y() + diff) && //Top<br /> (e_pos.x() < x() + diff)) || //Left<br /> //Right-Top<br /> ((e_pos.y() < y() + diff) && //Top<br /> (e_pos.x() > x() + width() - diff)) //Right<br /> )<br /> {<br /> //Left-Bottom<br /> if ((e_pos.y() > y() + height() - diff) && //Bottom<br /> (e_pos.x() < x() + diff)) { //Left<br /> mode = RESIZEBL;<br /> setCursor(QCursor(Qt::SizeBDiagCursor));<br /> }<br /> //Right-Bottom<br /> if ((e_pos.y() > y() + height() - diff) && //Bottom<br /> (e_pos.x() > x() + width() - diff)) { //Right<br /> mode = RESIZEBR;<br /> setCursor(QCursor(Qt::SizeFDiagCursor));<br /> }<br /> //Left-Top<br /> if ((e_pos.y() < y() + diff) && //Top<br /> (e_pos.x() < x() + diff)) { //Left<br /> mode = RESIZETL;<br /> setCursor(QCursor(Qt::SizeFDiagCursor));<br /> }<br /> //Right-Top<br /> if ((e_pos.y() < y() + diff) && //Top<br /> (e_pos.x() > x() + width() - diff)) { //Right<br /> mode = RESIZETR;<br /> setCursor(QCursor(Qt::SizeBDiagCursor));<br /> }<br /> }<br /> // проверка положения курсора по горизонтали<br /> else if ((e_pos.x() < x() + diff) || //Left<br /> ((e_pos.x() > x() + width() - diff))) { //Right<br /> if (e_pos.x() < x() + diff) { //Left<br /> setCursor(QCursor(Qt::SizeHorCursor));<br /> mode = RESIZEL;<br /> } else { //Right<br /> setCursor(QCursor(Qt::SizeHorCursor));<br /> mode = RESIZER;<br /> }<br /> }<br /> // проверка положения курсора по вертикали<br /> else if (((e_pos.y() > y() + height() - diff)) || //Bottom<br /> (e_pos.y() < y() + diff)) { //Top<br /> if (e_pos.y() < y() + diff) { //Top<br /> setCursor(QCursor(Qt::SizeVerCursor));<br /> mode = RESIZET;<br /> } else { //Bottom<br /> setCursor(QCursor(Qt::SizeVerCursor));<br /> mode = RESIZEB;<br /> }<br /> } else {<br /> setCursor(QCursor(Qt::ArrowCursor));<br /> mode = MOVE;<br /> }<br />}
void TContainer::mouseReleaseEvent(QMouseEvent *e) {<br /> QWidget::mouseReleaseEvent(e);<br />}
void TContainer::mouseMoveEvent(QMouseEvent *e) {<br /> QWidget::mouseMoveEvent(e);<br /> if (!m_isEditing) return;<br /> if (!m_infocus) return;<br /> if (!e->buttons() & Qt::LeftButton) {<br /> QPoint p = QPoint(e->x()+geometry().x(), e->y()+geometry().y());<br /> setCursorShape(p);<br /> return;<br /> }
if ((mode MOVE || mode NONE) && e->buttons() && Qt::LeftButton) {<br /> QPoint toMove = e->globalPos() - position;<br /> if (toMove.x() < 0) return;<br /> if (toMove.y() < 0) return;<br /> if (toMove.x() > this->parentWidget()->width()<s>this</s>>width()) return;<br /> move(toMove);<br /> emit newGeometry(this->geometry());<br /> this->parentWidget()<s>>repaint();<br /> return;<br /> }<br /> if ((mode != MOVE) && e</s>>buttons() && Qt::LeftButton) {<br /> switch (mode){<br /> case RESIZETL: { //Left-Top<br /> int newwidth = e->globalX() - position.x() - geometry().x();<br /> int newheight = e->globalY() - position.y() - geometry().y();<br /> QPoint toMove = e->globalPos() - position;<br /> resize(this->geometry().width()<s>newwidth,this</s>>geometry().height()<s>newheight);<br /> move(toMove.x(),toMove.y());<br /> break;<br /> }<br /> case RESIZETR: { //Right-Top<br /> int newheight = e</s>>globalY() - position.y() - geometry().y();<br /> QPoint toMove = e->globalPos() - position;<br /> resize(e->x(),this->geometry().height()<s>newheight);<br /> move(this</s>>x(),toMove.y());<br /> break;<br /> }<br /> case RESIZEBL: { //Left-Bottom<br /> int newwidth = e->globalX() - position.x() - geometry().x();<br /> QPoint toMove = e->globalPos() - position;<br /> resize(this->geometry().width()<s>newwidth,e</s>>y());<br /> move(toMove.x(),this->y());<br /> break;<br /> }<br /> case RESIZEB: { //Bottom<br /> resize(width(),e->y()); break;}<br /> case RESIZEL: { //Left<br /> int newwidth = e->globalX() - position.x() - geometry().x();<br /> QPoint toMove = e->globalPos() - position;<br /> resize(this->geometry().width()<s>newwidth,height());<br /> move(toMove.x(),this</s>>y());<br /> break;<br /> }<br /> case RESIZET: { //Top<br /> int newheight = e->globalY() - position.y() - geometry().y();<br /> QPoint toMove = e->globalPos() - position;<br /> resize(width(),this->geometry().height()<s>newheight);<br /> move(this</s>>x(),toMove.y());<br /> break;<br /> }<br /> case RESIZER: { //Right<br /> resize(e->x(),height()); break;}<br /> case RESIZEBR: { //Right-Bottom<br /> resize(e->x(),e->y()); break;}<br /> }<br /> this->parentWidget()<s>>repaint();<br /> }<br /> emit newGeometry(this</s>>geometry());<br /> //QWidget::mouseMoveEvent(e);<br />}<br />