Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. QTest GUI getting started
Forum Update on Monday, May 27th 2025

QTest GUI getting started

Scheduled Pinned Locked Moved Solved General and Desktop
11 Posts 5 Posters 3.4k Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • J Offline
    J Offline
    jastmc
    wrote on 6 Nov 2017, 11:13 last edited by
    #1

    I am trying to figure out how to use QTest to test my gui application, so I set up a simple program like the one in this posting Qt test empty window. (Using Qt 5.7 on Ubuntu)

    When I try to run it, I get the message tst_mytest.cpp:33: undefined reference to `MyMainWindow::MyMainWindow(QWidget)'*.

    This should be really simple but I can't figure out what it is.

    Thanks,
    Jas

    //tst_mytest.cpp
    
    #include <QString>
    #include <QtTest>
    #include <QCoreApplication>
    #include "../MyApp/mymainwindow.h"
    #include "../MyApp/ui_mymainwindow.h"
    
    #include <QDebug>
    #include <QPushButton>
    
    class MyTest : public QObject
    {
        Q_OBJECT
    
    public:
        MyTest();
    
    private Q_SLOTS:
        void initTestCase();
        void cleanupTestCase();
        void testCase1();
    
    private:
        MyMainWindow* MyMainWindow_ ;
    
    };
    
    MyTest::MyTest()
    {
    }
    
    void MyTest::initTestCase()
    {
        MyMainWindow_ = new MyMainWindow;
        MyMainWindow_->show();
    
    }
    
    void MyTest::cleanupTestCase()
    {
    }
    
    void MyTest::testCase1()
    {
        QVERIFY2(true, "Failure");
    }
    
    QTEST_MAIN(MyTest)
    
    #include "tst_mytest.moc"
    
    //MyTest.pro
    
    #-------------------------------------------------
    #
    # Project created by QtCreator 2017-11-06T10:12:26
    #
    #-------------------------------------------------
    
    QT       += widgets testlib core
    
    TARGET = tst_mytest
    CONFIG   += console
    CONFIG   -= app_bundle
    
    TEMPLATE = app
    
    
    SOURCES += tst_mytest.cpp
    DEFINES += SRCDIR=\\\"$$PWD/\\\"
    
    INCLUDEPATH += $$PWD/../MyApp
    
    //mymainwindow.cpp
    
    #include "mymainwindow.h"
    #include "ui_mymainwindow.h"
    
    MyMainWindow::MyMainWindow(QWidget *parent) :
        QMainWindow(parent),
        ui(new Ui::MyMainWindow)
    {
        ui->setupUi(this);
    }
    
    MyMainWindow::~MyMainWindow()
    {
        delete ui;
    }
    
    void MyMainWindow::on_pushButton_clicked()
    {
        if (ui->checkBox->isChecked())
            ui->checkBox->setChecked(false);
        else
            ui->checkBox->setChecked(true);
    }
    
    //mymainwindow.h
    
    #ifndef MYMAINWINDOW_H
    #define MYMAINWINDOW_H
    
    #include <QMainWindow>
    
    namespace Ui {
    class MyMainWindow;
    }
    
    class MyMainWindow : public QMainWindow
    {
        Q_OBJECT
    
    public:
        explicit MyMainWindow(QWidget *parent = 0);
        ~MyMainWindow();
    
    private slots:
        void on_pushButton_clicked();
    
    private:
        Ui::MyMainWindow *ui;
    };
    
    #endif // MYMAINWINDOW_H
    
    //MyApp.pro
    
    #ifndef MYMAINWINDOW_H
    #define MYMAINWINDOW_H
    
    #include <QMainWindow>
    
    namespace Ui {
    class MyMainWindow;
    }
    
    class MyMainWindow : public QMainWindow
    {
        Q_OBJECT
    
    public:
        explicit MyMainWindow(QWidget *parent = 0);
        ~MyMainWindow();
    
    private slots:
        void on_pushButton_clicked();
    
    private:
        Ui::MyMainWindow *ui;
    };
    
    #endif // MYMAINWINDOW_H
    
    //MySolution.pro
    
    TEMPLATE = subdirs  
    SUBDIRS += MyApp \
        MyTest
    
    
    J 1 Reply Last reply 6 Nov 2017, 11:22
    0
    • J jastmc
      6 Nov 2017, 11:13

      I am trying to figure out how to use QTest to test my gui application, so I set up a simple program like the one in this posting Qt test empty window. (Using Qt 5.7 on Ubuntu)

      When I try to run it, I get the message tst_mytest.cpp:33: undefined reference to `MyMainWindow::MyMainWindow(QWidget)'*.

      This should be really simple but I can't figure out what it is.

      Thanks,
      Jas

      //tst_mytest.cpp
      
      #include <QString>
      #include <QtTest>
      #include <QCoreApplication>
      #include "../MyApp/mymainwindow.h"
      #include "../MyApp/ui_mymainwindow.h"
      
      #include <QDebug>
      #include <QPushButton>
      
      class MyTest : public QObject
      {
          Q_OBJECT
      
      public:
          MyTest();
      
      private Q_SLOTS:
          void initTestCase();
          void cleanupTestCase();
          void testCase1();
      
      private:
          MyMainWindow* MyMainWindow_ ;
      
      };
      
      MyTest::MyTest()
      {
      }
      
      void MyTest::initTestCase()
      {
          MyMainWindow_ = new MyMainWindow;
          MyMainWindow_->show();
      
      }
      
      void MyTest::cleanupTestCase()
      {
      }
      
      void MyTest::testCase1()
      {
          QVERIFY2(true, "Failure");
      }
      
      QTEST_MAIN(MyTest)
      
      #include "tst_mytest.moc"
      
      //MyTest.pro
      
      #-------------------------------------------------
      #
      # Project created by QtCreator 2017-11-06T10:12:26
      #
      #-------------------------------------------------
      
      QT       += widgets testlib core
      
      TARGET = tst_mytest
      CONFIG   += console
      CONFIG   -= app_bundle
      
      TEMPLATE = app
      
      
      SOURCES += tst_mytest.cpp
      DEFINES += SRCDIR=\\\"$$PWD/\\\"
      
      INCLUDEPATH += $$PWD/../MyApp
      
      //mymainwindow.cpp
      
      #include "mymainwindow.h"
      #include "ui_mymainwindow.h"
      
      MyMainWindow::MyMainWindow(QWidget *parent) :
          QMainWindow(parent),
          ui(new Ui::MyMainWindow)
      {
          ui->setupUi(this);
      }
      
      MyMainWindow::~MyMainWindow()
      {
          delete ui;
      }
      
      void MyMainWindow::on_pushButton_clicked()
      {
          if (ui->checkBox->isChecked())
              ui->checkBox->setChecked(false);
          else
              ui->checkBox->setChecked(true);
      }
      
      //mymainwindow.h
      
      #ifndef MYMAINWINDOW_H
      #define MYMAINWINDOW_H
      
      #include <QMainWindow>
      
      namespace Ui {
      class MyMainWindow;
      }
      
      class MyMainWindow : public QMainWindow
      {
          Q_OBJECT
      
      public:
          explicit MyMainWindow(QWidget *parent = 0);
          ~MyMainWindow();
      
      private slots:
          void on_pushButton_clicked();
      
      private:
          Ui::MyMainWindow *ui;
      };
      
      #endif // MYMAINWINDOW_H
      
      //MyApp.pro
      
      #ifndef MYMAINWINDOW_H
      #define MYMAINWINDOW_H
      
      #include <QMainWindow>
      
      namespace Ui {
      class MyMainWindow;
      }
      
      class MyMainWindow : public QMainWindow
      {
          Q_OBJECT
      
      public:
          explicit MyMainWindow(QWidget *parent = 0);
          ~MyMainWindow();
      
      private slots:
          void on_pushButton_clicked();
      
      private:
          Ui::MyMainWindow *ui;
      };
      
      #endif // MYMAINWINDOW_H
      
      //MySolution.pro
      
      TEMPLATE = subdirs  
      SUBDIRS += MyApp \
          MyTest
      
      
      J Offline
      J Offline
      jastmc
      wrote on 6 Nov 2017, 11:22 last edited by
      #2

      @jastmc said in QTest GUI getting started:

      MyMainWindow_ = new MyMainWindow;

      I should have said the error occurs on the line on the line

      MyMainWindow_ = new MyMainWindow;
      
      JonBJ 1 Reply Last reply 6 Nov 2017, 11:50
      0
      • J jastmc
        6 Nov 2017, 11:22

        @jastmc said in QTest GUI getting started:

        MyMainWindow_ = new MyMainWindow;

        I should have said the error occurs on the line on the line

        MyMainWindow_ = new MyMainWindow;
        
        JonBJ Offline
        JonBJ Offline
        JonB
        wrote on 6 Nov 2017, 11:50 last edited by JonB 11 Jun 2017, 15:56
        #3

        [EDIT: please ignore this post, it's irrelevant and wrong. Sorry!]

        @jastmc
        Since the example you quote uses:

        mainWindow_ = new MainWindow();
        

        why do you write

        MyMainWindow_ = new MyMainWindow;
        

        and then ask why it doesn't behave same?

        JonBJ 1 Reply Last reply 6 Nov 2017, 12:31
        0
        • mrjjM Offline
          mrjjM Offline
          mrjj
          Lifetime Qt Champion
          wrote on 6 Nov 2017, 12:08 last edited by
          #4

          @jastmc said in QTest GUI getting started:

          MyMainWindow_ = new MyMainWindow;

          undefined reference to `MyMainWindow::MyMainWindow(QWidget)'*.

          It says it dont know your constructor.

          I would completely delete the build folder, and run qmake + rebuild all.

          If error persists, please check that MyMainWindow.cpp is actual included into the .pro file
          so it gets linked to the tst_mytest project.

          1 Reply Last reply
          1
          • SGaistS Offline
            SGaistS Offline
            SGaist
            Lifetime Qt Champion
            wrote on 6 Nov 2017, 12:24 last edited by
            #5

            Hi,

            In your test, you only build the test sources itself. What about the class you want to test ?

            Interested in AI ? www.idiap.ch
            Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

            1 Reply Last reply
            0
            • JonBJ JonB
              6 Nov 2017, 11:50

              [EDIT: please ignore this post, it's irrelevant and wrong. Sorry!]

              @jastmc
              Since the example you quote uses:

              mainWindow_ = new MainWindow();
              

              why do you write

              MyMainWindow_ = new MyMainWindow;
              

              and then ask why it doesn't behave same?

              JonBJ Offline
              JonBJ Offline
              JonB
              wrote on 6 Nov 2017, 12:31 last edited by
              #6

              @JNBarchan said in QTest GUI getting started:

              @jastmc
              Since the example you quote uses:

              mainWindow_ = new MainWindow();
              

              why do you write

              MyMainWindow_ = new MyMainWindow;
              

              and then ask why it doesn't behave same?

              Whoops? For my own edification, does stinky C++ allow you to omit the () in new Something and call a default constructor? parameterless constructor? complain if multiple constructors? or what??

              mrjjM 1 Reply Last reply 6 Nov 2017, 15:36
              0
              • J Offline
                J Offline
                jastmc
                wrote on 6 Nov 2017, 13:06 last edited by jastmc 11 Jun 2017, 13:07
                #7

                @SGaist OK. How do I make sure the class to be tested is built first?

                Regards
                Jas

                jsulmJ 1 Reply Last reply 6 Nov 2017, 13:48
                0
                • J jastmc
                  6 Nov 2017, 13:06

                  @SGaist OK. How do I make sure the class to be tested is built first?

                  Regards
                  Jas

                  jsulmJ Offline
                  jsulmJ Offline
                  jsulm
                  Lifetime Qt Champion
                  wrote on 6 Nov 2017, 13:48 last edited by
                  #8

                  @jastmc Add it to SOURCES and HEADERS in your pro file.

                  https://forum.qt.io/topic/113070/qt-code-of-conduct

                  1 Reply Last reply
                  0
                  • JonBJ JonB
                    6 Nov 2017, 12:31

                    @JNBarchan said in QTest GUI getting started:

                    @jastmc
                    Since the example you quote uses:

                    mainWindow_ = new MainWindow();
                    

                    why do you write

                    MyMainWindow_ = new MyMainWindow;
                    

                    and then ask why it doesn't behave same?

                    Whoops? For my own edification, does stinky C++ allow you to omit the () in new Something and call a default constructor? parameterless constructor? complain if multiple constructors? or what??

                    mrjjM Offline
                    mrjjM Offline
                    mrjj
                    Lifetime Qt Champion
                    wrote on 6 Nov 2017, 15:36 last edited by
                    #9

                    @JNBarchan
                    yes the new operator/compiler can construct an object if it can find
                    a default constructor (ctor). The ctor is a special function and and that is why () can be omitted even one often consider it just a function call.

                    https://stackoverflow.com/questions/620137/do-the-parentheses-after-the-type-name-make-a-difference-with-new

                    1 Reply Last reply
                    2
                    • J Offline
                      J Offline
                      jastmc
                      wrote on 6 Nov 2017, 15:49 last edited by
                      #10

                      @jsulm Thanks, that was part of it.

                      I then had to add

                      INCLUDEPATH += $$PWD/../MyApp/ \
                                      ../../build-MySolution-Desktop_Qt_5_7_0_GCC_64bit-Debug/MyApp/ \
                      

                      to the MyTest.pro file because the next message was that it could not find the ui_mymainwindow.h file. So that solved it.

                      Regards,
                      Jas

                      1 Reply Last reply
                      1
                      • SGaistS Offline
                        SGaistS Offline
                        SGaist
                        Lifetime Qt Champion
                        wrote on 6 Nov 2017, 20:07 last edited by
                        #11

                        You are missing the FORMS variable in your .pro file.

                        Interested in AI ? www.idiap.ch
                        Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

                        1 Reply Last reply
                        1

                        1/11

                        6 Nov 2017, 11:13

                        • Login

                        • Login or register to search.
                        1 out of 11
                        • First post
                          1/11
                          Last post
                        0
                        • Categories
                        • Recent
                        • Tags
                        • Popular
                        • Users
                        • Groups
                        • Search
                        • Get Qt Extensions
                        • Unsolved