Introduction to GUI Qt Test???

  • Good day,
    I have created a subdir project that contains first a Qt GUI project of a currency converter (see picture below) and a second project, a QtTest to test the converter.
    Here is the .pro file of the subdir project:

    TEMPLATE = subdirs
    SUBDIRS += \
        CurrencyConverter \
    tests.subdirs = QtTest
    app.subdirs = CurrencyConverter
    QtTest.depends = CurrencyConverter

    and here is the .pro file of the Qt Test project

    QT += testlib gui
    greaterThan(QT_MAJOR_VERSION, 4): QT += widgets
    CONFIG += qt warn_on depend_includepath testcase
    TEMPLATE = app
    INCLUDEPATH += ../CurrencyConverter
    SOURCES +=  tst_testconverter.cpp

    When I add the view of the currency project in my test case, I got an undefined reference of the constructor of the View. What am doing wrong?

    #include <QtTest>
    #include <QCoreApplication>
    #include "view.h"
    // add necessary includes here
    class testConverter : public QObject
    private slots:
        void initTestCase();
        void cleanupTestCase();
        void test_case1();
    void testConverter::initTestCase()
    void testConverter::cleanupTestCase()
    void testConverter::test_case1()
        View view; // the problem occurs here
    #include "tst_testconverter.moc"

  • Lifetime Qt Champion

    But does the view constructor takes no arguments ?
    Normally it takes a parent.

  • @mrjj
    thanks for your reply, Here is the view:

    #include <QMainWindow>
    namespace Ui { class View; }
    class Controller;
    class View : public QMainWindow
        View(QWidget *parent = nullptr);
        void setController(Controller *controller);
    public slots:
        void set_amount(const QString& val);
        void get_recieve();
        Ui::View *ui;
        Controller *m_controller;
        friend class testConverter;

    and its cpp

    View::View(QWidget *parent)
        : QMainWindow(parent)
        , ui(new Ui::View)
        , m_controller(nullptr)
        connect(ui->amount_el, SIGNAL(textChanged(const QString &)),
                this, SLOT(set_amount(const QString &)));
        connect(ui->convert_btn, SIGNAL(clicked()),
                this, SLOT(get_recieve()));

    The parent by default is nullptr

  • The sub-project is not linked to View, so it does not know how to create a View, that's "undefined reference of the constructor" means.
    But I don't know what's the usual way to test an app's internal class...

  • I'm not familiar with Qt Test, I'm just trying if I can simulate mouse click event I could probably test the GUI with but I'm not sure if I'm in the right path

  • Lifetime Qt Champion


    @Bonnie is right. Your current issue is that you don't build the class along your test hence your error.

    You have several way of solving this:

    • make a library out of the components of your application and link against it. You app project will mainly be a main.cpp file creating the needed objects using that library as well.
    • stay as you are currently and build the files needed within the test sub project.

  • Hi @SGaist
    I have tried the first bullet by refactoring the code having an app as main project that just call my lib:

    #include "mainwindow.h"
    #include "currencyconverter.h"
    MainWindow::MainWindow(QWidget *parent)
        : QMainWindow(parent)
        CurrencyConverter *currency = new CurrencyConverter(parent);

    and my main .pro file looks like this:

    QT       += core gui
    greaterThan(QT_MAJOR_VERSION, 4): QT += widgets
    CONFIG += c++11
    # The following define makes your compiler emit warnings if you use
    # any Qt feature that has been marked deprecated (the exact warnings
    # depend on your compiler). Please consult the documentation of the
    # deprecated API in order to know how to port your code away from it.
    # You can also make your code fail to compile if it uses deprecated APIs.
    # In order to do so, uncomment the following line.
    # You can also select to disable deprecated APIs only up to a certain version of Qt.
    #DEFINES += QT_DISABLE_DEPRECATED_BEFORE=0x060000    # disables all the APIs deprecated before Qt 6.0.0
    LIBS += \
    SOURCES += \
        main.cpp \
    HEADERS += \
    # Default rules for deployment.
    qnx: target.path = /tmp/$${TARGET}/bin
    else: unix:!android: target.path = /opt/$${TARGET}/bin
    !isEmpty(target.path): INSTALLS += target

    When compiling, I'm still getting undefined reference to CurrencyConverter at line where currency pointer is initiaze. the first bullet seems not working for me.
    for the second bullet, what you do you mean by "build the files needed within the test sub project." ho do you do that in .pro file?

  • Lifetime Qt Champion

    What OS are you on ?

Log in to reply