找回密码
 立即注册
收起左侧

qt 实现tv 界面;如何接收来自遥控器的按键信息?

6
回复
9738
查看
[复制链接]
累计签到:2 天
连续签到:1 天
来源: 2017-4-10 17:30:39 显示全部楼层 |阅读模式
1Qter豆
如题:最近在做一个项目,用qt实现电视机的控制界面类似于机顶盒,在接收来自遥控器按键信息时,不知道如何将按键转化为qt能够识别的事件类似于鼠标或者键盘?

例如:
遥控器上按下 up按键,qt识别后,界面同时更新。


回复

使用道具 举报

累计签到:2 天
连续签到:1 天
2017-4-12 19:13:33 显示全部楼层
alex_wang 发表于 2017-4-11 09:00
不是遥控器和设备之间的通信问题,而是 设备已经接收到了遥控器的按键信息,
qt 如何处理此按键信息,像处 ...

我的解决方案:
创建一个c++类:模拟键盘按键类,功能发送键盘事件到指定的UI对象如图:
通过点击UI上的两个按钮:‘UP’   'DOWN'
可以实现键盘‘up’ 'down'的功能:使焦点上下移动
模拟按键类如下:
CKeyEvent.h
  1. class CKeyEvent : public QObject
  2. {
  3.     Q_OBJECT

  4.     Q_ENUMS(CKEY_ID)

  5. public:
  6.     enum CKEY_ID
  7.     {
  8.         CKEY_UP = Qt::Key_Up,
  9.         CKEY_DOWN= Qt::Key_Down,
  10.         CKEY_MAX
  11.     };
  12.     CKeyEvent(QObject *parent = NULL);
  13.     ~CKeyEvent();

  14.     Q_INVOKABLE void sendCkeyPressEvent(QObject* receiver,Qt::Key CkeyId);
  15.     Q_INVOKABLE void sendCkeyReleaseEvent(QObject* receiver,Qt::Key CkeyId);

  16. private:
  17.     QObject* m_receiver;
  18. };
复制代码
类的实现:
CKeyEvent.cpp
  1. CKeyEvent::CKeyEvent(QObject *parent):QObject(parent)
  2. {

  3. }
  4. CKeyEvent::~CKeyEvent()
  5. {

  6. }

  7. void CKeyEvent::sendCkeyPressEvent(QObject* receiver,Qt::Key CkeyId)
  8. {
  9.     QKeyEvent keyPressEvent(QEvent::KeyPress, CkeyId, Qt::NoModifier);
  10.     QGuiApplication::sendEvent(receiver, &keyPressEvent);
  11. }
  12. void CKeyEvent::sendCkeyReleaseEvent(QObject* receiver,Qt::Key CkeyId)
  13. {
  14.     QKeyEvent keyReleaseEvent(QEvent::KeyRelease, CkeyId, Qt::NoModifier);
  15.     QGuiApplication::sendEvent(receiver, &keyReleaseEvent);
  16. }
复制代码
主函数:main.cpp
  1. int main(int argc, char *argv[])
  2. {
  3.     QGuiApplication app(argc, argv);


  4.     //注册C++类到QML
  5.     //qmlRegisterType<CKeyEvent>("CKeyEvent", 1, 0, "CKeyEvent");

  6.     QQmlApplicationEngine engine;

  7.     CKeyEvent *Ckey = new CKeyEvent();
  8.     engine.rootContext()->setContextProperty("cKeyCDP",Ckey);
  9.     engine.load(QUrl(QStringLiteral("qrc:/main.qml")));

  10.     return app.exec();
  11. }
复制代码
QML:main.qml
  1. Window {
  2.     id : mainWindows
  3.     visible: true
  4.     width: 1024
  5.     height: 768
  6.     title: qsTr("Hello World")

  7.     GridMenu {
  8.         id: gridMenu
  9.         y: 320; width: parent.width; height: 320
  10.         activeFocusOnTab: true
  11.     }

  12.     Button
  13.     {
  14.         id :keyUp
  15.         anchors.top:keyFocus.top
  16.         anchors.topMargin: 20
  17.         anchors.left : keyFocus.right
  18.         anchors.leftMargin: 20
  19.         width: 100
  20.         height: 60

  21.         text: qsTr("up")

  22.         onClicked:
  23.         {
  24.             cKeyCDP.sendCkeyPressEvent(mainWindows,Qt.Key_Up);
  25.             cKeyCDP.sendCkeyReleaseEvent(mainWindows,Qt.Key_Up);
  26.         }

  27.     }
  28.     Button
  29.     {
  30.         id :keyDown
  31.         anchors.top:keyUp.bottom
  32.         anchors.topMargin: 20
  33.         anchors.left : keyFocus.right
  34.         anchors.leftMargin: 20
  35.         width: 100
  36.         height: 60

  37.         text: qsTr("down")
  38.         onClicked:
  39.         {
  40.             cKeyCDP.sendCkeyPressEvent(mainWindows,Qt.Key_Down);
  41.             cKeyCDP.sendCkeyReleaseEvent(mainWindows,Qt.Key_Down);
  42.         }
  43.     }
  44. }
复制代码
GridMenu.qml:


  1. FocusScope {
  2.     property alias interactive: gridView.interactive

  3.     onActiveFocusChanged: {
  4.         //if (activeFocus)
  5.             //mainView.state = "showGridViews"
  6.     }

  7.     Rectangle {
  8.         anchors.fill: parent
  9.         clip: true
  10.         gradient: Gradient {
  11.             GradientStop { position: 0.0; color: "#193441" }
  12.             GradientStop { position: 1.0; color: Qt.darker("#193441") }
  13.         }

  14.         GridView {
  15.             id: gridView
  16.             anchors.fill: parent; anchors.leftMargin: 20; anchors.rightMargin: 20
  17.             cellWidth: 152; cellHeight: 152
  18.             focus: true
  19.             model: 12

  20.             //KeyNavigation.up: tabMenu
  21.             //KeyNavigation.down: listMenu
  22.             //KeyNavigation.left: contextMenu

  23.             delegate: Item {
  24.                 id: container
  25.                 width: GridView.view.cellWidth; height: GridView.view.cellHeight

  26.                 Rectangle {
  27.                     id: content
  28.                     color: "transparent"
  29.                     antialiasing: true
  30.                     anchors.fill: parent; anchors.margins: 20; radius: 10

  31.                     Rectangle { color: "#91AA9D"; anchors.fill: parent; anchors.margins: 3; radius: 8; antialiasing: true }
  32.                     Image { source: "images/qt-logo.png"; anchors.centerIn: parent }
  33.                 }

  34.                 MouseArea {
  35.                     id: mouseArea
  36.                     anchors.fill: parent
  37.                     hoverEnabled: true

  38.                     onClicked: {
  39.                         container.GridView.view.currentIndex = index
  40.                         container.forceActiveFocus()
  41.                     }
  42.                 }

  43.                 states: State {
  44.                     name: "active"; when: container.activeFocus
  45.                     PropertyChanges { target: content; color: "#FCFFF5"; scale: 1.1 }
  46.                 }

  47.                 transitions: Transition {
  48.                     NumberAnimation { properties: "scale"; duration: 100 }
  49.                 }
  50.             }
  51.         }
  52.     }
  53. }
复制代码


本帖子中包含更多资源

您需要 登录 才可以下载或查看,没有帐号?立即注册

x
回复

使用道具 举报

累计签到:3 天
连续签到:1 天
2017-4-10 21:27:23 显示全部楼层
关注一下,最近开始在做一个类似的
回复

使用道具 举报

尚未签到

2017-4-10 21:57:22 显示全部楼层
插个红外解码模块 用串口通信就可以了
回复

使用道具 举报

累计签到:2 天
连续签到:1 天
2017-4-11 09:00:29 显示全部楼层
909254 发表于 2017-4-10 21:57
插个红外解码模块 用串口通信就可以了

不是遥控器和设备之间的通信问题,而是 设备已经接收到了遥控器的按键信息,
qt 如何处理此按键信息,像处理鼠标或键盘事件一样?

例如: qt识别键盘‘up’键,界面上焦点向上移动
          那么,qt如何识别遥控器的‘up’键,使界面上焦点向上移动  
回复

使用道具 举报

累计签到:5 天
连续签到:1 天
2017-6-8 09:43:56 显示全部楼层
收到信息后处理该信息就行了山
回复

使用道具 举报

累计签到:238 天
连续签到:1 天
2020-5-7 14:22:16 显示全部楼层
alex_wang 发表于 2017-4-12 19:13
我的解决方案:
创建一个c++类:模拟键盘按键类,功能发送键盘事件到指定的UI对象如图:
通过点击UI上的 ...

楼主用的是什么版本,我用的是QT5.9.5  代码中的activeFocusOnTab: true无效
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

公告
可以关注我们的微信公众号yafeilinux_friends获取最新动态,或者加入QQ会员群进行交流:190741849、186601429(已满) 我知道了