• 8508阅读
  • 13回复

QGraphicsScene QGraphicsProxyWidge 窗口件焦点切换问题 [复制链接]

上一主题 下一主题
离线hends2012
 

只看楼主 倒序阅读 楼主  发表于: 2011-10-09
在使用QGraphicsScene 架构显示窗口按钮,按钮左右移动时显示按钮对应的窗口,使用代理窗口QGraphicsProxyWidge 将写好的QWidge添加到
Scene 中要实现左右键切换显示的窗口内容,按确认键进入相应的窗口,使窗口获取焦点,但是现在使用代理这种方式,窗口的焦点无法获取,只能左右切换显示的窗口,求解
离线wxj120bw

只看该作者 1楼 发表于: 2011-10-10
回 楼主(hends2012) 的帖子
建议还是贴些相关的代码
离线hends2012

只看该作者 2楼 发表于: 2011-10-13
回 1楼(wxj120bw) 的帖子
工程稍微有点大,如果你有兴趣,可以给你发文件看,呵呵。我是新手~
离线hends2012

只看该作者 3楼 发表于: 2011-10-13
效果图
效果图

离线wxj120bw

只看该作者 4楼 发表于: 2011-10-13
回 2楼(hends2012) 的帖子
看效果 不错 如果不方便 可以给QGraphicsProxyWidge相关的代码
离线hends2012

只看该作者 5楼 发表于: 2011-10-14
回 4楼(wxj120bw) 的帖子
哎,我老大说还没彻底解决。。。。。。
离线hends2012

只看该作者 6楼 发表于: 2011-10-14
  1. 按键事件:
  2. void ParallaxHome::keyPressEvent(QKeyEvent *event)
  3. {
  4.     if (event->key() == Qt::Key_Right)
  5.     {
  6.         m_naviBar->setPageOffset(+1);
  7.         printf("the right offset is %d\n",offset);
  8.         m_scene.addItem(Proxy_items[offset]);
  9.         Proxy_items[offset]->setPos(330,100);
  10.         Proxy_items[offset]->setZValue(2);
  11.         Proxy_items[offset-1]->close();
  12.         ++offset;
  13.     }
  14.     else if (event->key() == Qt::Key_Left)
  15.     {
  16.         m_naviBar->setPageOffset(-1);
  17.         m_scene.addItem(Proxy_items[offset]);
  18.         Proxy_items[offset]->setPos(330,100);
  19.         Proxy_items[offset]->setZValue(2);
  20.         Proxy_items[offset+1]->close();
  21.         --offset;
  22.     }
  23.     else if((event->key() == Qt::Key_Space)
  24.     {
  25.             Proxy_items[offset]->setFocus();
  26.         }
  27. }
  28. 构造函数:
  29. ParallaxHome::ParallaxHome():QGraphicsView(),m_pageOffset(0)
  30. {
  31.     setRenderHint(QPainter::Antialiasing, true);
  32.     setBackgroundBrush(QPixmap(":/images/bottom.png"));
  33.     setupScene();
  34.     setScene(&m_scene);
  35.     mytimeid=startTimer(1000);
  36.     setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);//去掉滑动条
  37.     setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
  38.     setFrameShape(QFrame::NoFrame);
  39.     setWindowTitle("Parallax Home");
  40.     num=0;
  41.     offset=1;
  42. }
  43. 另外:我是新手,呵呵


离线wxj120bw

只看该作者 7楼 发表于: 2011-10-14
回 6楼(hends2012) 的帖子
新手能做出这样的界面 已经很不错了 现在没时间分析代码 只能等晚上了
离线hends2012

只看该作者 8楼 发表于: 2011-10-15
回 7楼(wxj120bw) 的帖子
恩,谢谢关注~
离线wxj120bw

只看该作者 9楼 发表于: 2011-10-15
回 8楼(hends2012) 的帖子
现在你窗口获取焦点是通过空格键来获得吗?
离线hends2012

只看该作者 10楼 发表于: 2011-10-17
是想设置为空格获取焦点,但是就是达不到效果,请问一下让代理窗口获取焦点是不是这种方法,还是别的方法,setFocus()
离线hends2012

只看该作者 11楼 发表于: 2011-10-17
回 9楼(wxj120bw) 的帖子
你如果方便的话,加我QQ:524028457,谢谢继续关注     是想设置为空格获取焦点,但是就是达不到效果,请问一下让代理窗口获取焦点是不是这种方法,还是别的方法,setFocus()
离线wxj120bw

只看该作者 12楼 发表于: 2011-10-17
回 11楼(hends2012) 的帖子
QGraphicsProxyWidget embeds QWidget-based widgets, for example, a QPushButton, QFontComboBox, or even QFileDialog, into QGraphicsScene. It forwards events between the two objects and translates between QWidget's integer-based geometry and QGraphicsWidget's qreal-based geometry. QGraphicsProxyWidget supports all core features of QWidget, including tab focus, keyboard input, Drag & Drop, and popups. You can also embed complex widgets, e.g., widgets with subwidgets.
上面是摘自qt手册里 说了QGraphicsProxyWidget 负责将事件在widget和scene之间进行传递,而且支持qwidget的所有特性。所以用setFocus()应该没错。可以试着用qapplication::focusWidget ()推测下谁当前拥有焦点
PS:白天忙 没时间回你
离线hends2012

只看该作者 13楼 发表于: 2011-10-18
void ParallaxHome::keyPressEvent(QKeyEvent *event)
{
    if(model == 0)
    {
        if(event->key() == Qt::Key_A)
        {
            Proxy_items[0]->clearFocus();
            Proxy_items[0]->setFocus();
            //m_d_tv->clearFocus();
            //m_d_tv->setFocus();
            QGraphicsView::keyPressEvent(event);
            model = 1;
        }

        else if(event->key() == Qt::Key_Right)
        {
            m_naviBar->setPageOffset(+1);
            m_scene.addItem(Proxy_items[offset]);
            Proxy_items[offset]->setPos(330,100);
            Proxy_items[offset]->setZValue(2);
            Proxy_items[offset-1]->close();
            ++offset;
        }

        else if(event->key() == Qt::Key_Left)
        {
            m_naviBar->setPageOffset(-1);
            m_scene.addItem(Proxy_items[offset]);
            Proxy_items[offset]->setPos(330,100);
            Proxy_items[offset]->setZValue(2);
            Proxy_items[offset+1]->close();
            --offset;
        }
    }

    else if(model == 1)
    {
        if((event->key() == Qt::Key_A)||(event->key() == Qt::Key_Up)||(event->key() == Qt::Key_Down)||(event->key() == Qt::Key_Left)||(event->key() == Qt::Key_Right))
        {
            Proxy_items[0]->clearFocus();
            Proxy_items[0]->setFocus();
            //m_d_tv->clearFocus();
            //m_d_tv->setFocus();
            QGraphicsView::keyPressEvent(event);
            model = 0;
            return;
        }
    }
}



void d_tv::keyPressEvent(QKeyEvent *event)
{
    if(tv_model == 0)
    {
        if(event->key() == Qt::Key_A)
        {
            focusNextPrevChild(true);
            tv_model = 1;
        }
    }

    else if(tv_model == 1)
    {
        if(event->key() == Qt::Key_A)
        {    
            focusNextPrevChild(true);
            focusNextPrevChild(true);
            tv_model = 0;
        }
    }
}

现在获取焦点有点问题,现在用A键实现窗口内实现控件焦点切换,用//m_d_tv->clearFocus();
            //m_d_tv->setFocus();这种方式可以实现切换功能,但是不便于窗口的管理;Proxy_items[0]->clearFocus();
            Proxy_items[0]->setFocus();这种事件传递不到窗口中,焦点就切换不了,请问有没有好的解决办法
快速回复
限100 字节
 
上一个 下一个