自定義矩形可拖動及寬度大小的改變

2021-09-24 18:30:16 字數 2886 閱讀 2837

//標頭檔案

#pragma once

#include

#include "ui_dlg_daytime.h"

#include

#include

#include

#include

#define padding 6

enum direction ;

class dlg_daytime : public qwidget

;//原始檔

#include "dlg_daytime.h"

#include

#include

#include

dlg_daytime::dlg_daytime(qwidget *parent)

: qwidget(parent), m_ispressed(false), m_nindex(-1), m_movepos(0, 0)

,_startx(0)

void dlg_daytime::region(const qpoint &cursorglobalpoint)

}if (nindex == -1)

// 獲取窗體在螢幕上的位置區域,tl為topleft點,rb為rightbottom點

qrect rect = m_map[nindex];

qpoint tl = rect.topleft();//maptoglobal(rect.topleft());

qpoint rb = rect.bottomright();//maptoglobal(rect.bottomright());

int x = cursorglobalpoint.x();

int y = cursorglobalpoint.y();

qpoint templefttoppoint = qpoint(tl.x() - padding, 0);

qpoint temprightbottompoint = qpoint(tl.x(), size().height());

qrect leftrec = qrect(templefttoppoint, temprightbottompoint);

qpoint temprightpoint = qpoint(rb.x() - padding, 0);

qrect rightrec = qrect(temprightpoint, rb);

if (leftrec.contains(cursorglobalpoint))

else if (rightrec.contains(cursorglobalpoint))

//if (x <= tl.x() + padding && x >= tl.x())

//else if (x <= rb.x() && x >= rb.x() - padding)

else

}dlg_daytime::~dlg_daytime()

void dlg_daytime::paintevent(qpaintevent *event)

//當前畫的移動的資料

if (m_curstartpos.x() > 0)

int w = x2 - x;

int h = this->size().height();

painter.drawrect(x, 0, w, h);

}qwidget::paintevent(event);

}void dlg_daytime::mousepressevent(qmouseevent *event)

}if (isonrect)

else

m_ispressed = true;

}void dlg_daytime::mousereleaseevent(qmouseevent *event)

m_ispressed = false;

if (m_nindex != -1)

m_curendpos = event->pos();

qrect rec;

rec.setx(m_curstartpos.x());

rec.sety(0);

rec.setwidth(m_curendpos.x() - m_curstartpos.x());

rec.setheight(size().height());

static int ncount = 0;

m_map.insert(ncount++, rec);

m_curstartpos = qpoint(0, 0);

m_curendpos = qpoint(0, 0);

m_movepos = qpoint(0, 0);

}void dlg_daytime::mousemoveevent(qmouseevent *event)

if (m_nindex != -1 && m_ispressed)  //在矩形中

/*if (rb.x() - mousepos.x() <= nwidth)

else

*/break;

case right:

- tl.x());

break;

default:

break;

}m_map[m_nindex] = rmove;

}else

m_curstartpos = qpoint(0, 0);

m_curendpos = qpoint(0, 0);

m_movepos = qpoint(0, 0);

}else if (m_ispressed)  //不在矩形中

this->update();

}效果如下所示:藍色的寬度可變化

可自由拖動的自定義元件

通過監聽元件touch事件,來改變元件的left和top的樣式 1.首先,touchstart時,拿到元件初始狀態時的位置 2.touchmove,實時拿到元件的位置,並修改元件的left和top的值 3.如果要求吸附到旁邊,在touchend時,看最後元件的位置是偏向於左邊還是右邊,如果偏向於左邊...

c 自定義可拖動變形控制項

public class 控制項移動變形類 usercontrol 控制項類 rectangle 傳遞控制項 傳遞控制項相對於本控制項的範圍 rectangle 本控制項 本控制項相對於自己的範圍 rectangle 調節點邊框 new rectangle 8 8個點相對於本控制項的範圍 recta...

QT 自定義邊框,可伸縮,延伸, 拖動

有時為了美觀,會自己製作邊框,這就意味著,你完成上述功能,要實現三個功能 1 窗體可自由 伸縮.可大可小.2 窗體可隨著滑鼠移動 3 自己實現標題欄 包含 最小化,最大化,關閉 那麼就按照上述的順序來講解 1 有時你會發現,當滑鼠放在邊框邊時 滑鼠會有變化,箭頭向上,向下,向左,向右,左上.思路 計...