qdialog无边框拖拽代码如何实现
时间: 2023-04-09 20:04:49 浏览: 152
可以使用以下代码实现qdialog无边框拖拽:
```
class CustomDialog : public QDialog
{
public:
CustomDialog(QWidget *parent = 0) : QDialog(parent)
{
setWindowFlags(Qt::FramelessWindowHint);
setAttribute(Qt::WA_TranslucentBackground);
}
protected:
void mousePressEvent(QMouseEvent *event)
{
if (event->button() == Qt::LeftButton) {
m_dragging = true;
m_dragPosition = event->globalPos() - frameGeometry().topLeft();
event->accept();
}
}
void mouseMoveEvent(QMouseEvent *event)
{
if (m_dragging && (event->buttons() & Qt::LeftButton)) {
move(event->globalPos() - m_dragPosition);
event->accept();
}
}
void mouseReleaseEvent(QMouseEvent *event)
{
if (event->button() == Qt::LeftButton) {
m_dragging = false;
event->accept();
}
}
private:
bool m_dragging;
QPoint m_dragPosition;
};
```
这个代码可以让QDialog无边框,并且可以通过鼠标拖拽移动窗口。
阅读全文