Getting the value of a QLabel with an accessible name using UI Automation - qt

I'm trying to write an UI test (using Windows UI Automation) for an application that uses a QLabel to show an output to a user.
I create the label like this:
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
QMainWindow w;
w.setWindowTitle("MyWindowTitle");
auto centralWidget = new QWidget(&w);
centralWidget->setSizePolicy(QSizePolicy::Minimum, QSizePolicy::Minimum);
QVBoxLayout layout(centralWidget);
auto interrestingLabel = new QLabel(centralWidget);
QString valueCalculatedByApp = "1337";
interrestingLabel->setText(valueCalculatedByApp);
//interrestingLabel->setAccessibleName("MyAccessibleName");
layout.addWidget(interrestingLabel);
auto uninterrestingLabel = new QLabel(centralWidget);
uninterrestingLabel->setText("uninterrestingText");
layout.addWidget(uninterrestingLabel);
w.setCentralWidget(centralWidget);
w.show();
return a.exec();
}
Inspect.exe now shows the value "1337" as name of the widget:
Without accessible name
The problem with this would be that my UI test would need to figure out which one is the correct label.
If I uncomment setAccessibleName line, the widget is now identifiable, but I the text is no longer in visible in the properties.
With accessible name
Is there a way to read the text of a QLabel with an accessible name, or is there another way to make the QLabel identifieable while still being able to read the text?

I found a workaround:
Instead of a QLabel I use a QLineEdit. I set enabled to false so it is not selectable or editable (Like a QLabel) and use QSS to make it look like a QLabel:
#include <QApplication>
#include <QLabel>
#include <QLineEdit>
#include <QMainWindow>
#include <QSizePolicy>
#include <QVBoxLayout>
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
a.setAttribute(Qt::AA_NativeWindows);
QMainWindow w;
w.setWindowTitle("MyWindowTitle");
auto centralWidget = new QWidget(&w);
centralWidget->setSizePolicy(QSizePolicy::Minimum, QSizePolicy::Minimum);
QVBoxLayout layout(centralWidget);
auto interrestingLabel = new QLineEdit(centralWidget);
QString valueCalculatedByApp = "1337";
interrestingLabel->setText(valueCalculatedByApp);
interrestingLabel->setAccessibleName("MyAccessibleName");
interrestingLabel->setEnabled(false);
interrestingLabel->setStyleSheet(
"border-style: none;"
"color: black;"
"background:transparent"
);
layout.addWidget(interrestingLabel);
auto uninterrestingLabel = new QLabel(centralWidget);
uninterrestingLabel->setText("uninterrestingText");
layout.addWidget(uninterrestingLabel);
w.setCentralWidget(centralWidget);
w.show();
return a.exec();
}

Related

Having trouble drawing an image in Qt using QGraphicsScene

The code below loads an image using QLabel. The code uses: myLabel.setMask(pixmap.mask()); and it works as it should. The problem comes when I try and load an image using QGraphicsScene.
#include <QApplication>
#include <QLabel>
#include <QtGui>
int main(int argc, char *argv[])
{
QApplication app(argc, argv);
QLabel myLabel;
QPixmap pixmap("/path/tomy/image.jpg");
myLabel.setPixmap(pixmap);
myLabel.setMask(pixmap.mask());
myLabel.show();
return app.exec();
}
In this code I am attempting to the same as above but using QGraphicsScene. The pixmap is loaded properly, after that I am not sure why the program is not working properly. Is it because there is no setMask() operation? Or is there an operation missing that is needed to make the image visible?
#include <QtGlobal>
#if QT_VERSION >= 0x050000
#include <QtWidgets>
#else
#include <QtGui>
#endif
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
QPixmap pixmap("/path/tomy/image.jpg");
QGraphicsPixmapItem item( pixmap);
QGraphicsScene* scene = new QGraphicsScene;
scene->addItem(&item);
QGraphicsView view(scene);
view.show();
return a.exec();
}

Custom QWidget in QFormLayout, vertical alignment issue

I would like to use a custom made QWidget in one row of a QFormLayout. The code below presents a form layout in which the first row has a a QLineEdit and the second line has a custom made widget:
The problem is that the custom made widget is not vertically aligned. How to align it vertically?
project.pro:
QT += core gui
greaterThan(QT_MAJOR_VERSION, 4): QT += widgets
TARGET = CustomLineEdit
TEMPLATE = app
SOURCES += main.cpp
main.cpp
#include <QApplication>
#include <QHBoxLayout>
#include <QFormLayout>
#include <QLineEdit>
#include <QPushButton>
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
QWidget w;
QFormLayout *formLayout = new QFormLayout(&w);
QLineEdit *leUser = new QLineEdit;
QWidget *widget = new QWidget;
QHBoxLayout *hLayout = new QHBoxLayout(widget);
hLayout->addWidget(leUser);
hLayout->addWidget(new QPushButton);
formLayout->addRow("Text:", new QLineEdit);
formLayout->addRow("User:", widget);
w.show();
return a.exec();
}
This is because of margins. Just add followed line:
QHBoxLayout *hLayout = new QHBoxLayout(widget);
hLayout->setMargin(0); //this line

Undesirable text overlapping

When I edit the QTableView the old text is not cleared and so the new text overlaps it. How can I avoid this behaviour?
The code:
#include <QApplication>
#include <QtSql>
#include <QtGui>
#include <QTableView>
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
QSqlDatabase db1 = QSqlDatabase::addDatabase("QSQLITE");
db1.setDatabaseName(":memory:");
db1.open();
QSqlQuery("CREATE TABLE test (a integer primary key, s text)");
QSqlQuery("INSERT INTO test VALUES (1, 'aaa');");
QSqlTableModel *model = new QSqlTableModel(0, db1);
model->setTable("test");
model->select();
QTableView *view = new QTableView;
view->setModel(model);
view->show();
return a.exec();
}
I have simular issue with dynamic QLabel.
When label text is updated new text was overlaped with old one. The problem was related to transparent background color.
As you find out the solution for you is to use such stylesheet QTableView::item {}
Complete Code:
view->setStyleSheet("QTableView::item {}");

Error Occured while designing Dialog in Qt

The program name is GoToCell in the form I created a label and two push buttons and I wrote the code in main.cpp as follows:
#include <QtGui/QApplication>
#include<QDialog>
#include "ui_GoToCell.h"
#include "GoToCell.h"
int main(int argc, char *argv[]) {
QApplication a(argc, argv);
Ui::GoToCell ui;
QDialog *dialog=new QDialog;
ui.setupUi(dialog);
dialog->show();
return a.exec();
}
While running it I'm getting the following errors:
GoToCell is not a member of ui
What should I do?
I think you have misspelled object name in objectName property in the GoToCell.ui form.
Change it to GoToCell. It will then execute.

How to create a correct exit button in qt

I'm trying to create an exit button that correctly closes the GUI I have made in QT. I have tried doing this in the following way:
#include <QtGui/QApplication>
#include "mainwindow.h"
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
int window_width = QApplication::desktop()->width();
int window_height = QApplication::desktop()->height();
MainWindow w;
QPushButton * quit_btn = new QPushButton;
quit_btn->setParent(w.centralWidget());
quit_btn->setGeometry(window_width-50,12,32,32);
QObject::connect(quit_btn,SIGNAL(clicked()),qApp,SLOT(quit()));
w.resize(window_width,window_height);
w.show();
return a.exec();
}
Unfortunately when I push the button, the debugger gives an error:
Invalid address specified to RtlFreeHeap( 003E0000, 0028F950 )
Can anybody point me in the right direction?
Connect the button's clicked() signal to your main window's close() slot. That way things are cleaned up properly.

Resources