QT自定义窗口(模拟MainWindow)
在这里自定义窗口是通过继承于QFrame,内部分为上下两部分,上半部分就是标题栏,下面是窗口的具体内容。上下两部分通过布局固定位置。最后窗口的各种鼠标拖动,缩放,等操作通过添加鼠标事件来完成一个窗口的常规功能。当然,这个标题栏可以通过布局的改变把它放到任意地方。
下面是实现的代码: #pragma once #ifndef CUSTOMWIDGET_H_ #define CUSTOMWIDGET_H_
#include <QtWidgets/QFrame>
class QToolButton;
class CustomWidget : public QFrame
{
Q_OBJECT
public:
explicit CustomWidget();
~CustomWidget();
void setContentWidget(QWidget *contentWidget);
void setTitleInfo(const QString &titleString, const QPixmap &windowIcon);
public slots:
void slotShowSmall();
void slotShowMaxRestore();
void onMoveWindow();
signals:
void moveWindow();
protected:
void mousePressEvent(QMouseEvent *);
void mouseMoveEvent(QMouseEvent *);
void mouseReleaseEvent(QMouseEvent *);
bool nativeEvent(const QByteArray & eventType, void * message, long * result);
void paintEvent(QPaintEvent *);
private:
bool isMax;
bool isPress;
QPoint startPos;
QPoint clickPos;
QWidget *m_pTitleWidget;//顶栏窗体
QWidget *m_pContentWidget;//内容窗体
QPixmap maxPixmap;
QPixmap restorePixmap;
QToolButton *m_pMaxButton;
};
#endif //!CUSTOMWIDGET_H_
customWidget.cpp:
#include "stdafx.h" #include "customwidget.h" #include <QtWidgets/QLabel> #include <QtWidgets/QToolButton> #include <QtWidgets/QHBoxLayout> #include <QtCore/QEvent> #include <Window.h>
static const int TITLE_HEIGHT = 40;//标题栏高度 static const int FRAME_BORDER = 2;//边框宽度 CustomWidget::CustomWidget()
: m_pContentWidget(0)
//, m_llLocalWinId(0)
//, m_llRemoteWinId(0)
{
this->setWindowFlags(Qt::FramelessWindowHint);
this->setAttribute(Qt::WA_TranslucentBackground);
this->setMouseTracking(true);
isMax = false;
isPress = false;
m_pTitleWidget = new QWidget();
}
CustomWidget::~CustomWidget()
{
}
void CustomWidget::setContentWidget(QWidget *contentWidget)
{
if (contentWidget)
{
m_pContentWidget = contentWidget;
m_pContentWidget->move(m_pTitleWidget->rect().bottomLeft());
m_pContentWidget->show();
resize(m_pTitleWidget->width(), m_pTitleWidget->height() + m_pContentWidget->height());
}
}
void CustomWidget::setTitleInfo(const QString &titleString, const QPixmap &windowIcon)
{
QLabel *logoLabel = new QLabel();
logoLabel->setPixmap(windowIcon);
logoLabel->setFixedSize(16, 16);
logoLabel->setScaledContents(true);
QLabel *titleLabel = new QLabel();
titleLabel->setText(titleString);
QFont titleFont = titleLabel->font();
titleFont.setBold(true);
titleLabel->setFont(titleFont);
titleLabel->setObjectName("whiteLabel");
titleLabel->setStyleSheet("color:white;");
QToolButton *minButton = new QToolButton();
connect(minButton, SIGNAL(clicked()), this, SLOT(slotShowSmall()));
m_pMaxButton = new QToolButton();
connect(m_pMaxButton, SIGNAL(clicked()), this, SLOT(slotShowMaxRestore()));
QToolButton *closeButton = new QToolButton();
connect(closeButton, SIGNAL(clicked()), this, SLOT(close()));
connect(this, SIGNAL(moveWindow()), this, SLOT(onMoveWindow()));
QHBoxLayout *titleLayout = new QHBoxLayout();
titleLayout->addWidget(logoLabel);
titleLayout->addWidget(titleLabel);
titleLabel->setContentsMargins(5, 0, 0, 0);
titleLayout->addStretch();
titleLayout->addWidget(minButton, 0, Qt::AlignTop);
titleLayout->addWidget(m_pMaxButton, 0, Qt::AlignTop);
titleLayout->addWidget(closeButton, 0, Qt::AlignTop);
titleLayout->setSpacing(0);
titleLayout->setContentsMargins(5, 0, 0, 0);
m_pTitleWidget->setFixedSize(600, TITLE_HEIGHT);
m_pTitleWidget->setLayout(titleLayout);
m_pTitleWidget->installEventFilter(0);
m_pTitleWidget->setAutoFillBackground(true);
QPalette palette;
palette.setColor(QPalette::Background, QColor(31,25,25));
m_pTitleWidget->setPalette(palette);
}
void CustomWidget::slotShowSmall()//最小化
{
this->showMinimized();
}
void CustomWidget::slotShowMaxRestore()//还原按钮
{
if (isMax) {
this->showNormal();
m_pMaxButton->setIcon(maxPixmap);
}
else {
this->showMaximized();
m_pMaxButton->setIcon(restorePixmap);
}
isMax = !isMax;
}
void CustomWidget::onMoveWindow()
{
//m_pContentWidget->move(m_pTitleWidget->rect().bottomLeft());
}
void CustomWidget::mousePressEvent(QMouseEvent *e)
{
startPos = e->globalPos();
clickPos = e->pos();
if (e->button() == Qt::LeftButton) {
if (e->type() == QEvent::MouseButtonPress) {
isPress = true;
}
else if (e->type() == QEvent::MouseButtonDblClick && e->pos().y() <= TITLE_HEIGHT) {
this->slotShowMaxRestore();
}
}
}
void CustomWidget::mouseMoveEvent(QMouseEvent *e)
{
if (isMax || !isPress) {
return;
}
this->move(e->globalPos() - clickPos);
emit moveWindow();
}
void CustomWidget::mouseReleaseEvent(QMouseEvent *)
{
isPress = false;
}
bool CustomWidget::nativeEvent(const QByteArray & eventType, void * message, long * result)//实现拖拽边框的效果
{
Q_UNUSED(eventType);
const int HIT_BORDER = 5;
const MSG *msg = static_cast<MSG*>(message);
if (msg->message == WM_NCHITTEST) {
int xPos = ((int)(short)LOWORD(msg->lParam)) - this->frameGeometry().x();
int yPos = ((int)(short)HIWORD(msg->lParam)) - this->frameGeometry().y();
if (this->childAt(xPos, yPos) == 0) {
*result = HTCAPTION;
}
else {
return false;
}
if (xPos > 0 && xPos < HIT_BORDER) {
*result = HTLEFT;
}
if (xPos >(this->width() - HIT_BORDER) && xPos < (this->width() - 0)) {
*result = HTRIGHT;
}
if (yPos > 0 && yPos < HIT_BORDER) {
*result = HTTOP;
}
if (yPos >(this->height() - HIT_BORDER) && yPos < (this->height() - 0)) {
*result = HTBOTTOM;
}
if (xPos > 0 && xPos < HIT_BORDER && yPos > 0 && yPos < HIT_BORDER) {
*result = HTTOPLEFT;
}
if (xPos >(this->width() - HIT_BORDER) && xPos < (this->width() - 0) && yPos > 0 && yPos < HIT_BORDER) {
*result = HTTOPRIGHT;
}
if (xPos > 0 && xPos < HIT_BORDER && yPos >(this->height() - HIT_BORDER) && yPos < (this->height() - 0)) {
*result = HTBOTTOMLEFT;
}
if (xPos >(this->width() - HIT_BORDER) && xPos < (this->width() - 0) && yPos >(this->height() - HIT_BORDER) && yPos < (this->height() - 0)) {
*result = HTBOTTOMRIGHT;
}
return true;
}
return false;
}
void CustomWidget::paintEvent(QPaintEvent *e)
{//绘制边框
int border = FRAME_BORDER;
if (this->isMaximized()) {
border = 0;
}
QPainter painter(this);
QPainterPath painterPath;
painterPath.setFillRule(Qt::WindingFill);
painterPath.addRect(border, border, this->width() - 2 * border, this->height() - 2 * border);
painter.setRenderHint(QPainter::Antialiasing, true);
painter.fillPath(painterPath, QBrush(Qt::black));
QColor color(200, 200, 200);
for (int i = 0; i<border; i++) {
color.setAlpha((i + 1) * 30);
painter.setPen(color);
painter.drawRect(border - i, border - i, this->width() - (border - i) * 2, this->height() - (border - i) * 2);
}
painter.setPen(Qt::NoPen);
painter.setBrush(Qt::black);
// 这里可以在资源中指定一张标题背景图片
//painter.drawPixmap(QRect(border, border, this->width()-2*border, this->height()-2*border), QPixmap(DEFAULT_SKIN));
painter.drawRect(QRect(border, TITLE_HEIGHT, this->width() - 2 * border, this->height() - TITLE_HEIGHT - border));
QFrame::paintEvent(e);
}
///////////////全文完
http://blog.csdn.net/u011915578/article/details/46924151
QT自定义窗口(模拟MainWindow)的更多相关文章
- QT自定义窗口
qt 中允许自定义窗口控件,使之满足特殊要求, (1)可以修改其显示,自行绘制 (2)可以动态显示 (3)可以添加事件,支持鼠标和键盘操作 自定义控件可以直接在QtDesigner里使用,可以直接加到 ...
- Qt自定义窗口部件
QtDesigner自定义窗口部件有两种方法:改进法(promotion)和插件法(plugin) 改进法 1.改进法之前,要先写好子类化QSpinBox后的HexspinBox.h和Hexs ...
- qt 自定义窗口绘制正弦曲线
circlewidget.h #ifndef CIRCLAWIDGET_H #define CIRCLAWIDGET_H #include <QFrame> #include<QVe ...
- qt 自定义窗口显示鼠标划过的轨迹
鼠标事件分为四种: 1.按下 2.抬起 3.移动 4.双击 鼠标事件继承与QWidget void mouseDoubleClickEvent(QMouseEvent *event) void mou ...
- QT笔记之自定义窗口拖拽移动
1.QT自定义标题栏,拖拽标题栏移动窗口(只能拖拽标题,其他位置无法拖拽) 方法一: 转载:http://blog.sina.com.cn/s/blog_4ba5b45e0102e83h.html . ...
- Qt入门(19)——自定义窗口部件
我们介绍可以画自己的第一个自定义窗口部件.我们也加入了一个有用的键盘接口.我们添加了一个槽:setRange(). void setRange( int minVal, int maxV ...
- WPF 使用 WindowChrome,在自定义窗口标题栏的同时最大程度保留原生窗口样式(类似 UWP/Chrome)
WPF 自定义窗口样式有多种方式,不过基本核心实现都是在修改 Win32 窗口样式.然而,Windows 上的应用就应该有 Windows 应用的样子嘛,在保证自定义的同时也能与其他窗口样式保持一致当 ...
- QT5每日一学(二)编写QT多窗口程序
一.添加主窗口 1.首先打开Qt Creator,新建Qt Widgets Application,项目名称设置为windows,在类信息界面保持基类为QMainWindow.类名为MainWindo ...
- WPF.UIShell UIFramework之自定义窗口的深度技术 - 模态闪动(Blink)、窗口四边拖拽支持(WmNCHitTest)、自定义最大化位置和大小(WmGetMinMaxInfo)
无论是在工作和学习中使用WPF时,我们通常都会接触到CustomControl,今天我们就CustomWindow之后的一些边角技术进行探讨和剖析. 窗口(对话框)模态闪动(Blink) 自定义窗口的 ...
随机推荐
- Pawn Brotherhood
Pawn Brotherhood 1 alpha_table = "abcdefgh" 2 3 def safe_pawns(pawns): 4 safe_count = 0 5 ...
- 使用WCF实现SOA面向服务编程—— 架构设计
原文地址:http://www.cnblogs.com/leslies2/archive/2011/03/29/1997889.html SOA本身就是一种面向企业级服务的系统架构,简单来说,SOA就 ...
- Jquery 概念性内容编辑器
概念性jQuery内容编辑器,这是一款非常有特色的jQuery编辑器,该编辑器支持文字.列表.视频.引用等功能,是一款小巧简洁,富有个性化的jQuery内容编辑器插件. 代码: <!doct ...
- [Oracle] 参数修改小结
v$parameter Oracle参数的修改比较复杂,有些参数是可以在session级别修改,有些则必须在system级别修改,有些参数修改后马上生效(不需要重启),有些参数则必须重启才能生效,那么 ...
- 【转】linux 关机命令总结
linux下常用的关机命令有:shutdown.halt.poweroff.init:重启命令有:reboot.下面本文就主要介绍一些常用的关机命令以及各种关机命令之间的区别和具体用法. 首先来看一下 ...
- NYOJ17,单调递增最长子序列
单调递增最长子序列 时间限制:3000 ms | 内存限制:65535 KB 难度:4 描写叙述 求一个字符串的最长递增子序列的长度 如:dabdbf最长递增子序列就是abdf.长度为4 输入 第 ...
- FileSystemWatcher使用方法具体解释
FileSystemWatcher控件主要功能: 监控指定文件或文件夹的文件的创建.删除.修改.重命名等活动.能够动态地定义须要监控的文件类型及文件属性修改的类型. 1.经常使用的几个基本属性: (1 ...
- chart.js制作折线图
<!DOCTYPE html> <html> <head> <title></title> </head> <script ...
- oracle函数Lpad与Rpad
函数介绍 lpad函数从左边对字符串使用指定的字符进行填充.从其字面意思也可以理解,l是left的简写,pad是填充的意思,所以lpad就是从左边填充的意思. 语法格式如下: lpad( string ...
- spring aop获取目标对象的方法对象(包括方法上的注解)
这两天在学习权限控制模块.以前看过传智播客黎活明老师的巴巴运动网视频教程,里面就讲到权限控制的解决方案,当时也只是看看视频,没有动手实践,虽说看过几遍,可是对于系统中的权限控制还是很迷茫,所以借着这次 ...