Qt - Reddit
15 subscribers
280 photos
46 videos
4.22K links
News and discussion for the Qt Framework.

Subreddit: https://www.reddit.com/r/QtFramework

Powered by : @r_channels & @reddit2telegram
Download Telegram
Why you can't just "forget" raw pointers in Modern C++/Qt (and why smart pointers might crash your app)

Modern C++ (11/14/17/20...) has a very clear mantra: *"Forget* `new` *and* `delete`*, use smart pointers."* It’s excellent advice for pure C++. But the moment you open the Qt documentation, raw pointers (`T*`) start jumping at you from every page.

Why is it that even in 2026, you can't write a serious Qt application using exclusively smart pointers? Let’s look at where "smart" code actually breaks the framework’s logic.

# 1. The Ownership Conflict: Parent-Child vs. Smart Pointers

Qt’s core feature is the **Object Tree**. When you pass a parent to a `QObject` constructor (like a `QWidget`), you are delegating memory management to that parent.

**The "Double Free" Trap:**

Imagine trying to be "modern" and using `std::unique_ptr` (or `QScopedPointer`) where a parent already exists:

void createUI(QWidget* parent) {
// Ownership is passed to parent, BUT unique_ptr also thinks it's the owner
auto label = std::make_unique<QLabel>("Hello", parent);
// ... logic ...
} // unique_ptr goes out of scope and calls delete.

**What goes wrong?**

1. `unique_ptr` honestly deletes the object when the function ends.
2. However, the `parent` still keeps a pointer to this label in its internal `children()` list.
3. When the `parent` is eventually destroyed (e.g., closing the window), it tries to delete the already-deleted label.
4. **Result:** Crash / Double Free.

Smart pointers implement static or reference-counted ownership. They simply aren't aware of `QObject::parent()`.

# 2. The Chaos of GUI Lifecycles and QPointer

In GUI apps, object lifetimes are often unpredictable. A button might be deleted because a tab was closed, a dialog was dismissed, or a server signal was received.

If you need to store a reference to an object you *don't* own:

* `std::shared_ptr` won't help (it will keep the object alive when it should have died).
* `std::weak_ptr` only works with `std::shared_ptr`.

Enter `QPointer<T>`. This is Qt’s unique "weak" pointer. It doesn't own the object, but it **automatically turns to** `null` when the target `QObject` is deleted (via `delete` or by its parent). It does this by subscribing to the `destroyed()` signal. You can't achieve this safety with standard smart pointers without fighting the framework.

# 3. QML and the JavaScript Garbage Collector Trap

If you use QML, the pointer situation gets even weirder. When you pass a `QObject*` from C++ to QML, `QQmlEngine::ObjectOwnership` kicks in.

If a C++ object has no parent (`parent == nullptr`), the JavaScript engine might decide **it** owns the object. When the JS Garbage Collector (GC) sees the object isn't used in QML anymore, it deletes it. Your C++ pointer becomes a dangling "bomb" that will crash your app later. Smart pointers are powerless here; they can't control the JS GC.

# 4. Performance and Low-Level API

When working with `QImage::bits()` or OpenGL contexts, you need raw memory access.

uchar* scanline = image.scanLine(y);
for(int x = 0; x < width; ++x) {
// Direct memory access is the only way to hit 60 FPS
scanline[x] = process(scanline[x]);
}

Any smart pointer overhead inside such loops would kill the performance benefits of C++.

# The "Qt Memory Management" Cheatsheet

To avoid losing your mind between two systems, follow these rules:

1. **Creating a QObject with a Parent?** Use a raw pointer. The parent will handle it. `new QLabel("Text", this)` is perfectly fine.
2. **Creating a QObject WITHOUT a Parent?** Use `std::unique_ptr` or `QScopedPointer`. This prevents leaks.
3. **Need to track an object you don't own?** Use `QPointer`. It’s the only safe way to check if a widget is still alive.
4. **Non-QObject (Business Logic)?** Use `std::unique_ptr` (ownership) or `std::shared_ptr` (shared resources).
5. **Passing objects to QML?** Always set a parent or explicitly call `QQmlEngine::setObjectOwnership(obj, QQmlEngine::CppOwnership)`.
6. **Multi-threading?** Use `QSharedPointer`. It’s
the "native" Qt way to handle thread-safe ref-counting.

# Conclusion

Writing a Qt app is a balance between classical RAII and hierarchical ownership. Trying to use *only* smart pointers is a fight against the framework. Understand where Qt takes responsibility and where it leaves it to you, and your code will stay crash-free.

**TL;DR:** Qt's parent-child system and QML's garbage collector manage memory in ways that conflict with `std::unique_ptr`. Raw pointers are still "first-class citizens" in Qt, provided you understand the ownership rules.

https://redd.it/1ss960j
@qt_reddit
A question regarding Qt for Linux, GCC vs pure Clang ambiguity.

This is regarding edge case scenarios related to correctness.

Official 'Qt for Linux' guide doesn't mention clang or LLVM anywhere. It's all GCC in there.

Are the Qt libraries carefully designed around the GCC specific compiler intrinsics and shits and GNU's implementation of Standard Template Library that it can not be safely built with the pure Clang toolchain and used for serious use cases?

Pure clang is significantly faster to compiler, it can integrate with even faster 3rd party linkers as well. Binaries are noticeably smaller due to libc++ being 10x smaller.

I mean does the Qt have some kind of personal relationship with libstdc++ that we can't link it with libc++?

https://redd.it/1ssl9pz
@qt_reddit
PySide 6.11 doesn't build on macos

When trying to build PySide 6.11 it always gives this issue

[ 14%\] Building CXX object PySide6/QtCore/CMakeFiles/QtCore.dir/PySide6/QtCore/qdirlisting_wrapper.cpp.o

pyside-setup-everywhere-src-6.11.0/build/pyside6/PySide6/QtCore/PySide6/QtCore/qdirlisting_wrapper.cpp:152:86: error: no type named 'Default' in 'QDirIterator::IteratorFlag'

::QFlags<QDirIterator::IteratorFlag> cppArg1(QDirIterator::IteratorFlag::Default);

\~\~\~\~\~\~\~\~\~\~\~\~\~\~\~\~\~\~\~\~\~\~\~\~\~\~\~\~\^

pyside-setup-everywhere-src-6.11.0/build/pyside6/PySide6/QtCore/PySide6/QtCore/qdirlisting_wrapper.cpp:168:86: error: no type named 'Default' in 'QDirIterator::IteratorFlag'

::QFlags<QDirIterator::IteratorFlag> cppArg2(QDirIterator::IteratorFlag::Default);

\~\~\~\~\~\~\~\~\~\~\~\~\~\~\~\~\~\~\~\~\~\~\~\~\~\~\~\~\^

pyside-setup-everywhere-src-6.11.0/build/pyside6/PySide6/QtCore/PySide6/QtCore/qdirlisting_wrapper.cpp:232:44: error: no viable conversion from 'QFlags<IteratorFlag>' to 'QFlags<QDirIterator::IteratorFlag>'

QFlags<QDirIterator::IteratorFlag> cppResult = const_cast<const ::QDirListing *>(cppSelf)->iteratorFlags();

Shiboken6 and Shiboken6 generator build fine it only gives this error when building PySide6

On linux it built smoothly

Any ideas on what could be wrong?

Versions:

shiboken6= 6.11.0

Pyside6 = 6.11.0

Qt = 6.11.0

Compiler = tested with AppleClang 14, 16, 17

LLVM = tested with llvm14, llvm18 and llvm22

Macos 14 x86_64 and macos 26 arm64

Even following every step from the official guide results in the same error

https://doc.qt.io/qtforpython-6/building_from_source/macOS.html

https://redd.it/1sus0hu
@qt_reddit
Qt Creator + CMake on Windows is driving me insane (need help)

I’m honestly losing my mind over this and I’m hoping someone here can tell me what I’m doing wrong (or confirm that this setup is just cursed). I’m required to use Qt Creator (Qt 6.11, MinGW 64-bit) for a university course. We’re just supposed to build simple C++ programs (console + later small GUI/game stuff). Nothing crazy. But the environment is completely unstable for me.

Typical experience; I create a new project, first run works fine. Second run... suddenly: “No executable specified in run configuration” or CMake completely breaks (wrong paths, cache pointing to old directories). If I move or rename the project folder ONCE, everything is broken. Build folders seem to corrupt everything. CMakeCache points to directories that don’t even exist anymore and Qt Creator sometimes doesn’t show files correctly or ignores changes. On top of that: Windows (Smart App Control / Defender) randomly blocks my compiled .exe files so, I constantly have to unblock or disable things just to run my own code

Things I’ve already tried: reinstalling Qt multiple times, using different kits (MinGW, LLVM, sticking to MinGW now), deleting build folders and reconfiguring, creating fresh projects from scratch. And still after a short time its broken again.

What I actually want is a simple workflow where I can write code, press build, run program, That’s it.

My questions are:

1. How do you actually use Qt Creator + CMake without it constantly breaking?
2. Is it normal that you basically must NEVER move a project because of CMake?
3. How do you deal with Windows security blocking your own compiled executables?

At this point I’m spending way more time fighting the tooling than actually programming, and it’s killing all motivation. I just dont know what to try anymore and I just want this to work.

Any help or advice would be massively appreciated 🙏

https://redd.it/1sv8ikw
@qt_reddit
Weird QFont and QLabel behaviors

Hi everyone,

I ran into a font rendering inconsistency between QLabels with weird conditions.

Here's a minimal example of my app UI. I thought it was only on the app I'm currently working on, but it seems it's the same in this project too.

So I'm using Segoe UI font (which comes by default on Win 11, what I'm currently on) and I set it to my QApplication, and then retrieve it in MainWindow setupUi method, increment the pixel/point size and set it to my QLabels, but the text rendering is incorrect:

Red underline is added with Paint

The weird part is when I comment the first title setFont here, the rest renders correctly:

Red and green lines added with Paint, first title setFont is commented

Did anyone run is a similar situation or am I missing something about QFont usage, or some clash with QLabels ? Advice or even deeper explanations are more than welcome.

Thanks in advance

https://redd.it/1sxphau
@qt_reddit
Missing add definition in cpp

Since the latest update (17.0.0) I've been missing the option to generate a function in the cpp file, only the option to generate Outside or Inside class are showing, and it's driving me kinda crazy, I rolled back to 16.0.2 but it still happens

https://preview.redd.it/u9htik5idrcf1.png?width=274&format=png&auto=webp&s=82882e700575782c07137274b39fd8b095ddc221




https://redd.it/1lzby8f
@qt_reddit
The current state of QT3D and whether it's worth using

It's still included in the package—it's very useful, but it's marked as deprecated. I'd like to use it for a game built with QT; it's convenient because I'd also like to use it within a widget in my level editor, and that can be done in Python rather than QML.

https://redd.it/1t1f2iy
@qt_reddit
Questions about client-server app idea

I have an idea for an client-server app that consist of:

1 .A "client" for a touch screen device (e.g. tablet) that shows a bunch of "widgets" written in qml that interfaces with so called "modules", consisting of C++ code that send messages to the host

2. A "host app" that manages the client UI and receives messages from client that also has "modules" that interface with host's apps.

An example is a media controls "module" on both host and client and its respecting "widget" that is the UI on the client.

I have a few goals for this idea:

1. There can be different "widgets" that interface with a client module. A single widget can be full screen and can interface with multiple client modules.

2. Host modules expose a preferences menu to the host app to configure .

I have some questions about implementing this idea.

1. Should I separate the backend and frontend (ui) for the host app, where the backend does not use qt? This approach allows multiple frontends using different toolkits, but since i am going to use qml for the client ui anyway and client modules have to use qt specifics, i was thinking otherwise.

2. Should I have host modules not rely on qt specific stuff (slots, properties) for the logic, or use it? This helps with achieving the idea from question 1.

3. Boost.asio or qt network for communication between both host and client?

4. Client modules using c++ or qml based interface that interfaces with both widgets and the client code (especially sending messages to host)?

https://redd.it/1t1ol0a
@qt_reddit
Media is too big
VIEW IN TELEGRAM
Wrote 360 Video Player in Qt (PySide6) and PyOpenGL

https://redd.it/1t1xcpr
@qt_reddit
AstroCore G-Code Sender - Qt 6 - looking for help
https://redd.it/1t2iawb
@qt_reddit
CodePointer version 0.1.4 - new C++ IDE/editor

May release (named Fist my bump) is released. While the changelog is not large, a new huge feature was used: tree-sitter support. This means that C++ completion is "kinda" usable, with semantic support. It will fail in many edge cases (as the code is just parsed not evaluated), only for your code (not system libraries) - but it is very helpful.

One useful feature of this IDE is its low memory footprint, currently I see its using bellow 300 MB while 2 projects are loaded on Linux (Windows has similar memory footprint):

 364884 AppRun           /home/diego/Downloads/codepointer-v0.1.4-x86_64.AppImage                       5 diego       269M ⣀⣀⣀⣀⣀  0.0  

CodePointer editing its own main.cpp


Binary packages are in https://github.com/codepointerapp/codepointer/releases/tag/v0.1.4

Code is available at:
https://github.com/codepointerapp/codepointer
https://gitlab.com/codepointer/codepointer/



https://redd.it/1t2tkel
@qt_reddit
This media is not supported in your browser
VIEW IN TELEGRAM
Glitchy/shaky movement of text/items when rendered on a display with fractional scaling

https://redd.it/1t37zoz
@qt_reddit
Need advices on beginning programming on Qt

Hello, I intend to create GUI apps and improve myself on QT

I'm an "self-taught" C++ programmer ... I mean, I followed and watched a lot of videos on C++ programming to understand class, OOP, variables, pointers, namespaces, functions, pointers, heritage...

Problem is that I'm not a student from computer or programming universities... and so, now, I'm stuck because I don't know which way I need to follow to become autonomous on QT ...

Also, english is not my native langage, it's also another issue I have to face in my learning projects...

Question is, what should I do ?

Is there a free formation to follow ? Is there a path I should respect to progress ?

I mean, I have difficulties in the simple fact of creating by myself a QPushbutton in mywindow.cpp because I want to try without using the .ui file...

I feel so lost, and incompetent even in simple C++ ...

Thanks in advance for your feedbacks ;)

https://redd.it/1t3yku6
@qt_reddit
How to apply dark fusion on Linux ?

Hello,

I'd like to have the native dark fusion but it's not seem to even exist. Is somebody have an code example to have the dark fusion on linux ?

thanks!

https://redd.it/1t4bh5x
@qt_reddit
Self promotion A WIP PySide6 Book

I have gathered a fair amount of PySide6 examples and last year decided to turn them into a book. The book is intended cover basic Qt widgets usage, as well as general Qt topics like timers, properties, events and object trees, and some intermediate topics like model-view programming and multithreading, and some special topics like databases, networking, processes and state machines. Currently I have \~470 pages of code and text that still need quite a bit of polish.

All the material I have written so far is available as a free pdf sample and you can download it from here:

https://leanpub.com/pyside6blueprints/

The code itself is available on Github:

https://github.com/JoeAnonimist/pyside6-blueprints-code/tree/master/code

Since the last time I posted in this subreddit, I have added several new chapters to the book:

Ch 10. Tree Widgets
Ch 13. Dialogs
Ch 21. Properties
Ch 24. QAbstractItemModel (model/view)
Ch 25. Delagates (model/view)
Ch 26. Sorting, Filtering and Selection (model/view)
Ch 33. Databases
Ch 34. Processes

I would be grateful for any reviews and comments. Tia.



(addressing the sub guidelines)

1. Your content is high-quality: The book covers a range of Qt Widgets topics. All examples are self-contained, each followed with step-by-step instructions and code walkthroughs.

2. Your content is reasonably complete: The book has \~470 pages with five or six chapters yet to be added.

3. Your content is specifically about helping beginners learn programming: Most of the book is beginner-friendly with introductory units for

Qt Widgets
QMainWindow
Model/view programming
Multithreading







https://redd.it/1t57ubv
@qt_reddit
Self promotion A WIP PySide6 Book

I have gathered a fair amount of PySide6 examples and last year decided to turn them into a book. The book is intended to cover basic Qt widgets usage, as well as general Qt topics like timers, properties, events and object trees, some intermediate topics like model-view programming and multithreading, and some special topics like databases and processes. Currently I have \~470 pages of code and text that still need quite a bit of polish.

All the material I have written so far is available as a free pdf sample that you can download from here:

https://leanpub.com/pyside6blueprints/

The code itself is available on Github:

https://github.com/JoeAnonimist/pyside6-blueprints-code/tree/master/code

Since the last time I posted in this subreddit, I have added several new chapters to the book:

10. Tree Widgets
13. Dialogs
21. Properties
24. QAbstractItemModel (model/view)
25. Delagates (model/view)
26. Sorting, Filtering and Selection (model/view)
33. Databases
34. Processes

I would be grateful for any reviews and comments. Tia.



(addressing the sub guidelines)

1. Your content is high-quality: The book covers a range of Qt Widgets topics. All examples are self-contained, each followed with step-by-step instructions and code walkthroughs.

2. Your content is reasonably complete: The book has \~470 pages with five or six chapters yet to be added.

3. Your content is specifically about helping beginners learn programming: Most of the book is beginner-friendly with introductory units for

Qt Widgets
QMainWindow
Model/view programming
Multithreading

https://redd.it/1t57ubv
@qt_reddit
How to get the color value from a specific point of a gradient?

I have column elements with multiple rectangle child's inside of it, and i want to rectangle to have color such that the column as a whole when viewed gets a gradient effect. For this i though of creating a gradient element but the how would i get the color value from a specific point on the gradient and assign it to the rectangle.

ColumnLayout{

property double gradientPosition: (say 0.75; but is a variable)

Repeater{

model: 10

delegate: Reactangle{

color: gradient color of the value at 0.75

}

Gradient{

gradientStop: {position: 0.0; color: "green"}

gradientStop: {position: 0.5; color: "yellow"}

gradientStop: {position: 1; color: "red"}

}


i can answer your question if i failed to describe my problem accurately.

https://redd.it/1t724rj
@qt_reddit