A C++ desktop application with a Qt6 GUI that connects to a PostgreSQL database using libpqxx.
cpp-gui-postgres/
├── src/
│ └── main.cpp # Entry point
├── gui/
│ ├── mainwindow.h
│ └── mainwindow.cpp # Qt6 main window
├── database/
│ ├── db.h
│ └── db.cpp # PostgreSQL connection (libpqxx)
├── models/
│ └── user.h
├── repositories/
│ ├── userrepository.h
│ └── userrepository.cpp
├── services/
│ ├── userservice.h
│ └── userservice.cpp
├── config/
│ ├── env.h
│ └── env.cpp # .env file loader
├── .env.example # Environment variable template
├── CMakeLists.txt
└── .vscode/
├── c_cpp_properties.json
└── settings.json
Copy the example env file and fill in your PostgreSQL credentials:
cp .env.example .env # Linux / macOS
copy .env.example .env # Windows (Command Prompt)Edit .env:
DB_HOST=YOUR_DB_HOST
DB_PORT=YOUR_DB_PORT
DB_USER=YOU_DB_USER
DB_NAME=YOUR_DB_NAME
DB_PASSWORD=YOUR_DB_PASSWORDsudo apt install cmake g++ libpqxx-dev qt6-base-devQt headers are typically located at /usr/include/x86_64-linux-gnu/qt6.
Open .vscode/c_cpp_properties.json and ensure includePath includes:
{
"configurations": [
{
"name": "Linux",
"includePath": [
"${workspaceFolder}/**",
"/usr/include/x86_64-linux-gnu/qt6",
"/usr/include/x86_64-linux-gnu/qt6/QtWidgets",
"/usr/include/x86_64-linux-gnu/qt6/QtCore",
"/usr/include/x86_64-linux-gnu/qt6/QtGui"
],
"defines": [],
"compilerPath": "/usr/bin/gcc",
"cStandard": "c17",
"cppStandard": "gnu++17",
"intelliSenseMode": "linux-gcc-x64"
}
],
"version": 4
}mkdir build
cd build
cmake ..
cmake --build .
./app/bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)"brew install cmake libpqxx qtFind your Qt prefix path:
brew --prefix qtOpen .vscode/c_cpp_properties.json and update includePath (replace <qt-prefix> with the output above):
{
"configurations": [
{
"name": "Mac",
"includePath": [
"${workspaceFolder}/**",
"<qt-prefix>/include",
"<qt-prefix>/include/QtWidgets",
"<qt-prefix>/include/QtCore",
"<qt-prefix>/include/QtGui"
],
"defines": [],
"compilerPath": "/usr/bin/clang",
"cStandard": "c17",
"cppStandard": "gnu++17",
"intelliSenseMode": "macos-clang-x64"
}
],
"version": 4
}mkdir build
cd build
cmake .. -DCMAKE_PREFIX_PATH=$(brew --prefix qt)
cmake --build .
./appThe
-DCMAKE_PREFIX_PATHflag tells CMake where to find Qt since Homebrew doesn't install it to a system path.
- Visual Studio 2022 — select the "Desktop development with C++" workload during install
- CMake — check "Add CMake to system PATH" during install
- Qt6 — use the Qt Online Installer, select Qt 6.x.x > MSVC 2019/2022 64-bit
- PostgreSQL — includes headers and
libpqneeded bylibpqxx - libpqxx — must be built from source (see step 2)
Open Developer Command Prompt for VS and run:
git clone https://github.com/jtv/libpqxx.git
cd libpqxx
mkdir build && cd build
cmake .. -DCMAKE_BUILD_TYPE=Release -DPostgreSQL_ROOT="C:\Program Files\PostgreSQL\<version>"
cmake --build . --config Release
cmake --install . --prefix "C:\libpqxx"Replace <version> with your installed PostgreSQL version (e.g., 16).
Open Developer Command Prompt for VS from the project root:
mkdir build
cd build
cmake .. ^
-DCMAKE_PREFIX_PATH="C:\Qt\6.x.x\msvc2022_64" ^
-Dpqxx_DIR="C:\libpqxx\lib\cmake\libpqxx" ^
-DPostgreSQL_ROOT="C:\Program Files\PostgreSQL\<version>"
cmake --build . --config ReleaseReplace C:\Qt\6.x.x\msvc2022_64 with your actual Qt installation path.
cd Release
app.exeMake sure
.envis placed one directory above the executable (build\.env) so the app can find it at../.env.
Open .vscode/c_cpp_properties.json and update with your actual Qt and libpqxx paths:
{
"configurations": [
{
"name": "Win32",
"includePath": [
"${workspaceFolder}/**",
"C:/Qt/6.x.x/msvc2022_64/include",
"C:/Qt/6.x.x/msvc2022_64/include/QtWidgets",
"C:/Qt/6.x.x/msvc2022_64/include/QtCore",
"C:/Qt/6.x.x/msvc2022_64/include/QtGui",
"C:/libpqxx/include"
],
"defines": ["_DEBUG", "UNICODE", "_UNICODE"],
"windowsSdkVersion": "10.0.22621.0",
"compilerPath": "cl.exe",
"cStandard": "c17",
"cppStandard": "c++17",
"intelliSenseMode": "windows-msvc-x64"
}
],
"version": 4
}