pbootcms网站模板|日韩1区2区|织梦模板||网站源码|日韩1区2区|jquery建站特效-html5模板网

Qt - 確定絕對小部件和光標位置

Qt - Determine absolute widget and cursor position(Qt - 確定絕對小部件和光標位置)
本文介紹了Qt - 確定絕對小部件和光標位置的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學習吧!

問題描述

我有一個包含多個孩子的 QWidget.最終目標是能夠從一個小部件拖放到另一個小部件,在小部件之間移動一些東西.我有一個信號被觸發到我的父小部件的控制器,并且可以確定拖動何時正確開始和結束.我目前的問題是確定鼠標是否在鼠標向上時的目標小部件上方.

I have a QWidget that contains multiple children. The ultimate goal is to be able to drag and drop from one widget to the other, moving something between widgets. I've got a signal being fired to my parent widget's controller and can determine when the drag is starting and ending correctly. My current problem is determining whether or not the mouse is above the target widget on mouse up.

當我在文檔中看到 underMouse 時我很興奮,但它沒有' 在拖放事件期間不起作用(當我測試時,它似乎返回了不正確的值).如果做不到這一點,我的目標是找到包含目標小部件的矩形,并找到它是否包含鼠標向上時的鼠標坐標.我不能簡單地使用 contentsRect,因為它返回相對于它的小部件的位置正在被調用.我認為 mapToGlobal 會給我絕對的屏幕像素值,但它一直失敗好.我嘗試在父小部件的窗口上調用 mapTo,但這似乎也失敗了.

I got excited when I saw underMouse in the docs, but it doesn't work during drag and drop events (when I tested, it seemed to return incorrect values). Failing this, my goal was to find the rectangle containing the target widget and find whether it contained the mouse coordinates on a mouse up. I can't simply use contentsRect, since it returns positions relative to the widget it is being called on. I thought that mapToGlobal would give me absolute screen pixel values, but it keeps failing as well. I tried calling mapTo on the parent widget's window, but that also seemed to fail.

下面的代碼顯示了我使用各種方法獲得的各種 QRect 和 QPoint.也許其中一個有一個簡單的錯誤,所以我已經提供了所有.

Below is code showing the various QRects and QPoints I've gotten with the various methods. Maybe there's a simple error with one of them, so I've provided them all.

QRect relativeWidgetRect = targetWidget->contentsRect();
QRect *absoluteWidgetRect = new QRect(QWidget::mapToGlobal(relativeWidgetRect.topLeft()), QWidget::mapToGlobal(relativeWidgetRect.bottomRight()));
QRect *widgetRect = new QRect(mapTo(window(), relativeWidgetRect.topLeft()), mapTo(window(), relativeWidgetRect.bottomRight()));
QPoint relativeMousePos = QCursor::pos();
QPoint absoluteMousePos = QWidget::mapToGlobal(relativeMousePos);
QPoint widgetMousePos = mapTo(window(), relativeMousePos);

mapToParent 不適用于我的目的,因為目標小部件是實際上由頂級父級的子級撫養.

mapToParent won't work for my purposes, since the target widget is actually parented by a child of the top-level parent.

更新 這是最終得到解決的代碼.在我的頂級小部件(它是源和目標小部件的祖先)中,我添加了以下內容:

Update Here's the code that ended up working out. In my top-level widget (that was an ancestor to both the source and target widgets), I added this:

QRect widgetRect = targetWidget->Geometry();
QPoint mousePos = targetWidget->mapFromGlobal(QCursor::pos());
if(widgetRect.contains(mousePos))
{
// Logic
}

推薦答案

正如已經說過的,你應該使用 targetWidget->geometry() 而不是 contentsRect()這個特例.

As already said you should rather use targetWidget->geometry() instead of contentsRect() in this special case.

接下來我想知道您發布的代碼屬于哪個類.方法 QWidget::mapToGlobal() 應該從您的坐標相對于的 QWidget 調用.如果我猜對了,它應該看起來像這樣:

Next I wonder which class the code you posted belongs to. The method QWidget::mapToGlobal() should be invoked from the QWidget your coordinates are relative to. If I got you right, it should look like something like this:

QRect widgetRect = targetWidget->geometry();
widgetRect.moveTopLeft(targetWidget->parentWidget()->mapToGlobal(widgetRect.topLeft()));

然后注意 QCursor::pos() 已經返回全局屏幕坐標,所以這里不需要映射任何東西:

Then note QCursor::pos() is already returning global screen coordinates, so no need to map anything here:

if (widgetRect.contains(QCursor::pos())) {
    /* swap widgets */
}

最好不要將矩形映射到全局,而是將全局光標位置映射到小部件:

Probably it's even better to not map the rect to global, but to map the global cursor position to the widget:

if (targetWidget->rect().contains(targetWidget->mapFromGlobal(QCursor::pos()))) {
    /* do stuff */
}

這篇關于Qt - 確定絕對小部件和光標位置的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!

【網站聲明】本站部分內容來源于互聯網,旨在幫助大家更快的解決問題,如果有圖片或者內容侵犯了您的權益,請聯系我們刪除處理,感謝您的支持!

相關文檔推薦

How can I read and manipulate CSV file data in C++?(如何在 C++ 中讀取和操作 CSV 文件數據?)
In C++ why can#39;t I write a for() loop like this: for( int i = 1, double i2 = 0; (在 C++ 中,為什么我不能像這樣編寫 for() 循環: for( int i = 1, double i2 = 0;)
How does OpenMP handle nested loops?(OpenMP 如何處理嵌套循環?)
Reusing thread in loop c++(在循環 C++ 中重用線程)
Precise thread sleep needed. Max 1ms error(需要精確的線程睡眠.最大 1ms 誤差)
Is there ever a need for a quot;do {...} while ( )quot; loop?(是否需要“do {...} while ()?環形?)
主站蜘蛛池模板: 影像测量仪_三坐标测量机_一键式二次元_全自动影像测量仪-广东妙机精密科技股份有限公司 | 自动部分收集器,进口无油隔膜真空泵,SPME固相微萃取头-上海楚定分析仪器有限公司 | 杭州画室_十大画室_白墙画室_杭州美术培训_国美附中培训_附中考前培训_升学率高的画室_美术中考集训美术高考集训基地 | 防水套管_柔性防水套管_刚性防水套管-巩义市润达管道设备制造有限公司 | 户外健身路径_小区健身器材_室外健身器材厂家_价格-浩然体育 | 运动木地板_体育木地板_篮球馆木地板_舞台木地板-实木运动地板厂家 | 螺钉式热电偶_便携式温度传感器_压簧式热电偶|无锡联泰仪表有限公司|首页 | 博医通医疗器械互联网供应链服务平台_博医通 | 南汇8424西瓜_南汇玉菇甜瓜-南汇水蜜桃价格 | 骨密度仪-骨密度测定仪-超声骨密度仪-骨龄测定仪-天津开发区圣鸿医疗器械有限公司 | 皮带式输送机械|链板式输送机|不锈钢输送机|网带输送机械设备——青岛鸿儒机械有限公司 | 双效节能浓缩器-热回流提取浓缩机组-温州市利宏机械 | 派财经_聚焦数字经济内容服务平台 | 铝扣板-铝方通-铝格栅-铝条扣板-铝单板幕墙-佳得利吊顶天花厂家 elisa试剂盒价格-酶联免疫试剂盒-猪elisa试剂盒-上海恒远生物科技有限公司 | 浙江红酒库-冰雕库-气调库-茶叶库安装-医药疫苗冷库-食品物流恒温恒湿车间-杭州领顺实业有限公司 | 顺景erp系统_erp软件_erp软件系统_企业erp管理系统-广东顺景软件科技有限公司 | 塑胶地板-商用PVC地板-pvc地板革-安耐宝pvc塑胶地板厂家 | lcd条形屏-液晶长条屏-户外广告屏-条形智能显示屏-深圳市条形智能电子有限公司 | 仪器仪表网 - 永久免费的b2b电子商务平台| 艺术生文化课培训|艺术生文化课辅导冲刺-济南启迪学校 | 美缝剂_美缝剂厂家_美缝剂加盟-地老板高端瓷砖美缝剂 | 小型铜米机-干式铜米机-杂线全自动铜米机-河南鑫世昌机械制造有限公司 | 广东成考网-广东成人高考网 | Honsberg流量计-Greisinger真空表-气压计-上海欧臻机电设备有限公司 | 云阳人才网_云阳招聘网_云阳人才市场_云阳人事人才网_云阳人家招聘网_云阳最新招聘信息 | 超声骨密度仪,双能X射线骨密度仪【起草单位】,骨密度检测仪厂家 - 品源医疗(江苏)有限公司 | 分光色差仪,测色仪,反透射灯箱,爱色丽分光光度仪,美能达色差仪维修_苏州欣美和仪器有限公司 | 无负压供水设备,消防稳压供水设备-淄博创辉供水设备有限公司 | 电动高尔夫球车|电动观光车|电动巡逻车|电动越野车厂家-绿友机械集团股份有限公司 | 在线PH计-氧化锆分析仪-在线浊度仪-在线溶氧仪- 无锡朝达 | 天津次氯酸钠酸钙溶液-天津氢氧化钠厂家-天津市辅仁化工有限公司 | 珠光砂保温板-一体化保温板-有釉面发泡陶瓷保温板-杭州一体化建筑材料 | 莱州网络公司|莱州网站建设|莱州网站优化|莱州阿里巴巴-莱州唯佳网络科技有限公司 | 大倾角皮带机-皮带输送机-螺旋输送机-矿用皮带输送机价格厂家-河南坤威机械 | 智能监控-安防监控-监控系统安装-弱电工程公司_成都万全电子 | 齿轮减速电机一体机_蜗轮蜗杆减速马达-德国BOSERL齿轮减速机带电机生产厂家 | 水厂自动化-水厂控制系统-泵站自动化|控制系统-闸门自动化控制-济南华通中控科技有限公司 | 浙江浩盛阀门有限公司| 医学模型生产厂家-显微手术模拟训练器-仿真手术模拟训练系统-北京医教科技 | 塑料薄膜_PP薄膜_聚乙烯薄膜-常州市鑫美新材料包装厂 | 西安文都考研官网_西安考研辅导班_考研培训机构_西安在职考研培训 |