当前位置:   article > 正文

设置鼠标可拖动界面窗体_qt 设置界面可以拖动

qt 设置界面可以拖动

QT创建可拖动界面

一、.h文件

//头文件
public:
void setResizable(bool);//设置鼠标是否可以拖动改变大小
protected:
void mouseMoveEvent(QMouseEvent *event) override;//拖动鼠标
void mousePressEvent(QMouseEvent *event) override;
void mouseReleaseEvent(QMouseEvent *event) override;
private:
bool m_bCanResize;//鼠标移动标志
QPoint m_mousePoint;
bool m_iFlag;

二、.cpp文件

void MainWindow::setResizable(bool bCanResize)
{
m_bCanResize = bCanResize;
}
//鼠标移动位置
void MainWindow::mouseMoveEvent(QMouseEvent *event)
{
if (m_iFlag && (event->buttons() == Qt::LeftButton)) {
bool flag=this->isMaximized();
if(flag)
{
this->showNormal();
resize(1536,864);
this->setResizable(true);
}
move(event->globalPos() - m_mousePoint);
event->accept();
}
}
void MainWindow::mousePressEvent(QMouseEvent *event)
{
int y = event->pos().y();
if ((event->buttons() == Qt::LeftButton) && (y <= 58)) {
m_iFlag = true;
m_mousePoint = event->globalPos() - this->pos();
event->accept();
}
}

void MainWindow::mouseReleaseEvent(QMouseEvent *event)
{
m_iFlag = false;
QWidget::mouseReleaseEvent(event);
}

三、界面设置标题文本和图标显示

this->setWindowFlags(Qt::Window | Qt::FramelessWindowHint);
this->setWindowTitle(QStringLiteral("时间分析"));//下面菜单栏文本
this->setWindowIcon(QIcon(":/res/logo.png"));//修改菜单栏图标
  • 1
  • 2
  • 3
声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/IT小白/article/detail/137584
推荐阅读
相关标签
  

闽ICP备14008679号