Widget-moveable-and-resizeable

From Qt Wiki
Revision as of 09:38, 24 February 2015 by Maintenance script (talk | contribs)
Jump to navigation Jump to search

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
*/

  1. ifndef MAINWINDOW_H
    #define MAINWINDOW_H
  1. include <QMainWindow&gt;
    #include "tcontainer.h&quot;

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 &quot;mainwindow.h&amp;quot;<br />#include &quot;ui_mainwindow.h&amp;quot;

MainWindow::MainWindow(QWidget *parent) :  QMainWindow(parent),  ui(new Ui::MainWindow) {<br />    ui-&gt;setupUi(this);<br />    this-&gt;showMaximized();<br />    QLabel *lab1 = new QLabel(&quot;Label1&amp;quot;);<br />    QLabel *lab2 = new QLabel(&quot;Label2&amp;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

  1. include <QWidget&gt;
    #include <QMouseEvent&gt;
    #include <QtGui&gt;

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 &quot;tcontainer.h&amp;quot;

TContainer::TContainer(QWidget *parent, QPoint p, QWidget *cWidget) : QWidget(parent) {<br />    mode = NONE;<br />    childWidget = cWidget;<br />    setAttribute(Qt::WA_DeleteOnClose);<br />    this-&gt;setVisible(true);<br />    this-&gt;setAutoFillBackground(false);<br />    this-&gt;setMouseTracking(true);<br />    this-&gt;setFocusPolicy(Qt::ClickFocus);<br />    this-&gt;setFocus();<br />    this-&gt;move(p);

   vLayout = new QVBoxLayout(this);<br />    if (cWidget != 0) {<br />        cWidget-&gt;setParent(this);<br />        cWidget-&gt;releaseMouse();<br />        cWidget-&gt;setAttribute(Qt::WA_TransparentForMouseEvents, true);<br />        vLayout-&gt;addWidget(cWidget);<br />        vLayout-&gt;setContentsMargins(0,0,0,0);<br />    }<br />    this-&gt;setLayout(vLayout);

   m_infocus = true;<br />    m_showMenu = false;<br />    m_isEditing = true;<br />    this-&gt;installEventFilter(parent);<br />}

TContainer::~TContainer() {<br />    delete vLayout;<br />}

void TContainer::setChildWidget(QWidget *cWidget) {<br />    if (cWidget != 0) {<br />        childWidget = cWidget;<br />        childWidget-&gt;setAttribute(Qt::WA_TransparentForMouseEvents, true);<br />        childWidget-&gt;setParent(this);<br />        vLayout-&gt;addWidget(cWidget);<br />        vLayout-&gt;setContentsMargins(0,0,0,0);<br />    }<br />}

void TContainer::popupShow(const QPoint &amp;pt) {<br />    if (menu-&gt;isEmpty()) return;<br />    QPoint global = this-&gt;mapToGlobal(pt);<br />    m_showMenu = true;<br />    menu-&gt;exec&amp;amp;#40;global&amp;amp;#41;;<br />    m_showMenu = false;<br />}

void TContainer::focusInEvent(QFocusEvent *e) {<br />    m_infocus = true;<br />    this-&gt;parentWidget()<s>&gt;installEventFilter(this);<br />    this</s>&gt;parentWidget()<s>&gt;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>&gt;parentWidget();<br />        if (w  obj &amp;amp;&amp;amp; evt-&amp;gt;type()QEvent::Paint) {<br />            //Рисуем выделение контейнара<br />            QPainter painter(w);<br />            QPoint p = this-&gt;mapTo(w,QPoint(–3,<s>3));<br />            QPoint LT = w</s>&gt;mapFrom(w,p);<br />            QPoint LB = w-&gt;mapFrom(w,QPoint(p.x(),p.y()+this-&gt;height()));<br />            QPoint RB = w-&gt;mapFrom(w,QPoint(p.x()+this-&gt;width(),p.y()+this-&gt;height()));<br />            QPoint RT = w-&gt;mapFrom(w,QPoint(p.x()+this-&gt;width(),p.y()));

           painter.fillRect(LT.x(),LT.y(),6,6,QColor(&quot;black&amp;quot;));<br />            painter.fillRect(LB.x(),LB.y(),6,6,QColor(&quot;black&amp;quot;));<br />            painter.fillRect(RB.x(),RB.y(),6,6,QColor(&quot;black&amp;quot;));<br />            painter.fillRect(RT.x(),RT.y(),6,6,QColor(&quot;black&amp;quot;));<br />            return QWidget::eventFilter(obj,evt);<br />        }<br />    }<br />    return QWidget::eventFilter(obj, evt);}

void TContainer::mousePressEvent(QMouseEvent *e) {<br />    position = QPoint(e-&gt;globalX()<s>geometry().x(), e</s>&gt;globalY()<s>geometry().y());<br />    if (!m_isEditing) return;<br />    if (!m_infocus) return;<br />    //QWidget::mouseMoveEvent(e);<br />    if (!e</s>&gt;buttons() &amp; Qt::LeftButton) {<br />        setCursorShape(e-&gt;pos());<br />        return;<br />    }<br />    if(e-&gt;button()==Qt::RightButton) {<br />        popupShow(e-&gt;pos());<br />        e-&gt;accept();<br />    }<br />}

void TContainer::keyPressEvent(QKeyEvent *e) {<br />    if (!m_isEditing) return;<br />    if (e-&gt;key()  Qt::Key_Delete) &amp;#123;
        this-&amp;gt;deleteLater();
    &amp;#125;
    //Двигаем контайнер при помощи клавиш
    if (QApplication::keyboardModifiers()  Qt::ControlModifier) {<br />        QPoint newPos(this-&gt;x(),this-&gt;y());<br />        if (e-&gt;key()  Qt::Key_Up) newPos.setY(newPos.y()-1);
        if (e-&amp;gt;key()  Qt::Key_Down) newPos.setY(newPos.y()+1);<br />        if (e-&gt;key()  Qt::Key_Left) newPos.setX(newPos.x()-1);
        if (e-&amp;gt;key()  Qt::Key_Right) newPos.setX(newPos.x()+1);<br />        move(newPos);<br />    }<br />    if (QApplication::keyboardModifiers()  Qt::ShiftModifier) &amp;#123;
        if (e-&amp;gt;key()  Qt::Key_Up) resize(width(),height()<s>1);<br />        if (e</s>&gt;key()  Qt::Key_Down) resize(width(),height()+1);
        if (e-&amp;gt;key()  Qt::Key_Left) resize(width()<s>1,height());<br />        if (e</s>&gt;key() == Qt::Key_Right) resize(width()''1,height());<br />    }<br />    emit newGeometry(this-&gt;geometry());<br />}
<br />void TContainer::setCursorShape(const QPoint &amp;e_pos) {<br />    const int diff = 3;<br />    if (<br />            //Left-Bottom<br />            ((e_pos.y() &gt; y()'' height() - diff) &amp;&amp;          //Bottom<br />             (e_pos.x() &lt; x()''diff)) ||                      //Left<br />            //Right-Bottom<br />            ((e_pos.y() &gt; y()'' height() - diff) &amp;&amp;          //Bottom<br />             (e_pos.x() &gt; x() + width() - diff)) ||          //Right<br />            //Left-Top<br />            ((e_pos.y() &lt; y() + diff) &amp;&amp;                     //Top<br />             (e_pos.x() &lt; x() + diff)) ||                    //Left<br />            //Right-Top<br />            ((e_pos.y() &lt; y() + diff) &amp;&amp;                     //Top<br />             (e_pos.x() &gt; x() + width() - diff))             //Right<br />       )<br />    {<br />        //Left-Bottom<br />        if ((e_pos.y() &gt; y() + height() - diff) &amp;&amp;           //Bottom<br />            (e_pos.x() &lt; x() + diff)) {                      //Left<br />            mode = RESIZEBL;<br />            setCursor(QCursor(Qt::SizeBDiagCursor));<br />        }<br />        //Right-Bottom<br />        if ((e_pos.y() &gt; y() + height() - diff) &amp;&amp;           //Bottom<br />            (e_pos.x() &gt; x() + width() - diff)) {            //Right<br />            mode = RESIZEBR;<br />            setCursor(QCursor(Qt::SizeFDiagCursor));<br />        }<br />        //Left-Top<br />        if ((e_pos.y() &lt; y() + diff) &amp;&amp;                      //Top<br />            (e_pos.x() &lt; x() + diff)) {                      //Left<br />            mode = RESIZETL;<br />            setCursor(QCursor(Qt::SizeFDiagCursor));<br />        }<br />        //Right-Top<br />        if ((e_pos.y() &lt; y() + diff) &amp;&amp;                      //Top<br />            (e_pos.x() &gt; x() + width() - diff)) {            //Right<br />            mode = RESIZETR;<br />            setCursor(QCursor(Qt::SizeBDiagCursor));<br />        }<br />    }<br />    // проверка положения курсора по горизонтали<br />    else if ((e_pos.x() &lt; x() + diff) ||             //Left<br />            ((e_pos.x() &gt; x() + width() - diff))) {  //Right<br />        if (e_pos.x() &lt; 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() &gt; y() + height() - diff)) || //Bottom<br />              (e_pos.y() &lt; y() + diff)) {             //Top<br />        if (e_pos.y() &lt; 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-&gt;buttons() &amp; Qt::LeftButton) {<br />        QPoint p = QPoint(e-&gt;x()+geometry().x(), e-&gt;y()+geometry().y());<br />        setCursorShape(p);<br />        return;<br />    }

   if ((mode  MOVE || mode  NONE) &amp;&amp; e-&gt;buttons() &amp;&amp; Qt::LeftButton) {<br />        QPoint toMove = e-&gt;globalPos() - position;<br />        if (toMove.x() &lt; 0) return;<br />        if (toMove.y() &lt; 0) return;<br />        if (toMove.x() &gt; this-&gt;parentWidget()-&gt;width()<s>this</s>&gt;width()) return;<br />        move(toMove);<br />        emit newGeometry(this-&gt;geometry());<br />        this-&gt;parentWidget()<s>&gt;repaint();<br />        return;<br />    }<br />    if ((mode != MOVE) &amp;&amp; e</s>&gt;buttons() &amp;&amp; Qt::LeftButton) {<br />        switch (mode){<br />            case RESIZETL: {  //Left-Top<br />                int newwidth = e-&gt;globalX() - position.x() - geometry().x();<br />                int newheight = e-&gt;globalY() - position.y() - geometry().y();<br />                QPoint toMove = e-&gt;globalPos() - position;<br />                resize(this-&gt;geometry().width()<s>newwidth,this</s>&gt;geometry().height()<s>newheight);<br />                move(toMove.x(),toMove.y());<br />                break;<br />            }<br />            case RESIZETR: {  //Right-Top<br />                int newheight = e</s>&gt;globalY() - position.y() - geometry().y();<br />                QPoint toMove = e-&gt;globalPos() - position;<br />                resize(e-&gt;x(),this-&gt;geometry().height()<s>newheight);<br />                move(this</s>&gt;x(),toMove.y());<br />                break;<br />            }<br />            case RESIZEBL: {  //Left-Bottom<br />                int newwidth = e-&gt;globalX() - position.x() - geometry().x();<br />                QPoint toMove = e-&gt;globalPos() - position;<br />                resize(this-&gt;geometry().width()<s>newwidth,e</s>&gt;y());<br />                move(toMove.x(),this-&gt;y());<br />                break;<br />            }<br />            case RESIZEB: {   //Bottom<br />                resize(width(),e-&gt;y()); break;}<br />            case RESIZEL:  {  //Left<br />                int newwidth = e-&gt;globalX() - position.x() - geometry().x();<br />                QPoint toMove = e-&gt;globalPos() - position;<br />                resize(this-&gt;geometry().width()<s>newwidth,height());<br />                move(toMove.x(),this</s>&gt;y());<br />                break;<br />            }<br />            case RESIZET:  {  //Top<br />                int newheight = e-&gt;globalY() - position.y() - geometry().y();<br />                QPoint toMove = e-&gt;globalPos() - position;<br />                resize(width(),this-&gt;geometry().height()<s>newheight);<br />                move(this</s>&gt;x(),toMove.y());<br />                break;<br />            }<br />            case RESIZER:  {  //Right<br />                resize(e-&gt;x(),height()); break;}<br />            case RESIZEBR: {  //Right-Bottom<br />                resize(e-&gt;x(),e-&gt;y()); break;}<br />        }<br />        this-&gt;parentWidget()<s>&gt;repaint();<br />    }<br />    emit newGeometry(this</s>&gt;geometry());<br />    //QWidget::mouseMoveEvent(e);<br />}<br />