Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

QT4 Drag Window Without Title Bar

Tags:

qt4

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

like image 819
Michael Avatar asked Sep 01 '09 07:09

Michael


2 Answers

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;
    }
}
like image 83
erelender Avatar answered Sep 24 '22 03:09

erelender


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;
    }
}
like image 21
user2697311 Avatar answered Sep 26 '22 03:09

user2697311