Let's get started by following these steps:
- Let's create a Qt Widgets Application project and open up mainwindow.cpp. After that, add the following headers to the top of the source code:
#include <QPushButton>
#include <QGridLayout>
#include <QMessageBox>
#include <QTime>
#include <QDebug>
- Create a QGridLayout object and set its parent as centralWidget:
MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent), ui(new Ui::MainWindow)
{
ui->setupUi(this);
QGridLayout *layout = new QGridLayout(ui->centralWidget);
- Create a QTime object. We will be using this to measure the performance of our next operation:
QTime* time = new QTime;
time->start();
- We will use two loops to fill in 600 push buttons to our grid layout and connect all of them to a lambda function when clicked. We will then measure the elapsed time and print out the result, as follows:
for (int i = 0; i < 40; ++i) {
for (int j = 0; j < 15; ++j) {
QPushButton* newWidget = new QPushButton();
newWidget->setText("Button");
layout->addWidget(newWidget, i, j);
connect(newWidget, QPushButton::clicked,
[this]() {
QMessageBox::information(this, "Clicked", "Button has been clicked!");
});
}
}
qDebug() << "Test GUI:" << time->elapsed() << "msec";
- If we build and run the project now, we will see that a window fills with lots of buttons on it. When we click on one of them, a message box will pop up on the screen. It only took around nine milliseconds on my computer to create and lay out all of the 600 buttons on the main window. There is also no performance issue when we move the window around or resize it, which is quite impressive. It proves that Qt 5 can handle this pretty well. However, please be aware that your users might be using older machines, and you might want to be extra careful when designing your user interface:
- Let's add a style sheet to each of the buttons, like so:
QPushButton* newWidget = new QPushButton();
newWidget->setText("Button");
newWidget->setStyleSheet("background-color: blue; color: white;");
layout->addWidget(newWidget, i, j);
- Build and run the program again. This time, it took roughly 75 milliseconds to set up the GUI. This means that the style sheet does have some impact on the performance of your program:
- Once you are done with that, let's do some performance tests on different types of C++ containers. Open up main.cpp and add the following headers:
#include "mainwindow.h"
#include <QApplication>
#include <QDebug>
#include <QTime>
#include <vector>
#include <QVector>
- Create a testArray() function before the main() function:
int testArray(int count) {
int sum = 0;
int *myarray = new int[count];
for (int i = 0; i < count; ++i)
myarray[i] = i;
for (int j = 0; j < count; ++j)
sum += myarray[j];
delete [] myarray;
return sum;
}
- Create another function called testVector(), as follows:
int testVector(int count) {
int sum = 0;
std::vector<int> myarray;
for (int i = 0; i < count; ++i)
myarray.push_back(i);
for (int j = 0; j < count; ++j)
sum += myarray.at(j);
return sum;
}
- Once you are done with that, proceed to create yet another function called testQtVector():
int testQtVector(int count) {
int sum = 0;
QVector<int> myarray;
for (int i = 0; i < count; ++i)
myarray.push_back(i);
for (int j = 0; j < count; ++j)
sum += myarray.at(j);
return sum;
}
- Inside the main() function, define a QTime object and an integer variable called lastElapse:
int main(int argc, char *argv[]) {
QApplication a(argc, argv);
MainWindow w;
w.show();
QTime* time = new QTime;
time->start();
int lastElapse = 0;
- We will call the three functions we created in the previous steps to test their performance:
int result = testArray(100000000);
qDebug() << "Array:" << (time->elapsed() - lastElapse) << "msec";
lastElapse = time->elapsed();
int result2 = testVector(100000000);
qDebug() << "STL vector:" << (time->elapsed() - lastElapse) << "msec";
lastElapse = time->elapsed();
int result3 = testQtVector(100000000);
qDebug() << "Qt vector:" << (time->elapsed() - lastElapse) << "msec";
lastElapse = time->elapsed();
- Build and run the program now; we will see the performance difference of these containers. On my computer, the array took 650 milliseconds to execute, STL vector took roughly 3,830 milliseconds, and Qt vector took around 5,400 milliseconds to execute. As a result, the array is still the container that yields the best performance, despite its lack of features compared to the other two. Surprisingly, Qt's own vector class works slightly slower than the vector container provided by the C++ standard library.