Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. International
  3. Chinese
  4. QT5.12 Qwidget cannot get focus
Qt 6.11 is out! See what's new in the release blog

QT5.12 Qwidget cannot get focus

Scheduled Pinned Locked Moved Unsolved Chinese
1 Posts 1 Posters 603 Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • X Offline
    X Offline
    xixi_cly
    wrote on last edited by
    #1

    在按键机上使用qt测试按键,窗口无法获取焦点,按键不生效,是不是我的代码写的不对?

    Widget::Widget(QWidget *parent)
        : QWidget(parent)
    {
        setFocusPolicy(Qt::StrongFocus);
        setFixedSize(800, 800);
        qDebug()<<"begin test"<<endl;
        btn = new QPushButton(tr("open"),this);
        if (hasFocus()) {
            qDebug() << "has focus" << endl;
        } else {
            qDebug() << "NO focus" << endl;
        }
    }
    
    Widget::~Widget()
    {
    }
    
    void Widget::keyPressEvent(QKeyEvent *event) {
        qDebug()<<"try"<<endl;
        if(event->key() == Qt::Key_1) {
            qDebug()<<"press 1"<<endl;
            btn->move(50, 50);
            
            if (hasFocus()) {
                qDebug() << "has focus" << endl;
            } else {
                qDebug() << "NO focus" << endl;
            }
        }
    }
    
    1 Reply Last reply
    0

    • Login

    • Login or register to search.
    • First post
      Last post
    0
    • Categories
    • Recent
    • Tags
    • Popular
    • Users
    • Groups
    • Search
    • Get Qt Extensions
    • Unsolved