您好,登錄后才能下訂單哦!
這篇文章主要介紹“Qt如何實(shí)現(xiàn)通用無邊框拖動(dòng)拉伸”,在日常操作中,相信很多人在Qt如何實(shí)現(xiàn)通用無邊框拖動(dòng)拉伸問題上存在疑惑,小編查閱了各式資料,整理出簡(jiǎn)單好用的操作方法,希望對(duì)大家解答”Qt如何實(shí)現(xiàn)通用無邊框拖動(dòng)拉伸”的疑惑有所幫助!接下來,請(qǐng)跟著小編一起來學(xué)習(xí)吧!
相信各位CS結(jié)構(gòu)開發(fā)的程序員,多多少少都遇到過需要美化界面的事情,一般都不會(huì)采用系統(tǒng)的標(biāo)題欄,這樣就需要無邊框標(biāo)題欄窗體,默認(rèn)的話無邊框的標(biāo)題欄都不支持拉伸和拖動(dòng)的,畢竟去掉了標(biāo)題欄則意味著失去了系統(tǒng)的窗體的屬性,拉伸和拖動(dòng)都需要自己寫代碼去實(shí)現(xiàn),網(wǎng)上有很多類似的開源的方案,我也看過不少,總體來說復(fù)雜了些,對(duì)于初學(xué)者來說有可能看的云里霧里的,比如邊框四周八個(gè)方位都可以自由拉伸這塊,我的思路是針對(duì)設(shè)定的八個(gè)方位的區(qū)域進(jìn)行識(shí)別鼠標(biāo)是否按下,按下的哪個(gè)部位則執(zhí)行什么拉伸策略,鼠標(biāo)移到哪個(gè)位置則對(duì)應(yīng)改變鼠標(biāo)指針形狀,更淺顯易懂一些,至于拖動(dòng)移動(dòng),還可以設(shè)置拖動(dòng)的標(biāo)題欄的高度等。
主要功能:
可以指定需要無邊框的widget
邊框四周八個(gè)方位都可以自由拉伸
可設(shè)置對(duì)應(yīng)位置的邊距,以便識(shí)別更大區(qū)域
可設(shè)置是否允許拖動(dòng)
可設(shè)置是否允許拉伸
bool FramelessWidget::eventFilter(QObject *watched, QEvent *event) { if (widget != 0 && watched == widget) { if (event->type() == QEvent::Resize) { //重新計(jì)算八個(gè)描點(diǎn)的區(qū)域,描點(diǎn)區(qū)域的作用還有就是計(jì)算鼠標(biāo)坐標(biāo)是否在某一個(gè)區(qū)域內(nèi) int width = widget->width(); int height = widget->height(); //左側(cè)描點(diǎn)區(qū)域 rectLeft = QRect(0, padding, padding, height - padding * 2); //上側(cè)描點(diǎn)區(qū)域 rectTop = QRect(padding, 0, width - padding * 2, padding); //右側(cè)描點(diǎn)區(qū)域 rectRight = QRect(width - padding, padding, padding, height - padding * 2); //下側(cè)描點(diǎn)區(qū)域 rectBottom = QRect(padding, height - padding, width - padding * 2, padding); //左上角描點(diǎn)區(qū)域 rectLeftTop = QRect(0, 0, padding, padding); //右上角描點(diǎn)區(qū)域 rectRightTop = QRect(width - padding, 0, padding, padding); //左下角描點(diǎn)區(qū)域 rectLeftBottom = QRect(0, height - padding, padding, padding); //右下角描點(diǎn)區(qū)域 rectRightBottom = QRect(width - padding, height - padding, padding, padding); } else if (event->type() == QEvent::HoverMove) { //設(shè)置對(duì)應(yīng)鼠標(biāo)形狀,這個(gè)必須放在這里而不是下面,因?yàn)榭梢栽谑髽?biāo)沒有按下的時(shí)候識(shí)別 QHoverEvent *hoverEvent = (QHoverEvent *)event; QPoint point = hoverEvent->pos(); if (resizeEnable) { if (rectLeft.contains(point)) { widget->setCursor(Qt::SizeHorCursor); } else if (rectRight.contains(point)) { widget->setCursor(Qt::SizeHorCursor); } else if (rectTop.contains(point)) { widget->setCursor(Qt::SizeVerCursor); } else if (rectBottom.contains(point)) { widget->setCursor(Qt::SizeVerCursor); } else if (rectLeftTop.contains(point)) { widget->setCursor(Qt::SizeFDiagCursor); } else if (rectRightTop.contains(point)) { widget->setCursor(Qt::SizeBDiagCursor); } else if (rectLeftBottom.contains(point)) { widget->setCursor(Qt::SizeBDiagCursor); } else if (rectRightBottom.contains(point)) { widget->setCursor(Qt::SizeFDiagCursor); } else { widget->setCursor(Qt::ArrowCursor); } } //根據(jù)當(dāng)前鼠標(biāo)位置,計(jì)算XY軸移動(dòng)了多少 int offsetX = point.x() - lastPos.x(); int offsetY = point.y() - lastPos.y(); //根據(jù)按下處的位置判斷是否是移動(dòng)控件還是拉伸控件 if (moveEnable) { if (pressed) { widget->move(widget->x() + offsetX, widget->y() + offsetY); } } if (resizeEnable) { if (pressedLeft) { int resizeW = widget->width() - offsetX; if (widget->minimumWidth() <= resizeW) { widget->setGeometry(widget->x() + offsetX, rectY, resizeW, rectH); } } else if (pressedRight) { widget->setGeometry(rectX, rectY, rectW + offsetX, rectH); } else if (pressedTop) { int resizeH = widget->height() - offsetY; if (widget->minimumHeight() <= resizeH) { widget->setGeometry(rectX, widget->y() + offsetY, rectW, resizeH); } } else if (pressedBottom) { widget->setGeometry(rectX, rectY, rectW, rectH + offsetY); } else if (pressedLeftTop) { int resizeW = widget->width() - offsetX; int resizeH = widget->height() - offsetY; if (widget->minimumWidth() <= resizeW) { widget->setGeometry(widget->x() + offsetX, widget->y(), resizeW, resizeH); } if (widget->minimumHeight() <= resizeH) { widget->setGeometry(widget->x(), widget->y() + offsetY, resizeW, resizeH); } } else if (pressedRightTop) { int resizeW = rectW + offsetX; int resizeH = widget->height() - offsetY; if (widget->minimumHeight() <= resizeH) { widget->setGeometry(widget->x(), widget->y() + offsetY, resizeW, resizeH); } } else if (pressedLeftBottom) { int resizeW = widget->width() - offsetX; int resizeH = rectH + offsetY; if (widget->minimumWidth() <= resizeW) { widget->setGeometry(widget->x() + offsetX, widget->y(), resizeW, resizeH); } if (widget->minimumHeight() <= resizeH) { widget->setGeometry(widget->x(), widget->y(), resizeW, resizeH); } } else if (pressedRightBottom) { int resizeW = rectW + offsetX; int resizeH = rectH + offsetY; widget->setGeometry(widget->x(), widget->y(), resizeW, resizeH); } } } else if (event->type() == QEvent::MouseButtonPress) { //記住當(dāng)前控件坐標(biāo)和寬高以及鼠標(biāo)按下的坐標(biāo) QMouseEvent *mouseEvent = (QMouseEvent *)event; rectX = widget->x(); rectY = widget->y(); rectW = widget->width(); rectH = widget->height(); lastPos = mouseEvent->pos(); //判斷按下的手柄的區(qū)域位置 if (rectLeft.contains(lastPos)) { pressedLeft = true; } else if (rectRight.contains(lastPos)) { pressedRight = true; } else if (rectTop.contains(lastPos)) { pressedTop = true; } else if (rectBottom.contains(lastPos)) { pressedBottom = true; } else if (rectLeftTop.contains(lastPos)) { pressedLeftTop = true; } else if (rectRightTop.contains(lastPos)) { pressedRightTop = true; } else if (rectLeftBottom.contains(lastPos)) { pressedLeftBottom = true; } else if (rectRightBottom.contains(lastPos)) { pressedRightBottom = true; } else { pressed = true; } } else if (event->type() == QEvent::MouseMove) { //改成用HoverMove識(shí)別 } else if (event->type() == QEvent::MouseButtonRelease) { //恢復(fù)所有 pressed = false; pressedLeft = false; pressedRight = false; pressedTop = false; pressedBottom = false; pressedLeftTop = false; pressedRightTop = false; pressedLeftBottom = false; pressedRightBottom = false; widget->setCursor(Qt::ArrowCursor); } } return QObject::eventFilter(watched, event); }
到此,關(guān)于“Qt如何實(shí)現(xiàn)通用無邊框拖動(dòng)拉伸”的學(xué)習(xí)就結(jié)束了,希望能夠解決大家的疑惑。理論與實(shí)踐的搭配能更好的幫助大家學(xué)習(xí),快去試試吧!若想繼續(xù)學(xué)習(xí)更多相關(guān)知識(shí),請(qǐng)繼續(xù)關(guān)注億速云網(wǎng)站,小編會(huì)繼續(xù)努力為大家?guī)砀鄬?shí)用的文章!
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。