对于某些GUI应用程序,我使用带有不同控件的QMainWindow:QGraphicsScene + QGraphicsView,QPushButtons,QWidget。 里面QGraphicsScene位于很多不同的项目类型:QGraphicsItem和Tab顺序
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键再次重点已经转移到其他的QWidget的的QGraphicsView控制之外。 请问,您可以帮助我与Tab顺序焦点为QGraphicsItem。
谢谢
编辑:最终版本,这要归功于斯特芬。
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或false)有很大的影响。 – Dmitry 2011-05-07 02:57:36