Basics
Hello world
User input
While loop
If statement
For loop
Switch statement
Read file using ifstream
Write to a file using ofstream
Containers
Basic usage of std::list
Basic usage of std::map
Basic usage of std::vector
Data type conversion
Concurrency
Hello world in thread
Windows API with Exmaples
DeviceIoControl
FindFirstVolumeW
GetDiskFreeSpaceExW
GetLogicalDrives
GetLogicalDriveStrings
C++/WinRT
C++/WinRT Hello World
QNX
Using timer interrupt
Qt Cross-platform
Installing Qt Creator 9.0.1 on Windows 10
Qt console hello world
Qt Widget hello world
Qt Widget hello world code only
Keywords: C++, Qt, Programming, Tutorial
Wednesday, October 01, 2025
QNX: Running a function at specific interval using timer interrupt
#include <iostream>
#include <csignal>
#include <ctime>
#include <cerrno>
#include <cstring>
#include <unistd.h>
#include <sys/time.h>
#include <time.h>
#include <stdio.h>
void timer_handler(int signo) {
struct timeval tv;
gettimeofday(&tv, NULL);
char buf[100];
struct tm* nowtm;
nowtm = localtime(&tv.tv_sec);
strftime(buf, sizeof(buf), "%Y-%m-%d %H:%M:%S", nowtm);
printf("Timer expired at %s.%03ld\n", buf, tv.tv_usec / 1000);
}
int main() {
timer_t timerid;
struct sigevent sev;
struct itimerspec its;
struct sigaction sa;
// Install the timer handler
sa.sa_flags = 0;
sa.sa_handler = timer_handler;
sigemptyset(&sa.sa_mask);
if (sigaction(SIGRTMIN, &sa, NULL) == -1) {
std::cerr << "sigaction: " << errno << std::endl;
return -1;
}
// Create the timer
sev.sigev_notify = SIGEV_SIGNAL;
sev.sigev_signo = SIGRTMIN;
if (timer_create(CLOCK_REALTIME, &sev, &timerid) == -1) {
std::cerr << "timer_create: " << errno << std::endl;
return -1;
}
// Set the timer to expire every second
its.it_value.tv_sec = 1;
its.it_value.tv_nsec = 0;
its.it_interval.tv_sec = 1;
its.it_interval.tv_nsec = 0;
if (timer_settime(timerid, 0, &its, NULL) == -1) {
std::cerr << "timer_settime: " << errno << std::endl;
return -1;
}
// Main loop
while (true) {
pause(); // Wait for signals
}
return 0;
}
Sunday, September 21, 2025
Qt console hello world
Qt console hello world
This guide shows how a Qt console hello world looks like.
▣ Open Qt Creator and click on Create Project...
▣ Create Qt Console Application by following the section from @1 to
@3.
▣ In Project Location window provide a project name, i.e.,
HelloWorldConsole▣ In Define Build System select CMake
▣ In Translation File, take the default values
▣ In Kit Selection, select the toolchain kit to be used. Below shows only one which is MinGW 64-bit.
▣ In Project Management, take the default.
▣ In line 6, I have added an instruction to output Hello World in to the console. Also changed to return 0 to exit the application as event loop is not needed.
▣ Gist source code shown below for convenience
In Qt Creator 17.0.1/Qt 6.9.2, need to add QTextStream
▣ Running it using Qt Creator is well and good but we probably want to run
it external to the IDE. To deploy the binary with all the dependencies, open
the compiler environment used to build the application. In the case above we
have used Qt 6.4.1 (MinGW 11.2.0 64-bit).
▣ Run the following command (change the paths as necessary)cd \ md tmp cd tmp copy C:\Users\t\Documents\build-HelloWorldConsole-Desktop_Qt_6_4_1_MinGW_64_bit-Debug\HelloWorldConsole.exe windeployqt HelloWorldConsole.exe
▣ The folder structure should look like this. It also shows a sample out
from the application.
Keywords: CPP, Qt, Hello World
Sunday, January 22, 2023
C++/WinRT Hello World
C++/WinRT Hello World
This is a quick start quide to prepare a development environment to develop against C++/WinRT.System:
Development OS: Windows 10 Pro 22H2
Target: Windows 10 LTSB build 10.0.14393
IDE: Visual Studio 2022 (used 17.4.4)
Install Visual Studio 2022 Universal Windows Platform development workload. The following is very _important_ as this is what will allow you to create C++/WinRT project. Check C++ (v143) Universal Windows Platform tools, doing so will also enable and install corresponding SDK. Set back and relax as this will download ~24GB of data from Microsoft.
After installing Visual Studio, create Windows Console Application (C++/WinRT) For the Minimum version, select Windows 10 Anniversary Update (10.0; Build 14393)
The template should look like below If all is well, running it should look like below
Keywords: Visual Studio, C++/WinRT
Saturday, January 21, 2023
Getting the last error code while debugging even if the Win32 API returned value was not saved
Getting the last error code while debugging even if the Win32 API returned value was not saved
It is not uncommon that a Win32 API call return value is not checked in code. This information can prove to be very invaluable while debuging. Fortunately, Visual Studio saves pseudovariables which includes the value normally returned by GetLastError function. To view what would have returned by GetLastError use $err,hr in the Watch window. Refs:Visual Studio Pseudovariables
Tags: Visual Studio, CPP
Wednesday, December 28, 2022
Qt Widget hello world code only
Qt Widget hello world code only
This post shows how to create a Hello World Qt Widget application in pure code, not using the UI Designer.▣ Create a console application - consult this link for a quick guide.
▣ Change main.cpp like below
▣ Update CMakeList.txt like below
Saturday, December 24, 2022
Installing Qt Creator 9.0.1 on Windows 10
Installing Qt Creator 9.0.1 on Windows 10
This is a quick guide on installing Qt Creator 9.0.1 on Windows 10. As of December 24, 2022, the latest available online installer is qt-unified-windows-x64-4.5.1-online.exe. Binaries can be downloaded from https://www.qt.io/download, I was using Qt for open source use.What follows are the screen captures to get working installation of Qt Creator (Qt 6.4.1).
![]() |
| Accept open source obligations |
![]() |
| Welcome to open source Qt setup |
![]() |
| Contribute to Qt development |
![]() |
| Installation Folder - Custom installation |
![]() |
| Select Components - Use default, add "MinGW 11.2.0 64-bit" |
![]() |
| License Agreement |
![]() |
| Start Menu Shortcuts |
![]() |
| Ready to Install |
![]() |
| Installing |
![]() |
| Finished |
Keywords: Qt Creator, CPP
Installing Qt Creator 4.11.x on Windows 10
This is a quick guide on installing Qt Creator 4.11.x on Windows 10.
* Download the QT Online Installer (as of March 2020 it can be downloaded from here).
* Run qt-unified-windows-x86-3.2.2-online.exe and follow the installation wizard like below
Keywords: Qt Creator, MinGW 7.3.x
* Download the QT Online Installer (as of March 2020 it can be downloaded from here).
* Run qt-unified-windows-x86-3.2.2-online.exe and follow the installation wizard like below
![]() |
| Welcome screen |
![]() |
| Create an account or login |
![]() |
| Accept Qt Open Source obligations |
| Setup Qt |
| Contribute to Qt Create User Experience Development |
| Select installation folder |
| Select Components - Default selection |
| Select MinGW 7.3.0 32-bit and MinGW 7.3.0 64-bit |
| Under Developer and Designer Tools - check MinGW 7.3.0 32-bit/64-bit |
| Accept License Agreement |
| Start Menu Shortcuts |
| Ready to Install |
| Installing Qt |
| Creating Maintenance Tool |
| Completing the Qt Wizard |
| Qt Creator! |
Thursday, August 04, 2022
CPP: Windows: GetDiskFreeSpaceExW
CPP: Windows: GetDiskFreeSpaceExW
Retrieves information about the amount of space that is available on a disk volume, which is the total amount of space, the total amount of free space, and the total amount of free space available to the user that is associated with the calling thread.Notes:
- Tested using Visual Studio 2022 (17.2.6) C++ Windows Console template
Reference(s):
https://docs.microsoft.com/en-us/windows/win32/api/fileapi/nf-fileapi-getdiskfreespaceexw
CPP: Windows: FindFirstVolumeW
CPP: Windows: FindFirstVolumeW
Retrieves the name of a volume on a computer. FindFirstVolume is used to begin scanning the volumes of a computer. See line 85 for sample usageNotes:
- Tested using Visual Studio 2022 (17.2.6) C++ Windows Console template
Reference(s):
https://docs.microsoft.com/en-us/windows/win32/api/fileapi/nf-fileapi-findfirstvolumew
Sunday, July 24, 2022
CPP: Windows: DeviceIoControl
CPP: Windows: DeviceIoControl
An application can use the DeviceIoControl function to perform direct input and output operations on, or retrieve information about, a floppy disk drive, hard disk drive, tape drive, or CD-ROM drive.The following example demonstrates how to retrieve information about the first physical drive in the system. It uses the CreateFile function to retrieve the device handle to the first physical drive, and then uses DeviceIoControl with the IOCTL_DISK_GET_DRIVE_GEOMETRY control code to fill a DISK_GEOMETRY structure with information about the drive.
Notes:
- Tested using Visual Studio 2022 (17.2.6) C++ Windows Console template
Reference(s):
https://docs.microsoft.com/en-us/windows/win32/devio/calling-deviceiocontrol
Saturday, July 23, 2022
CPP: Windows: GetLogicalDriveStrings
CPP: Windows: GetLogicalDriveStrings
Fills a buffer with strings that specify valid drives in the system.Notes:
- Tested using Visual Studio 2022 C++ Windows Console template
Reference(s):
https://docs.microsoft.com/en-us/windows/win32/api/fileapi/nf-fileapi-getlogicaldrivestringsw
Sunday, July 17, 2022
CPP: Windows: Get logical drives
CPP: Windows: Get logical drives
Retrieves a bitmask representing the currently available disk drives.Notes:
- Tested using Visual Studio 2022 C++ Windows Console template
Reference(s):
https://docs.microsoft.com/en-us/windows/win32/api/fileapi/nf-fileapi-getlogicaldrives
Wednesday, March 04, 2020
CPP: Basic usage of std::cin for getting input from stdin
CPP: Basic usage of std::cin for getting input from stdin
Basic usage of getting data from standard input (keyboard normally) using std::cin.
To build:
Run it:
Basic usage of getting data from standard input (keyboard normally) using std::cin.
To build:
$: clang++ getinput.cpp
Run it:
$: ./a.out
Thursday, February 13, 2020
CPP: Simple data type conversion
Convert string to int:
To build:
Run it:
To build:
$: clang++ dataconv.cpp
Run it:
$: ./a.out
Friday, February 07, 2020
CPP: Basic usage of std::vector
Very basic usage of C++ STL std::vector
To build:
Run it:
To build:
$: clang++ stdvector.cpp
Run it:
$: ./a.out
Wednesday, February 05, 2020
CPP: Basic usage of std::map
Very basic usage of C++ STL std::map
To build:
Run it:
To build:
$: clang++ stdmap.cpp
Run it:
$: ./a.out
Tuesday, February 04, 2020
CPP: Using std::list
Very basic usage of C++ STL std::list
To build:
Run it:
To build:
$: clang++ stdlist.cpp
Run it:
$: ./a.out
Wednesday, January 29, 2020
CPP: Write to a file using ofstream
To build:
$: clang++ ofstream.cpp
Run it:
$: ./a.out
Read the file:
$: cat file.txt
Subscribe to:
Posts (Atom)






























