title: How to resize a frameless widget in Qt
body: <p>I am researching on how to resize the frameless widget in Qt 5.1. Due to cross-platform consideration, Windows-related codes will not be appropriate.</p>
  
  <p>Now my method is: when a user hovers mouse at the edge of the window(widget),cursor shape changes to hint the window can be resized at that time. After clicking and then drag, a rubberband is being shown to demonstrate the rectangle area that the window would resize to. Once the user releases the mouse, rubberband disappears, and the window then resize to the desired size. Have I made it clear?</p>
  
  <p>Since Qt can't make drawing directly on the screen, as a makeshift, a full screenshot(put in a QLabel) is utilized to simulate the actual screen, and a rubberband can then be constructed in the full-screen QLabel. It seems to the users that they are dragging the window in the real window screen.</p>
  
  <p>My problem is: after the user clicked the main widget,though a full-screen QLabel is shown, the rubberband refuses to appear. Instead, only after you release mouse first and then click-drag again can the rubberband appear.</p>
  
  <p>Although I have sent one "MouseButtonRelease" event to the main widget and a "MouseMove" event to the  label, it doesn't seem to work. Any hint or suggestion will be highly appreciated, thanks a lot!</p>
  
  <p>(To simplify the code, any other extra codes have been removed)</p>
  
  <p>mainwidget.h</p>
  
  <pre><code>    class MainWidget : public QWidget
      {
          Q_OBJECT
  
      public:
          MainWidget(QWidget *parent = 0);
          ~MainWidget();
  
      private:
          QScreen *screen;
  
          QLabel *fullScreenLabel;
          QPixmap fullScreenPixmap;
          QRubberBand *rubberBand;
  
      protected:
          virtual bool eventFilter(QObject *o, QEvent *e);
  
          void mousePressEvent(QMouseEvent *e);
      };
  </code></pre>
  
  <p>mainwidget.cpp</p>
  
  <pre><code>    MainWidget::MainWidget(QWidget *parent)
          : QWidget(parent)
      {
          this-&gt;setWindowFlags(Qt::FramelessWindowHint);
          screen = QGuiApplication::primaryScreen();
          rubberBand=NULL;
  
          fullScreenLabel=new QLabel();
          fullScreenLabel-&gt;installEventFilter(this);
  
          resize(600,450);
      }
  
      MainWidget::~MainWidget()
      {
          delete fullScreenLabel;
      }
  
      bool MainWidget::eventFilter(QObject *o, QEvent *e)
      {
          if(o!=fullScreenLabel)
              return QWidget::eventFilter(o,e);
  
          QMouseEvent *mouseEvent=static_cast&lt;QMouseEvent*&gt;(e);
  
          if(mouseEvent-&gt;type()==QEvent::MouseMove)
          {
              qDebug()&lt;&lt;"label mouse move: "&lt;&lt; mouseEvent-&gt;pos();
              if(!rubberBand)
              {
                  rubberBand=new QRubberBand(QRubberBand::Rectangle,fullScreenLabel);
                  rubberBand-&gt;setGeometry(QRect(this-&gt;pos(),QSize()));
                  rubberBand-&gt;show();
              }
  
              rubberBand-&gt;setGeometry(QRect(this-&gt;pos(),mouseEvent-&gt;pos()).normalized());
  
              return true;
          }
  
          if((mouseEvent-&gt;button()==Qt::LeftButton)
                  &amp;&amp; (mouseEvent-&gt;type()==QEvent::MouseButtonRelease))
          {        
              if(rubberBand)
              {
                  rubberBand-&gt;hide();
                  delete rubberBand;
                  rubberBand=NULL;
              }
  
              return true;
          }
          return false;
      }
  
      void MainWidget::mousePressEvent(QMouseEvent *e)
      {
          if(screen)
          {
              fullScreenPixmap=QPixmap();
              fullScreenPixmap=screen-&gt;grabWindow(0,0,0,-1,-1);
          }
  
          fullScreenLabel-&gt;setPixmap(fullScreenPixmap);
          fullScreenLabel-&gt;showFullScreen();
  
          QMouseEvent clickEvent(QEvent::MouseButtonRelease,
                                 e-&gt;pos(),
                                 Qt::LeftButton,
                                 Qt::LeftButton,
                                 Qt::NoModifier);
          qApp-&gt;sendEvent(this,&amp;clickEvent);
  
          QMouseEvent moveEvent(QEvent::MouseMove,
                                 this-&gt;pos(),
                                 Qt::NoButton,
                                 Qt::NoButton,
                                 Qt::NoModifier);
          qApp-&gt;sendEvent(fullScreenLabel,&amp;moveEvent);
      }
  </code></pre>