gpt4 book ai didi

c++ - QGraphicsItem 和 Tab 键顺序

转载 作者:太空宇宙 更新时间:2023-11-04 12:19:29 28 4
gpt4 key购买 nike

对于某些 GUI 应用程序,我使用带有不同控件的 QMainWindow:QGraphicsScene + QGraphicsView、QPushButtons、QWidget。在 QGraphicsScene 中有很多不同类型的项目:

QGraphicsPolygonItem
QGraphicsTextItem
QGraphicsRectItem

但对我来说最重要的是多边形项目,这个项目有那些标志:

setFlag(QGraphicsItem::ItemIsFocusable, true);
setFlag(QGraphicsItem::ItemIsMovable, true);
setFlag(QGraphicsItem::ItemIsSelectable, true);

我可以通过鼠标选择每个项目,但我需要通过 Tab 键更改这些项目的焦点。怎么可能?

SetTabOrder 仅适用于 QGraphicsObjects。我试图通过重新定义 QGraphicsView::focusNextPrevChild 来解决这个问题

bool MyGraphicsView::focusNextPrevChild( bool next )
{
QGraphicsPolygonItem *target;
QGraphicsPolygonItem *current;

if( scene()->focusItem() )
{
target = qgraphicsitem_cast<QGraphicsPolygonItem*>( scene()->focusItem() );

bool is_focus_next=false;
foreach( QGraphicsItem *item, scene()->items() )
{
current = qgraphicsitem_cast<QGraphicsPolygonItem*>( item );

// set focus for next before selected
if( current && is_focus_next )
{
item->setFocus( Qt::MouseFocusReason );
// item->setSelected( true );
is_focus_next = false;
break;
}

// searching for selected item
if( current && current == target )
{
is_focus_next = true;
}
}

}
}

但只有第一个 Tab 起作用,当我再次按下 Tab 时,焦点已移至 QGraphicsView 控件之外的其他 QWidget。请帮我解决 QGraphicsItem 的 Tab 顺序焦点问题。

谢谢

编辑:最终版本,感谢 Steffen。

bool MyGraphicsView::focusNextPrevChild( bool next )
{
QGraphicsPolygonItem *target;
QGraphicsPolygonItem *current;

if( scene()->focusItem() )
{
target = qgraphicsitem_cast<QGraphicsPolygonItem*>( scene()->focusItem() );

bool is_focus_next=false;
foreach( QGraphicsItem *item, scene()->items() )
{
current = qgraphicsitem_cast<QGraphicsPolygonItem*>( item );

// set focus for next before selected
if( current && is_focus_next )
{
item->setFocus( Qt::MouseFocusReason );
return true;
}

// searching for selected item
if( current && current == target )
{
is_focus_next = true;
}
}

}

return QGraphicsView::focusNextPrevChild(next);
}

最佳答案

您应该返回 true 以表明您确实找到了一个小部件。由于您目前根本没有 return 语句,因此行为未定义。您还可以添加一些逻辑以在最后一个元素上返回 false 以允许用户再次离开您的 QGraphicsView。另请参阅 QWidget::focusNextPrevChild() 的文档.

关于c++ - QGraphicsItem 和 Tab 键顺序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5910947/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com