技术专栏
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
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
<
.
#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;
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
- 44
- 45
- 46
<
.
实例
接下来是使用这个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(); // 模态对话框,关闭该子窗口前不能对主窗口进行任何操作。
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
<
原文连接:https://blog.csdn.net/u013165921/article/details/79391330
声明:本文内容由易百纳平台入驻作者撰写,文章观点仅代表作者本人,不代表易百纳立场。如有内容侵权或者其他问题,请联系本站进行删除。
红包
点赞
收藏
评论
打赏
- 分享
- 举报
评论
0个
手气红包

相关专栏
-
浏览量:10470次2020-08-19 18:36:04
-
浏览量:3532次2020-08-18 20:09:59
-
浏览量:2507次2020-08-19 18:24:06
-
浏览量:3876次2020-09-20 21:19:24
-
浏览量:15243次2020-11-12 21:55:56
-
浏览量:3485次2020-08-19 18:32:47
-
浏览量:5971次2020-09-23 23:07:37
-
浏览量:3861次2020-08-19 18:27:30
-
浏览量:3098次2020-05-06 15:52:54
-
浏览量:8465次2020-12-12 17:47:04
-
浏览量:8228次2020-12-12 17:55:00
-
浏览量:9877次2020-08-19 22:41:20
-
浏览量:34488次2021-03-03 17:25:19
-
浏览量:14218次2021-08-13 16:08:47
-
浏览量:5459次2017-11-23 12:40:31
-
浏览量:2254次2020-08-03 12:02:37
-
浏览量:3309次2024-02-28 15:36:09
-
浏览量:1900次2020-08-03 12:01:28
-
浏览量:631次2023-08-03 17:18:52
置顶时间设置
结束时间
删除原因
-
广告/SPAM
-
恶意灌水
-
违规内容
-
文不对题
-
重复发帖
打赏作者

在学了在学了!
您的支持将鼓励我继续创作!
打赏金额:
¥1

¥5

¥10

¥50

¥100

支付方式:

举报反馈
举报类型
- 内容涉黄/赌/毒
- 内容侵权/抄袭
- 政治相关
- 涉嫌广告
- 侮辱谩骂
- 其他
详细说明
审核成功
发布时间设置
发布时间:
请选择发布时间设置
是否关联周任务-专栏模块
审核失败
失败原因
请选择失败原因
备注
请输入备注