Frameless window with dragging



  • This is how to to make a frameless window with dragging

    This is "mainwindow.h"

    #ifndef MAINWINDOW_H
    #define MAINWINDOW_H
    
    #include <QMainWindow>
    #include <QMouseEvent>
    #include <QPoint>
    
    namespace Ui {
    class MainWindow;
    }
    
    class MainWindow : public QMainWindow
    {
        Q_OBJECT
    
    public:
        explicit MainWindow(QWidget *parent = 0);
        ~MainWindow();
    
    private:
        Ui::MainWindow *ui;
    
    private:
        void mousePressEvent(QMouseEvent *event);
        void mouseMoveEvent(QMouseEvent *event);
        int m_nMouseClick_X_Coordinate;
        int m_nMouseClick_Y_Coordinate;
    };
    
    #endif // MAINWINDOW_H
    

    This is "mainwindow.cpp"

    #include "mainwindow.h"
    #include "ui_mainwindow.h"
    
    MainWindow::MainWindow(QWidget *parent) :
        QMainWindow(parent,Qt::FramelessWindowHint),
        ui(new Ui::MainWindow)
    {
        ui->setupUi(this);
    }
    
    MainWindow::~MainWindow()
    {
        delete ui;
    }
    void MainWindow::mousePressEvent(QMouseEvent *event) {
        m_nMouseClick_X_Coordinate = event->x();
        m_nMouseClick_Y_Coordinate = event->y();
    }
    
    void MainWindow::mouseMoveEvent(QMouseEvent *event) {
        move(event->globalX()-m_nMouseClick_X_Coordinate,event->globalY()-m_nMouseClick_Y_Coordinate);
    }
    
    

    This is "main.cpp"

    #include "mainwindow.h"
    #include <QApplication>
    
    int main(int argc, char *argv[])
    {
        QApplication a(argc, argv);
        MainWindow w;
        w.show();
    
        return a.exec();
    }
    
    

    Adding this to get full windowbar functions

    void MainWindow::on_Minimize_clicked()
    {
        this->setWindowState(Qt::WindowMinimized);
    }
    
    void MainWindow::on_Maximize_clicked()
    {
        this->setWindowState(Qt::WindowMaximized);
    }
    
    void MainWindow::on_Close_clicked()
    {
        this->close();
    }
    
    

    This code works with a .ui file.i mean gui is written in with qt designer.
    This work good.Is there any code to add to make it more suitable ?


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.