Important: Please read the Qt Code of Conduct -

QLabel`s hyperlink tab order activation

  • Hello!
    I have small question about Tab Order on the form with qlabels. In my project I have some qlabels with hyperlinks inside (inserted with rich text editor in designer with html tags).
    I settings up focusPolicy property on qlabel and set it to "TabFocus". So, I have correct tab order with some controls with qlabels.
    But I need that on tab press hyperlink have been activated, not entire qlabel. Now qlabel focused first and after this hyperlink inside qlabel on next tab press.
    QLabels was setup like

    label->setTextInteractionFlags(Qt::LinksAccessibleByKeyboard | Qt::LinksAccessibleByMouse);

    How can I skip focus on QLabel and immediately focused on Hypertext inside that qlabel?

    PS: I don`t know is that problem similar or not - but when I go to QToolButton with tab_key - it is not focused. QToolButton on form has stylesheet:

    	background: transparent;
    	border: 1px solid blue;
    QToolButton:focused, pressed
    	border: 1px solid blue;
    	border: 1px solid blue;

    but when I go to this button with tab key - widget reacts on pressing by Space, but is not highlighted with border.
    Thx again

    [Edit: Added code tags -- @Wieland]

  • I realize it's an old post. Anyway. The way I did this is by installing an event filter for QLabel in question with


    then in

    bool MyDialog::eventFilter(QObject * _target, QEvent * _event)

    emulate tab click on FocusIn event for QLabel:

       if (_target == ui->label && _event->type() == QEvent::FocusIn) {
    //        focusNextChild();
           QKeyEvent *tab_key_press = new QKeyEvent(QKeyEvent::KeyPress, Qt::Key_Tab, Qt::NoModifier);
           QApplication::sendEvent(_target, tab_key_press);

    note that focusNextChild(); (commented out) does not help here, is skips QLabel altogether. Emulated tab click works.

Log in to reply