[英]How drag and drop qwidget consisting of several widgets?
我有幾個QWidget,比如說PreviewWidget ,每個QWidget都由2個QLabel組成(可能比QLabel還要多)。 我想在主窗口中拖放PreviewWidget 。
問題:我可以通過在綠色區域(PreviewWidget區域)上按下鼠標來移動窗口小部件。 但是,如果我嘗試通過單擊標簽之一來拖動小部件,則該標簽會移出PreviewWidget (有時我什至不知道會發生什么)。 我想要的是移動整個PreviewWidget,或者至少在鼠標按下其子級時什么也不發生。
我的方法。 我重載了mousePressEvent() ,如下所示:
void MainWindow::mousePressEvent(QMouseEvent *event)
{
// I beleive my problem is right here...
PreviewWidget *child = static_cast<PreviewWidget*>(this->childAt(event->pos()));
if (!child)
return; // this is not returned even if the child is not of a PreviewWidget type
// Create QDrag object ...
}
如何以我想要的方式拖放PreviewWidget? 任何示例都被贊賞。
我建議一種在光標坐標處識別孩子的策略。
在你的mousePressEvent
:
//...
QWidget * child = childAt(e->pos());
if(child != 0)
{
QString classname(child->metaObject()->className());
if( classname == "QLabel")
{
child = child->parentWidget();
if(child != 0)
{
classname = child->metaObject()->className();
}
}
if(classname == "PreviewWidget")
{
//do whatever with child ...
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.