3

I have a QMainWindow with this flag :

this->setWindowFlags(Qt::SubWindow);

How do to forbid the window moving, and this, keeping this window style ?

artoon
  • 729
  • 2
  • 14
  • 41

1 Answers1

2

I don't think there is a cross-os Qt way to achieve this when using the standard window controls.

You can try stuff like:

class Widget : public QWidget {
  Q_OBJECT

public:
  Widget()
    : fixed_pos_(QPoint(100, 100)) {
    setWindowFlags(Qt::SubWindow);
  }

  void SetFixedPos(const QPoint& pos) {
    fixed_pos_ = pos;
  }

protected:
  void moveEvent(QMoveEvent* ev) {
    if (ev->pos() != fixed_pos_)
      move(fixed_pos_);
  }

private:
  QPoint fixed_pos_;
};

These have a few issues like flicker, does not update until Mouse-release and so on that's also different per OS.

Most efficient way is to just make your Window a Qt::FramelessWindowHint and render a titlebar yourself. That way you can pretty much do what you want when it comes to handling events on that titlebar.

Viv
  • 17,170
  • 4
  • 51
  • 71
  • 2
    qt docs specifically warn against calling move() from within moveEvent(): "Warning: Calling move() or setGeometry() inside moveEvent() can lead to infinite recursion." – fyngyrz Dec 24 '16 at 14:31