技术专栏
QT从零入门教程(十一):QT自定义窗口
ifndef CUSTOMWINDOW_H
#define CUSTOMWINDOW_H
#include <QtGui>
#include <QtWidgets>
#include <QMenuBar>
#include <QMainWindow>
class CustomWindow : public QDialog
{
Q_OBJECT
public:
CustomWindow(QWidget *parent = 0);
~CustomWindow();
protected:
virtual void mousePressEvent(QMouseEvent *event);
virtual void mouseMoveEvent(QMouseEvent *event);
virtual void mouseReleaseEvent(QMouseEvent *event);
private:
bool mMoveing;
QPoint mMovePosition;
};
#endif // CUSTOMWINDOW_H
.
#include <QtGui>
#include <QtWidgets>
#include <QMenuBar>
#include <QMainWindow>
#include "header/CustomWindow.h"
CustomWindow::CustomWindow(QWidget *parent)
{
mMoveing = false;
//Qt::FramelessWindowHint 无边框
//Qt::WindowStaysOnTopHint 窗口在最顶端,不会拖到任务栏下面
setWindowFlags(Qt::FramelessWindowHint | Qt::WindowMinimizeButtonHint | Qt::WindowStaysOnTopHint);
}
CustomWindow::~CustomWindow()
{
}
//重写鼠标按下事件
void CustomWindow::mousePressEvent(QMouseEvent *event)
{
mMoveing = true;
//记录下鼠标相对于窗口的位置
//event->globalPos()鼠标按下时,鼠标相对于整个屏幕位置
//pos() this->pos()鼠标按下时,窗口相对于整个屏幕位置
mMovePosition = event->globalPos() - pos();
return QDialog::mousePressEvent(event);
}
//重写鼠标移动事件
void CustomWindow::mouseMoveEvent(QMouseEvent *event)
{
//(event->buttons() && Qt::LeftButton)按下是左键
//鼠标移动事件需要移动窗口,窗口移动到哪里呢?就是要获取鼠标移动中,窗口在整个屏幕的坐标,然后move到这个坐标,怎么获取坐标?
//通过事件event->globalPos()知道鼠标坐标,鼠标坐标减去鼠标相对于窗口位置,就是窗口在整个屏幕的坐标
if (mMoveing && (event->buttons() && Qt::LeftButton)
&& (event->globalPos() - mMovePosition).manhattanLength() > QApplication::startDragDistance())
{
move(event->globalPos() - mMovePosition);
mMovePosition = event->globalPos() - pos();
}
return QDialog::mouseMoveEvent(event);
}
void CustomWindow::mouseReleaseEvent(QMouseEvent *event)
{
mMoveing = false;
}
.
实例
接下来是使用这个CustomWindow类的方法与实例:
// 在“图像处理自编系统”中,“预览”窗口及“关于”弹窗都用到了自定义窗口
// 为方便演示,在工具栏上加一按钮,以“关于”为例。
QPushButton *button = new QPushButton(tr("关于"));
ui.mainToolBar->addWidget(button);
connect(button, SIGNAL(clicked()), this, SLOT(showWin()));
// 下面完善槽函数
void mainWindow::showWin()
{
CustomWindow *helpWin = new CustomWindow(); // 此处对类进行实例化
helpWin->resize(600, 400); // 设置图像大小
QLabel *label_about = new QLabel(helpWin);
label_about->setText(tr("图像处理自编软件 1.0 版"));
QLabel *label_right = new QLabel(helpWin);
label_right->setText(tr("Copyright (C) 2018 深圳 ATR"));
QLabel *label_author = new QLabel(helpWin);
label_author->setText(tr("作者:笔尖 http://blog.csdn.net/u013165921"));
QPushButton *button_ok = new QPushButton(helpWin);
button_ok->setText(tr("确定"));
connect(button_ok, SIGNAL(clicked()), helpWin, SLOT(close()));
label_about->move(100, 100);
label_right->move(100, 180);
label_author->move(100, 260);
button_ok->move(400, 180);
helpWin->exec(); // 模态对话框,关闭该子窗口前不能对主窗口进行任何操作。
}
原文连接:https://blog.csdn.net/u013165921/article/details/79391330
声明:本文内容由易百纳平台入驻作者撰写,文章观点仅代表作者本人,不代表易百纳立场。如有内容侵权或者其他问题,请联系本站进行删除。
红包
点赞
收藏
评论
打赏
- 分享
- 举报
评论
0个
手气红包
暂无数据
相关专栏
-
浏览量:9241次2020-08-19 18:36:04
-
浏览量:3078次2020-08-18 20:09:59
-
浏览量:2130次2020-08-19 18:24:06
-
浏览量:3406次2020-09-20 21:19:24
-
浏览量:14734次2020-11-12 21:55:56
-
浏览量:2995次2020-08-19 18:32:47
-
浏览量:5281次2020-09-23 23:07:37
-
浏览量:3207次2020-08-19 18:27:30
-
浏览量:2451次2020-05-06 15:52:54
-
浏览量:32618次2021-03-03 17:25:19
-
浏览量:8002次2020-12-12 17:47:04
-
浏览量:7757次2020-12-12 17:55:00
-
浏览量:9121次2020-08-19 22:41:20
-
浏览量:13221次2021-08-13 16:08:47
-
浏览量:4949次2017-11-23 12:40:31
-
浏览量:1938次2020-08-03 12:02:37
-
浏览量:211次2024-02-28 15:36:09
-
浏览量:260次2023-08-03 17:18:52
-
浏览量:1568次2020-08-03 12:01:28
置顶时间设置
结束时间
删除原因
-
广告/SPAM
-
恶意灌水
-
违规内容
-
文不对题
-
重复发帖
打赏作者
在学了在学了!
您的支持将鼓励我继续创作!
打赏金额:
¥1
¥5
¥10
¥50
¥100
支付方式:
微信支付
打赏成功!
感谢您的打赏,如若您也想被打赏,可前往 发表专栏 哦~
举报反馈
举报类型
- 内容涉黄/赌/毒
- 内容侵权/抄袭
- 政治相关
- 涉嫌广告
- 侮辱谩骂
- 其他
详细说明
审核成功
发布时间设置
发布时间:
请选择发布时间设置
是否关联周任务-专栏模块
审核失败
失败原因
请选择失败原因
备注
请输入备注