QT_WIDGETS_LIB



  • In my unit test, QTEST_MAIN macro does not generate a QApplication. Reading the doc realized that it is necessary to define QT_WIDGETS_LIB but do not know if I'm doing it right.

    @/*

    • dynamicqtwidgetstest.hpp
    • This file is part of dynamicqtwidgets.so
    • Copyright (C) 2014 - Matheus Saraiva
    • dynamicqtwidgets.so is free software; you can redistribute it and/or modify
    • it under the terms of the GNU General Public License as published by
    • the Free Software Foundation; either version 2 of the License, or
    • (at your option) any later version.
    • dynamicqtwidgets.so is distributed in the hope that it will be useful,
    • but WITHOUT ANY WARRANTY; without even the implied warranty of
    • MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
    • GNU General Public License for more details.
    • You should have received a copy of the GNU General Public License
    • along with dynamicqtwidgets.so. If not, see http://www.gnu.org/licenses/.
      */

    #ifndef DYNAMICQTWIDGETSTEST_H

    define DYNAMICQTWIDGETSTEST_H

    #ifdef QT_WIDGETS_LIB

    #include <QtCore/QString>
    #include <QtTest/QtTest>
    #include "dynamicqtwidgets.hpp"

    class DynamicQtWidgetsTest : public QObject
    {
    Q_OBJECT

    private Q_SLOTS:
       void testCreateWidget();
       void testCreateWidget_data();
    

    };

    QTEST_MAIN(DynamicQtWidgetsTest)

    #endif
    #endif@



  • Forgot to tell, I'm not using qmake, the makefile manually written



  • Use #define QT_WIDGETS_LIB Instead of #ifdef QT_WIDGETS_LIB and don't forget to remove the corresponding #endif
    I'm not sure if it is good idea to have QTEST_MAIN to a header file.
    Also you may need to include moc generated file at the end of your test.cpp file.



  • I changed the code of test for a unique cpp file, as is done in the QTest documentation .
    I am now getting an error of type conversion.

    @In file included from /opt/Qt/5.3/gcc_64/include/QtTest/qtest.h:293:0,
    from /opt/Qt/5.3/gcc_64/include/QtTest/QtTest:7,
    from dynamicqtwidgetstest.cpp:23:
    /opt/Qt/5.3/gcc_64/include/QtTest/qtestsystem.h: In function ‘bool QTest::qWaitForWindowActive(QWidget*, int)’:
    /opt/Qt/5.3/gcc_64/include/QtTest/qtestsystem.h:122:56: error: cannot convert ‘QWindow*’ to ‘QWidget*’ for argument ‘1’ to ‘bool QTest::qWaitForWindowActive(QWidget*, int)’
    return qWaitForWindowActive(window, timeout);
    ^
    /opt/Qt/5.3/gcc_64/include/QtTest/qtestsystem.h: In function ‘bool QTest::qWaitForWindowExposed(QWidget*, int)’:
    /opt/Qt/5.3/gcc_64/include/QtTest/qtestsystem.h:129:57: error: cannot convert ‘QWindow*’ to ‘QWidget*’ for argument ‘1’ to ‘bool QTest::qWaitForWindowExposed(QWidget*, int)’
    return qWaitForWindowExposed(window, timeout);
    ^
    @

    "My Makefile":http://paste.ubuntu.com/7794975/

    test code

    @/*

    • dynamicqtwidgetstest.cpp
    • This file is part of dynamicqtwidgets.so
    • Copyright (C) 2014 - Matheus Saraiva
    • dynamicqtwidgets.so is free software; you can redistribute it and/or modify
    • it under the terms of the GNU General Public License as published by
    • the Free Software Foundation; either version 2 of the License, or
    • (at your option) any later version.
    • dynamicqtwidgets.so is distributed in the hope that it will be useful,
    • but WITHOUT ANY WARRANTY; without even the implied warranty of
    • MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
    • GNU General Public License for more details.
    • You should have received a copy of the GNU General Public License
    • along with dynamicqtwidgets.so. If not, see http://www.gnu.org/licenses/.
      */

    define QT_WIDGETS_LIB

    #include <QtCore/QString>
    #include <QtTest/QtTest>
    #include "dynamicqtwidgets.hpp"

    class DynamicQtWidgetsTest : public QObject
    {
    Q_OBJECT

    private Q_SLOTS:
    void testCreateWidget();
    void testCreateWidget_data();
    };

    void DynamicQtWidgetsTest::testCreateWidget()
    {
    QFETCH(QString, file_ui);
    QFETCH(bool, widget);

    DynamicQtWidgets dynawidg(file_ui); // DynamicQtWidgets object
    QWidget *widtest = dynawidg.createWidget();
    QCOMPARE(widtest->isWidgetType(), widget); // Compare DynamicQtWidgets::createWidget() return with QWidget type
    delete widtest;
    }

    void DynamicQtWidgetsTest::testCreateWidget_data()
    {
    QTest::addColumn<QString>("file_ui");
    QTest::addColumn<bool>("widget");

    QTest::newRow("Valid File") << "test.ui" << true; // Valid File
    QTest::newRow("Inexistent File") << "inexistent.ui" << true; // Inexistent File
    QTest::newRow("Invalid File") << "test_text.ui" << true; // Invalid File
    QTest::newRow("Empty Ui File") << "testempty.ui" << true; // Empty File
    }

    QTEST_MAIN(DynamicQtWidgetsTest)
    #include "dynamicqtwidgetstest.moc"@



  • If i remove "#define QT_WIDGETS_LIB" initial problem back

    If i use QT_GUI_LIB return: undefined reference to symbol '_ZN15QGuiApplicationC1ERiPPci' error
    error adding symbols: DSO missing from command line

    @#ifndef QT_GUI_LIB

    define QT_GUI_LIB

    #endif

    #include <QtCore/QString>
    #include <QtTest/QtTest>
    #include "dynamicqtwidgets.hpp"

    class DynamicQtWidgetsTest : public QObject
    {
    Q_OBJECT

    private Q_SLOTS:
    void testCreateWidget();
    void testCreateWidget_data();
    };

    void DynamicQtWidgetsTest::testCreateWidget()
    {
    QFETCH(QString, file_ui);
    QFETCH(bool, widget);

    DynamicQtWidgets dynawidg(file_ui); // DynamicQtWidgets object
    QWidget *widtest = dynawidg.createWidget();
    QCOMPARE(widtest->isWidgetType(), widget); // Compare DynamicQtWidgets::createWidget() return with QWidget type
    delete widtest;
    }

    void DynamicQtWidgetsTest::testCreateWidget_data()
    {
    QTest::addColumn<QString>("file_ui");
    QTest::addColumn<bool>("widget");

    QTest::newRow("Valid File") << "test.ui" << true; // Valid File
    QTest::newRow("Inexistent File") << "inexistent.ui" << true; // Inexistent File
    QTest::newRow("Invalid File") << "test_text.ui" << true; // Invalid File
    QTest::newRow("Empty Ui File") << "testempty.ui" << true; // Empty File
    }

    #include "dynamicqtwidgetstest.moc"
    QTEST_MAIN(DynamicQtWidgetsTest)@



  • @
    dynamicqtwidgetstest.moc: dynamicqtwidgetstest.cpp
    moc dynamicqtwidgetstest.cpp -o ../mocs/dynamicqtwidgetstest.moc
    @
    Is it possible that you have another moc in the $PATH ?



  • No, just the mocfile that was generated by the compilation line you quoted.



  • I meant an executable. Do you have another Qt installed?
    You calling moc in Makefile without full path so if you have other moc in the $PATH then it may be called instead.



  • I created a test case using your files. And got the error you are describing.
    I think it is related to the fact that you don't use qmake and did not provided all necessary flags.
    If you have to avoid qmake (why?) then try to create an empty Qt Test project in QtCreator, run qmake and compare the include and link flags.



  • Unsuccessfully

    I followed his advice, I created a test project with QtCreator and looked flags which he is using. I Added the same flags in my compile line, but the error continues.

    I added flags:

    -pipe -g- W -D_REENTRANT

    I also changed the flag-fPIC to -fPIE

    This is my first deep contact with C + + and Qt, and I not want to become dependent of QtCreator.
    But pareque that Qt does not encourage more manual programming.



  • Here is my Makefile that worked for me
    @
    #compiler
    CXX = g++

    #path for Qt
    QT = /home/user/Qt5.3.1/5.3/gcc_64

    #path for libs Qt
    LIBSQT = -L$(QT)/lib/

    #path for includes Qt
    INCSQT = -I$(QT)/include/

    #path for include taget test
    INCOBJTEST = -I../src

    #path for moc files
    MOCS = -I../mocs

    #path for taget test
    OBJTEST = -L../build

    #All includes
    INCPATH = -I$(QT)/mkspecs/linux-g++ -I$(QT)/include/QtWidgets -I$(QT)/include/QtTest -I$(QT)/include/QtGui -I$(QT)/include/QtCore -I.
    INCS = $(INCPATH) $(INCSQT) $(MOCS) $(INCOBJTEST) -DQT_WIDGETS_LIB -DQT_TESTLIB_LIB -DQT_GUI_LIB -DQT_CORE_LIB

    #All libs
    LIBS = $(LIBSQT) -L$(QT)/lib -lQt5Widgets -lQt5Test -lQt5Gui -lQt5Core -lGL -lpthread

    #final target
    DynamicQtWidgetsTest: dynamicqtwidgetstest.moc dynamicqtwidgetstest.cpp
    $(CXX) -Wall -fPIC -oDynamicQtWidgetsTest $(word 2,$^) $(INCS) -Wl,-rpath,/home/user/Qt5.3.1/5.3/gcc_64 -Wl,-rpath,/home/user/Qt5.3.1/5.3/gcc_64/lib $(LIBS)

    #Generate moc file
    dynamicqtwidgetstest.moc: dynamicqtwidgetstest.cpp
    $(QT)/bin/moc -DQT_WIDGETS_LIB -DQT_TESTLIB_LIB -DQT_GUI_LIB -DQT_CORE_LIB -DQT_TESTCASE_BUILDDIR="/home/user/tmp/test_forum" -I$(QT)/mkspecs/linux-g++ -I/home/user/tmp/qmake_test/untitled1 -I$(QT)/include -I$(QT)/include/QtWidgets -I$(QT)/include/QtTest -I$(QT)/include/QtGui -I$(QT)/include/QtCore -I. dynamicqtwidgetstest.cpp -o dynamicqtwidgetstest.moc

    clean:
    rm -f dynamicqtwidgetstest.moc DynamicQtWidgetsTest
    @



  • I chose create the main manually, I had no success, even after seeing your makefile.
    I put the "project":https://github.com/matheusssilva/DynamicQtWidgets on github, there you can have a full view of it.
    Maybe so you can tell what is missing for QTEST_MAIN work



  • I've modified three files to make your project buildable and runable

    • src/Makefile
      @
      #compiler
      CXX = g++

    #path for Qt
    QT = /opt/Qt/5.3/gcc_64

    #path for libs Qt
    LIBSQT = -L$(QT)/lib/

    #path for includes Qt
    INCSQT = -I$(QT)/include/

    #generate final lib
    ../build/libdynamicqtwidgets.so: ../build/dynamicqtwidgets.o
    $(CXX) -Wall -shared -o ../build/libdynamicqtwidgets.so $< $(LIBSQT) -lQt5Core -lQt5Widgets -lQt5UiTools

    #generate dynamicqtwidgets.o
    ../build/dynamicqtwidgets.o: dynamicqtwidgets.cpp dynamicqtwidgets.hpp dynamicqtwidgets_global.hpp
    $(CXX) -Wall -c -fPIC -o ../build/dynamicqtwidgets.o $< $(INCSQT)
    @

    • tests/Makefile
      @
      #compiler
      CXX = g++

    #path for Qt
    QT = /opt/Qt/5.3/gcc_64

    #path for libs Qt
    LIBSQT = -L$(QT)/lib/

    #path for includes Qt
    INCSQT = -I$(QT)/include/

    #path for include taget test
    INCOBJTEST = -I../src

    #path for moc files
    MOCS = -I../mocs

    #path for taget test
    OBJTEST = -L../build

    #All includes
    INCS = $(INCSQT) $(MOCS) $(INCOBJTEST) -I.

    #All libs
    LIBS = $(LIBSQT) $(OBJTEST) -L$(QT)/lib -lQt5Widgets -lQt5Test -lQt5Gui -lQt5Core -lGL -lpthread

    #final target
    ../build/Test: ../mocs/dynamicqtwidgetstest.moc main.cpp dynamicqtwidgetstest.cpp dynamicqtwidgetstest.hpp
    $(CXX) -Wall -fPIC -o ../build/Test $(word 2,$^) $(word 3,$^) $(INCS) -Wl,-rpath,'$$ORIGIN' $(LIBS) -ldynamicqtwidgets

    #Generate moc file
    ../mocs/dynamicqtwidgetstest.moc: dynamicqtwidgetstest.hpp
    $(QT)/bin/moc $< -o ../mocs/dynamicqtwidgetstest.moc
    @

    • tests/main.cpp
      @
      #include "dynamicqtwidgetstest.hpp"
      #include <QtWidgets/QApplication>

    int main(int argc, char *argv[])
    {
    QApplication app(argc, argv);

    DynamicQtWidgetsTest tests;
    QTest::qExec(&tests, argc, argv);

    return 0;
    }
    @

    [EDIT] I replaced '@' character in the Makefiles to make qt-project source code parses happy.



  • Thanks bro. I wanted to use QTEST_MAIN but Qt does not document the use of QT_WIDGETS_LIB without qmake.

    Thanks for the tip of the main.cpp, I sought a way to finalize the main after test. Could not remember the old 'return 0'.
    As for the '@', did not understand what you say.



  • [quote author="Exotic_Devel" date="1405550468"]I wanted to use QTEST_MAIN but Qt does not document the use of QT_WIDGETS_LIB without qmake.[/quote]

    qmake adds a -DQT_WIDGETS_LIB option to g++ command. As well as some other defines like -DQT_TESTLIB_LIB -DQT_GUI_LIB -DQT_CORE_LIB

    See my previous Makefile example.

    You can use QTEST_MAIN just add these options to g++ command line.
    But if you need to make your test multi-platform then you need to investigate what qmake does for Visual Studio and clang. I think clang has the same command line as g++.

    And for that reason (multi-platform) qmake is very handy.

    [quote author="Exotic_Devel" date="1405550468"]
    As for the '@', did not understand what you say.[/quote]
    Your original Makefiles contained '$@' as a parameter for -o option in g++ and moc. This website parser uses '@' as source code tag and does not produce nice looking Makefile.
    So I replaced all occurrences of '$@' with file names.


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.