Qt 圆角矩形+鼠标左键拖动窗口

 #ifndef MAINWINDOW_H
#define MAINWINDOW_H #include <QMainWindow> namespace Ui {
class MainWindow;
} class MainWindow : public QMainWindow
{
Q_OBJECT public:
explicit MainWindow(QWidget *parent = );
~MainWindow(); protected:
void paintEvent(QPaintEvent *event);
void mousePressEvent(QMouseEvent *event);
void mouseMoveEvent(QMouseEvent *event);
void mouseReleaseEvent(QMouseEvent *event); private:
Ui::MainWindow *ui; QPoint m_pos;
bool m_drag;
}; #endif // MAINWINDOW_H
 #include "mainwindow.h"
#include "ui_mainwindow.h" #include <QPainter>
#include <QBitmap>
#include <QMouseEvent> MainWindow::MainWindow(QWidget *parent)
: QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this); ui->menuBar->close();
ui->mainToolBar->close();
ui->statusBar->close(); setFixedSize(, );
setWindowFlags(Qt::FramelessWindowHint); m_drag = false;
} MainWindow::~MainWindow()
{
delete ui;
} void MainWindow::paintEvent(QPaintEvent *event)
{
Q_UNUSED(event);
QBitmap bitmap(this->size());
QPainter painter(&bitmap);
painter.fillRect(this->rect(), Qt::white);
painter.setBrush(QColor(, , ));
painter.setRenderHint(QPainter::Antialiasing);
painter.setPen(QPen(Qt::blue, , Qt::SolidLine, Qt::RoundCap, Qt::RoundJoin));
painter.drawRoundedRect(this->rect(), , );
this->setMask(bitmap);
} void MainWindow::mousePressEvent(QMouseEvent *event)
{
if (Qt::LeftButton == event->button()) {
m_drag = true;
m_pos = event->pos();
}
} void MainWindow::mouseMoveEvent(QMouseEvent *event)
{
Q_UNUSED(event); if (m_drag) {
this->move(event->globalPos() - m_pos);
}
} void MainWindow::mouseReleaseEvent(QMouseEvent *event)
{
Q_UNUSED(event);
m_drag = false;
}
上一篇:requests-所有异常归类


下一篇:python中break和continue的区别