Skip to Content
Hands-On GUI Programming with C++ and Qt5
book

Hands-On GUI Programming with C++ and Qt5

by Lee Zhi Eng
April 2018
Intermediate to advanced
404 pages
8h 27m
English
Packt Publishing
Content preview from Hands-On GUI Programming with C++ and Qt5

Unit testing in Qt

Qt comes with a built-in unit testing module, which we can use by adding the testlib keyword to our project file (.pro):

QT += core gui testlib 

After that, add the following header to our source code:

#include <QtTest/QtTest> 

Then, we can start testing our code. We must declare our test functions as private slots. Other than that, the class must also inherit from the QOBject class. For example, I created two text functions called testString() and testGui(), like so:

private slots: 
   void testString(); 
   void testGui(); 

The function definitions look something like this:

void MainWindow::testString() { QString text = "Testing"; QVERIFY(text.toUpper() == "TESTING"); } void MainWindow::testGui() { QTest::keyClicks(ui->lineEdit, ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Start your free trial

You might also like

C++ GUI Programming with Qt 4

C++ GUI Programming with Qt 4

Jasmin Blanchette, Mark Summerfield

Publisher Resources

ISBN: 9781788397827Supplemental Content