Qdialog无边框且可以拖拽代码如何实现
时间: 2023-04-09 16:04:52 浏览: 101
QT无边框窗口,无边框弹出框,无边框MessageBox
可以使用以下代码实现:
```
class CustomDialog : public QDialog
{
public:
CustomDialog(QWidget *parent = nullptr) : QDialog(parent)
{
setWindowFlags(Qt::FramelessWindowHint | Qt::WindowSystemMenuHint);
}
protected:
void mousePressEvent(QMouseEvent *event) override
{
if (event->button() == Qt::LeftButton)
{
m_dragging = true;
m_dragPosition = event->globalPos() - frameGeometry().topLeft();
event->accept();
}
}
void mouseMoveEvent(QMouseEvent *event) override
{
if (m_dragging && (event->buttons() & Qt::LeftButton))
{
move(event->globalPos() - m_dragPosition);
event->accept();
}
}
void mouseReleaseEvent(QMouseEvent *event) override
{
if (event->button() == Qt::LeftButton)
{
m_dragging = false;
event->accept();
}
}
private:
bool m_dragging = false;
QPoint m_dragPosition;
};
```
这个类继承自 QDialog,重写了鼠标事件的处理函数,实现了无边框且可以拖拽的功能。
阅读全文