11

The application I'm working on has a custom UI that required me to remove the title bar from the main window. Unfortunately, I can't figure out how to make it so I can move the application on the screen :)

The code I have which is removing the title bar is the following:

MainWindow::MainWindow(QWidget *parent)
    : QMainWindow(parent, Qt::CustomizeWindowHint), ui(new Ui::MainWindowClass)
{
    ui->setupUi(this);

Any idea how I can move the window around using either another widget or the main form window itself?

Thanks

3 Answers 3

13

Most intuitive way to do this would be clicking on the widget surface and dragging. In order to achieve this, you need to follow some steps.

The method goes as follows: When the user presses inside the widget, set a boolean flag and store the position of the mouse and then when the button is released, set it to false. The next step is moving the widget. In the mouseMoveEvent, check if the flag is set. If it is set, take the new position of the mouse. Calculate the difference between new position and the stored one. Then, set the position of the window to original position + the calculated mouse movement. Then store the new mouse position.

The code required would be this:

WARNING: This code example is incorrect and will result in a jumping behavior when dragging the window. Please use the code from the Qt Shaped Clock example instead.

/// Header contents:
class MyWidget : public QMainWindow
{
protected:
    void mouseMoveEvent(QMouseEvent* event);
    void mousePressEvent(QMouseEvent* event);
    void mouseReleaseEvent(QMouseEvent* event);
private:
    QPoint mLastMousePosition;
    bool mMoving;
}
/// Source:
void MyWidget::mousePressEvent(QMouseEvent* event)
{
    if(event->button() == Qt::LeftButton)
    {
        mMoving = true;
        mLastMousePosition = event->pos();
    }
}

void MyWidget::mouseMoveEvent(QMouseEvent* event)
{
    if( event->buttons().testFlag(Qt::LeftButton) && mMoving)
    {
        this->move(this->pos() + (event->pos() - mLastMousePosition));
        mLastMousePosition = event->pos();
    }
}

void MyWidget::mouseReleaseEvent(QMouseEvent* event)
{
    if(event->button() == Qt::LeftButton)
    {
        mMoving = false;
    }
}
5
  • 3
    See also Qt's shaped clock example, which does the same thing: doc.trolltech.com/4.5/widgets-shapedclock.html Sep 1, 2009 at 23:09
  • erelender, Even after over a year I still come back to my own question to get your answer. Thanks again for providing this. =) I do use globalPos() instead though, otherwise it's jumpy
    – Michael
    Jan 30, 2012 at 17:51
  • As I mentioned below, using window->move() only moves the window inside the screen, no corner can be moved into outside, this is very different from what a windows manager does
    – saeedgnu
    Nov 22, 2012 at 5:16
  • That is why SO discourage using LINKS in the answer. All the links for the clock example are dead. Good job guys.
    – GeneCode
    Sep 14, 2017 at 1:19
  • Using links is OK as long as the answer is not entirely links. For example, this answer would still be valid if the links were removed. They are only intended to enhance the answer. Furthermore, the intended target is not some obscure corner of the web, it's still reachable via a simple search. There's no need to be sarcastic. The comment about jumping behavior might be valid though, I remember using this implementation multiple times without such a thing, but things might have changed with the passing years.
    – erelender
    Sep 14, 2017 at 6:41
3

You should try this instead

class MyWidget : public QMainWindow
{
protected:
    void mouseMoveEvent(QMouseEvent* event);
    void mousePressEvent(QMouseEvent* event);
    void mouseReleaseEvent(QMouseEvent* event);
private:
    QPoint mLastMousePosition;
    bool mMoving;
}
/// Source:
void MyWidget::mousePressEvent(QMouseEvent* event)
{
    if(event->button() == Qt::LeftButton)
    {
        mMoving = true;
        mLastMousePosition = event->pos();
    }
}

void MyWidget::mouseMoveEvent(QMouseEvent* event)
{
    if( event->buttons().testFlag(Qt::LeftButton) && mMoving)
    {
        this->move(this->pos() + (event->pos() - mLastMousePosition));
    }
}

void MyWidget::mouseReleaseEvent(QMouseEvent* event)
{
    if(event->button() == Qt::LeftButton)
    {
        mMoving = false;
    }
}
0
0

I don't remenber very well, but you can register a custom event handler, is easy there are so many examples on google. check for mouse event, before that you can use QObject::sender() and get the metaobject to know the class of the object tha is pressed if the object is a qframe or qwidget or another class that you want to use to move the window. then you can apply a logic to drag the window. the pros is that you can use with all window and frame without reimplement each one of them with mouse events

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.