How can I test a full Qt5 GUI using qtestlib? - c++

Using qtestlib with my Qt5 C++ widgets application, how can I test my full GUI?
In the documentation for qtestlib, it is explained how I could test an indivdual QWidget by simulating keypresses etc, however this seems impossible to do for a full UI, because the individual widgets of my UI are hidden inside the automatically generated ui_XXX.h file.
So how would I go about doing this?

In your ui files you can give names to the widgets. You can then search through the children from your toplevel widget.
For example to test that a Label reflects the characters entered into a LineEdit you could use:
MainWindow win;
QLineEdit *edit = win.findChild<QLineEdit *>("myLineEdit");
QTest::sendKeys(edit, "Example");
QLabel *label = win.findChild<QLabel *>("myLabel");
QCOMPARE(label->text(), "Example");

Related

Qt5 Designer and Encapsulation

I've created a main window in Qt Desginer which has the following structure
Ui_MainWindow
VerticalLayout
QTabWidget
Widget (*A)
QChart
QChart
The uic generates a header file "ui_mainwindow.h". This header files contains references on all the sub (subsub, subsubsub, ...) widgets. In my point of view this isn't good encapsulation.
I found a way to set custom classes for the different widgets in the Qt Designer.
Since I have to write these classes manually I have no access/references to the child widgets. I only can control the widget itself. Still all widgets are referenced in the MainWindow UI.
Is there a way to generate separate classes for each (or some selected) widget? I'd like to intercept *A and access the child QCharts.
Thank you,
Even though you can already "intercept A" in the current form. You can certainly separate A in a different section then add a QWidget in your main window, right click on it and promote it to being A

How to acces other ui files in Qt Creator

How can I acces other ui files from code using Qt Creator?
I am trying to create a small text editor. As always, I designed the main window by pressing on the Design button . After that , I created a new ui file from Qt creator called about.ui (the IDE also added about.h and about.cpp ). The main window's functions(like on_button_cliked() ) are in the file textedit.cpp
I created a small menu with an about entry, and then I went to Action Editor ,I selected the entry , Go to Slot, and the trigerred();
The IDE created all the things for me, so all I need is to define the function.
How can I acces the about ui that I created earlier?
Thanks in advance
You don't need the access to the '.ui' file. Qt's ui application does everything for you. All you need now is to create an object of your About class with MainWindow as a parent and show it.
The approximate code will be:
#include "about.h"
// ...
void MainWindow::on_about_triggered()
{
About dialog(this);
dialog.exec();
}

Qt creator - .ui form displaying in new window

I'm in the process of creating a GUI in Qt creator. Right now I have different .ui forms that correspond with different parts of my GUI. My login.ui window appears when you first run the program. When you click the loginButton, a new window displays with my MainMenuScreen.ui form. My problem is I don't want the MainMenuScreen to open in a new window...I want the application to only have one window and the contents change...any way I can do this by keeping separate .ui forms and without using StackedWidgets? My LoginScreen.ui and MainMenuScreen.ui both extend QWidget and in my Login.cpp I have an onclick event function which calls show() and creates the new window of my MainMenuScreen.
Thanks!
I think you can just call the setCentralWidget() method of your QMainWindow to reset the widget that is displayed in the Window.

retranslateUi() clears QLabel text

My qt4-based application dynamically changes the GUI language by calling retranslateUi() function. It works like a charm, but when it processes the QLabel which text changes by the program, it tries to translate original text - the one, specified in Qt Designer, not the one, set by my program.
I understand it happens because retranslateUi() is auto-generated during build process so it couldn't possibly know about my changes, but I'd prefer to skip such QLabels completely. Is there a way to tell Qt to skip certain widgets in retranslateUi()? Alternatively, maybe I can disable content change for QLabel in my program?
I know I can sub-class QLabel and by overriding setText() solve this problem, but I prefer to use standard QLabel to be able to manipulate it using Qt Designer.
As I remember, in Designer you can uncheck on QLabel should it be translated. By default it is. Choose label you don't want to be translated, in property editor scroll to "text" property, expand it and uncheck translate checkbox. Then you should generate ui_*.h file again. After that your label shouldn't be in retranslateUi code

Floating/Embedded QDockWidget in a QWidget (KXmlGuiWindow's CentralWidget designed in QT Designer)

I'm just trying to get into QT (and KDE) program, and have hit a snag trying to add a floatable, draggable QDockWidget to the .ui file (based as a QWidget) that is embedded into my KDE 4 program.
This is all from the basic template provided by KDevelop 4, so while I understand what's going on, I just don't know the best way to change it.
Here's the deal: main.cpp spawns a new AEmpire window, which starts the whole show off:
AEmpire::AEmpire()
: KXmlGuiWindow(),
m_view(new AEmpireView(this)),
m_printer(0)
{
// tell the KXmlGuiWindow that this is indeed the main widget
setCentralWidget(m_view);
setupActions();
setupGUI();
}
When a new AEmpireView(this) is created (which inherits from QWidget) and is assigned to m_view, this constructor is called:
AEmpireView::AEmpireView(QWidget *)
{
ui_aempireview_base.setupUi(this);
settingsChanged();
setAutoFillBackground(true);
}
So, when I am editing the ui to my program in QT Designer, I'm actually editing the AEmpireView_base ui file, which is a QWidget. It just represents the main view of the KXmlGuiWindow (derived from QMainWindow) and is loaded at runtime.
So, how do I add floatable, draggable QDockWidgets into my main application? Is designing them separately, and add them to the UI the best option? Or maybe removing the entire AEmpireView class, and making my ui file directly represent a KXmlGuiWindow object to be loaded by the AEmpireClass?
Or am I totally overlooking something obvious? Thanks for reading!
I would design the QDockWidget contents as separate UI files. Then create them and stick them into the QDockWidgets in the AEmpire constructor.