自定义窗口控件的无边框,窗口事件由于没有系统自带边框,无法实现拖拽拉伸等事件的处理,一种方法就是重新重写主窗口的鼠标事件,一种时通过nativeEvent事件处理。重写事件比较繁琐,个人喜欢nativeEvent处理,nativeEvent事件的处理其实也就是把窗口边界的事件转化为Windows的窗口事件.两种方式各有优缺点。例子如下:
mainwindow.h
#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include <QMainWindow>
#include <windows.h>
#include <windowsx.h>
#include <QLabel>
class MainWindow : public QLabel
{
Q_OBJECT
public:
MainWindow(QWidget *parent = 0);
~MainWindow();
public:
bool nativeEvent(const QByteArray &eventType, void *message, long *result);
};
#endif // MAINWINDOW_H
mainwindow.cpp
#include "mainwindow.h"
#include <QDebug>
#include <QDialog>
MainWindow::MainWindow(QWidget *parent)
: QLabel(parent)
{
this->setWindowFlags(Qt::FramelessWindow