Implementing Qt project through CMake

Your script has several errors, also a few things can be improved. After changes it will be looks like: cmake_minimum_required(VERSION 3.0.2) project(MyProject) find_package(Qt5Widgets) set(CMAKE_AUTOMOC ON) set(CMAKE_AUTOUIC ON) set(CMAKE_INCLUDE_CURRENT_DIR ON) add_library(mainwindow mainwindow.cpp) target_link_libraries (mainwindow Qt5::Widgets) add_executable(MyProject main.cpp) target_link_libraries (MyProject mainwindow) Errors: Wrong add_executable directive. You try to add a library, but for this purpose you need … Read more

Access C++ function from QML

For any C++ code to be called from QML, it must reside inside a QObject. What you need to do is create a QObject descended class with your function, register it to QML, instantiate it in your QML and call the function. Note also that you have to mark your function with Q_INVOKABLE. Code: #ifndef … Read more

How to convert a QJsonObject to QString

Remembering when I first needed to do this, the documentation can be a bit lacking and assumes you have knowledge of other QJson classes. To obtain a QString of a QJsonObject, you need to use the QJsonDocument class, like this: – QJsonObject jsonObj; // assume this has been populated with Json data QJsonDocument doc(jsonObj); QString … Read more

Set label text in QTabWidget tab

It sounds like you’re talking about Qt Designer, since it defaults to showing two tabs (called “Tab 1” and “Tab 2”) when you add a QTabWidget through the interface. If so, click on the tab you want to rename, then in the Property Editor (if you can’t find it make sure it’s visible by using … Read more

Efficient off-screen rendering of QPainterPaths (OpenGL and non-OpenGL solution required)

The document of Qt-interest Archive, August 2008 QGLContext::create() says: A QGLContext can only be created with a valid GL paint device, which means it needs to be bound to either a QGLWidget, QGLPixelBuffer or QPixmap when you create it. If you use a QPixmap it will give you software-only rendering, and you don’t want that. … Read more

Qt – Determine absolute widget and cursor position

As already said you should rather use targetWidget->geometry() instead of contentsRect() in this special case. Next I wonder which class the code you posted belongs to. The method QWidget::mapToGlobal() should be invoked from the QWidget your coordinates are relative to. If I got you right, it should look like something like this: QRect widgetRect = … Read more

Changing DPI scaling size of display make Qt application’s font size get rendered bigger

High DPI support is enabled from Qt 5.6 onward. Setting QGuiApplication::setAttribute(Qt::AA_EnableHighDpiScaling) in your application source code allows automatic high-DPI scaling. NOTICE: To use the attribute method, you must set the attribute before you create your QApplication object: #include <QApplication> int main(int argc, char *argv[]) { QApplication::setAttribute(Qt::AA_EnableHighDpiScaling); QApplication app(argc, argv); return app.exec(); }