AccessibleName for QPushButton stopped working in the 6.9.2 with the QTabWidget
-
What software are you using for accessibility ?
macOS as built-in support
There's NVDA for Windows
Orca for Linux
Those are just some example from memory, things may have changed since they were common. -
Please provide a minimal, compilable example (from what I see it should fit into 20 lines in a single main.cpp) so we can take a look on what you are trying to do and what's no longer working. Looks like you mis-use accessibleName property to modify the style of your widgets but I'm not sure.
-
*# .pro:
QT += core gui
greaterThan(QT_MAJOR_VERSION, 4): QT += widgets
CONFIG += c++17
You can make your code fail to compile if it uses deprecated APIs.
In order to do so, uncomment the following line.
#DEFINES += QT_DISABLE_DEPRECATED_BEFORE=0x060000 # disables all the APIs deprecated before Qt 6.0.0
SOURCES +=
main.cpp
mainwindow.cppHEADERS +=
mainwindow.hFORMS +=
mainwindow.uiDefault rules for deployment.
qnx: target.path = /tmp/$${TARGET}/bin
else: unix:!android: target.path = /opt/$${TARGET}/bin
!isEmpty(target.path): INSTALLS += targetRESOURCES +=
resource.qrc -
# main.cpp:
#include "mainwindow.h"
#include <QApplication>
#include <QFile>int main(int argc, char *argv[])
{
QApplication app(argc, argv);QFile styleFile(":/res/stylesheet.qss"); bool ret = styleFile.open(QFile::ReadOnly); qDebug() << "\nMain function (app starts): styleFile.open = " << ret << "\n"; QString styleSheet = QLatin1String(styleFile.readAll()); app.setStyleSheet(styleSheet); MainWindow win; win.show(); return app.exec();
}
====================================================
# mainwindow.h:
#ifndef MAINWINDOW_H
#define MAINWINDOW_H#include <QMainWindow>
QT_BEGIN_NAMESPACE
namespace Ui {
class MainWindow;
}
QT_END_NAMESPACEclass MainWindow : public QMainWindow
{
Q_OBJECTpublic:
MainWindow(QWidget *parent = nullptr);
~MainWindow();private:
Ui::MainWindow *ui;
};
#endif // MAINWINDOW_H====================================================
# mainwindow.cpp:
#include "mainwindow.h"
#include "ui_mainwindow.h"MainWindow::MainWindow(QWidget *parent)
: QMainWindow(parent)
, ui(new Ui::MainWindow)
{
ui->setupUi(this);
}MainWindow::~MainWindow()
{
delete ui;
}====================================================
# mainwindow.ui:
<?xml version="1.0" encoding="UTF-8"?>
<ui version="4.0">
<class>MainWindow</class>
<widget class="QMainWindow" name="MainWindow">
<property name="geometry">
<rect>
<x>0</x>
<y>0</y>
<width>800</width>
<height>600</height>
</rect>
</property>
<property name="windowTitle">
<string>MainWindow</string>
</property>
<widget class="QWidget" name="centralwidget">
<widget class="QTabWidget" name="tabWidget">
<property name="geometry">
<rect>
<x>20</x>
<y>140</y>
<width>751</width>
<height>131</height>
</rect>
</property>
<property name="currentIndex">
<number>1</number>
</property>
<widget class="QWidget" name="tab">
<attribute name="title">
<string>Tab 1</string>
</attribute>
<widget class="QPushButton" name="btn1">
<property name="geometry">
<rect>
<x>60</x>
<y>40</y>
<width>80</width>
<height>25</height>
</rect>
</property>
<property name="accessibleName">
<string>btn_1</string>
</property>
<property name="text">
<string>Button 1</string>
</property>
</widget>
</widget>
<widget class="QWidget" name="tab_2">
<attribute name="title">
<string>Tab 2</string>
</attribute>
<widget class="QPushButton" name="btn2">
<property name="geometry">
<rect>
<x>130</x>
<y>40</y>
<width>80</width>
<height>25</height>
</rect>
</property>
<property name="accessibleName">
<string>btn_2</string>
</property>
<property name="text">
<string>Button 2</string>
</property>
</widget>
</widget>
</widget>
</widget>
<widget class="QMenuBar" name="menubar">
<property name="geometry">
<rect>
<x>0</x>
<y>0</y>
<width>800</width>
<height>22</height>
</rect>
</property>
</widget>
<widget class="QStatusBar" name="statusbar"/>
</widget>
<resources/>
<connections/>
</ui>====================================================
# stylesheet.qss:
QPushButton[accessibleName="btn_1"]
{
background-color: #9afc9d;
color: #000000;
border: 1px solid #728773;
border-radius: 6px;
}QPushButton[accessibleName="btn_2"]
{
background-color: #0afc9d;
color: #000000;
border: 1px solid #728773;
border-radius: 6px;
} -
These are more than 20 lines and even ore than one file.
Reduce your testcase if you want me to look at it. -
Oh sorry, not 20 lines but 23:
#include <QtWidgets> int main(int argc, char** argv) { QApplication app(argc, argv); app.setStyleSheet(R"( QPushButton[accessibleName="btn_1"] { background-color: #9afc9d; } QPushButton[accessibleName="btn_2"] { background-color: #0afc9d; } )"); QWidget w; QVBoxLayout* lay = new QVBoxLayout(&w); auto pb1 = new QPushButton("Push me once"); pb1->setAccessibleName("btn_1"); auto pb2 = new QPushButton("Push me twice"); pb2->setAccessibleName("btn_2"); lay->addWidget(pb1); lay->addWidget(pb2); w.show(); return app.exec(); }
This is working fine for me with current Qt from git with windows/windowsvista/windows11 and fusion style:
/edit: Installed 6.9.2 with the online installer and also works as expected.
-
This is also working fine:
int main(int argc, char** argv) { QApplication app(argc, argv); app.setStyleSheet(R"( QPushButton[accessibleName="btn_1"] { background-color: #9afc9d; } QPushButton[accessibleName="btn_2"] { background-color: #0afc9d; } )"); QMainWindow mw; auto tw = new QTabWidget; mw.setCentralWidget(tw); auto w = new QWidget; tw->addTab(w, "Tab"); QVBoxLayout* lay = new QVBoxLayout(w); auto pb1 = new QPushButton("Push me once"); pb1->setAccessibleName("btn_1"); auto pb2 = new QPushButton("Push me twice"); pb2->setAccessibleName("btn_2"); lay->addWidget(pb1); lay->addWidget(pb2); mw.show(); return app.exec(); }
Please provide a minimal, compilable example to reproduce the problem.
-
I added it before and it works. But this code doesn't reproduce the problem :(
#include <QApplication>
#include <QMainWindow>
#include <QtWidgets>int main(int argc, char** argv)
{
QApplication app(argc, argv);app.setStyleSheet(R"(
QPushButton[accessibleName="btn_1"]
{ background-color: #9afc9d; }
QPushButton[accessibleName="btn_2"]
{ background-color: #0afc9d; }
)");QMainWindow win; QWidget cw(&win); win.setCentralWidget(&cw); QTabWidget tw(&cw); QWidget w1; tw.addTab(&w1, "1"); QVBoxLayout* lay1 = new QVBoxLayout(&w1); auto pb1 = new QPushButton("Push me once", &w1); pb1->setAccessibleName("btn_1"); lay1->addWidget(pb1); QWidget w2; tw.addTab(&w2, "2"); QVBoxLayout* lay2 = new QVBoxLayout(&w2); auto pb2 = new QPushButton("Push me twice", &w2); pb2->setAccessibleName("btn_2"); lay2->addWidget(pb2); win.show(); return app.exec();
}
Thanks. Sorry, I provide example earlier. It reproduces the problem.
-
Maybe have a look at the generated
ui_mainwindow.h
file. You need to figure out what is different between the example without the ui-file and with the ui-file. The ui-file just gets translated to regular C++ code. -
Thanks. There is no any unusual :(. It worked for years. UI file created by qt designer:
<widget class="QPushButton" name="btn1">
<property name="geometry">
<rect>
<x>60</x>
<y>40</y>
<width>80</width>
<height>25</height>
</rect>
</property>
<property name="accessibleName">
<string>btn_1</string>
</property>
<property name="text">
<string>Button 1</string>
</property>
</widget>Just "accessibleName". And it works for second page of the TabWidget.
-
I still don't understand what's the problem minimizing your code until the problem goes away... The ui file is also only c++ code so move it to your cpp and reduce it. Daily business of a programmer.
-
The problem isn't in minimizing my code :(. The question is about this issue. Why did this happen? What is the cause? It's not about my code. I'll figure out what to do to fix it. I want to figure out why it happened. It mustn't be such issue, but it exists in 6.9.2 exactly. And when and where will the problem with the "accessibleName" happen next time? Always move parameter "accessibleName" to c++ code? Will it work if ui file has more complex GUI?
-
The problem isn't in minimizing my code :(. The question is about this issue. Why did this happen? What is the cause? It's not about my code. I'll figure out what to do to fix it. I want to figure out why it happened. It mustn't be such issue, but it exists in 6.9.2 exactly. And when and where will the problem with the "accessibleName" happen next time? Always move parameter "accessibleName" to c++ code? Will it work if ui file has more complex GUI?
@Olex said in AccessibleName for QPushButton stopped working in the 6.9.2 with the QTabWidget:
Why did this happen? What is the cause?
You will find it out when you minimize your code until the problem goes away... I won't do it for you, I already showed you that it is working fine.
-
I didn't ask you do any thing for me and won't ask. And you haven't showed that parameter "accessibleName" from ui file file works fine :(. You just showed how you can get around the problem.
@Olex said in AccessibleName for QPushButton stopped working in the 6.9.2 with the QTabWidget:
And you haven't showed that parameter "accessibleName" from ui file file works fine
Qt Designer saves a
.ui
file. When you "build" a Qt program,uic
, is run on the.ui
file. That produces a C++ source/header file, namedui_....h
and placed in the build output directory. You include it in your.cpp
via a#include
.So the
.ui
has no special significance. It is not used at runtime. Go look in that file to see what it does for theaccessibleName
you set in Designer --- I'm guessing it's something like code for creating aQObject
property on the widget. Whatever you can work from there. It means the.ui
cannot "affect" anything directly, everything it contains is turned into C++ code in theui_....h
file.