Quantcast
Channel: Planet Qt
Viewing all 15410 articles
Browse latest View live

Qt – basysKom: Qt OPC UA updates

$
0
0

Qt OPC UA updates

The Qt OPC UA module has been ported to CMake and will be part of Qt 6 right from the first release.
In addition to numerous bug fixes and improved test coverage, the open62541 plugin has been updated to open62541 v1.1 and uses OpenSSL for security support, thus removing the dependency on mbedTLS.

Continue reading Qt OPC UA updates at basysKom.


The Qt Company Blog: Qt Creator 4.13.1 released

$
0
0

We are happy to announce the release of Qt Creator 4.13.1 !

The Qt Company Blog: HMI Development with Model-Based Design using Qt Design Studio and Simulink

$
0
0

"In traditional design processes, the design information is usually transferred and handled in the form of text-based documents, which are difficult to understand and subject to interpretation bias. Engineers create embedded code and data manually from text-based documents, leading to a time consuming and error-prone process. There is also little scope to ensure that changes are implemented correctly or not.

The market demands embedded products that are highly customizable, life-long maintainable, recyclable and that can be disassembled, as well as which no longer follow any traditional design process models.

For embedded control and algorithm designers, the focus is on modeling, which has always been an essential part of the design process. The model-based design is a prominent change in embedded system development. In this context, when MBD is used effectively, it provides a single design platform to optimize overall system design. It helps embedded software developers to understand the difference between simulator and software development tool in order to create simulation models and check whether algorithms will work before the embedded code is written. Through virtual prototyping, system engineers can easily see whether the whole system (mechanical, electrical, and embedded software) will work as intended, even before the hardware is manufactured and available for testing." [1].

What is Simulink?

Simulink® is a multi-domain modeling and simulation environment for engineers and scientists who design controls, wireless, and other dynamic systems (Figure 1). Its primary interface is a graphical editor for modeling all components of a system and a customizable set of pre-built blocks for modeling algorithms and physical systems.

Building a Simulink model for a system consists of selecting the appropriate blocks and connecting them together in a way that enables the desired functionality. Adding blocks happens by dragging and dropping them from the Simulink library to the desired location on the model building stage. Furthermore, a connected set of blocks can be encapsulated into a component that provides a layer of abstraction that facilitates creating complex models.

Figure 1: Simulink

Qt Design Studio 1.6

The new Qt Design Studio 1.6 release comes with built-in Simulink communication capabilities.A simulation model created using Simulink can interact with a QML application developed using Qt Design Studio. Data can flow in both directions between the 2 parties. This allows complete separation between the data (Simulink model) and its visual representation (QML app). This new feature is available for our commercial users and is still experimental. As of now, only Windows is supported and Simulink Qt blockset works with MATLAB version 2019b. In the next release we will support all MATLAB versions starting from version 2015b up to the latest. Also, we will improve the usability and features based on user feedback.

The Qt Company Blog: Qt World Summit 2020 agenda released! More news on Qt 6, straight from the source.

$
0
0
For the first time, #QtWS20 has gone virtual! The online event gathers designers, developers, and technology managers to learn about the latest developments in Qt, meet partners and exchange ideas in a virtually 3D-rendered Palm Springs. Join us and get motivated on how you cancreate even higher performance UIs, connect those devices, and impress your boss and customers with ultimate user experiences both now and in the future.

KDAB on Qt: KDE’s Akademy 2020 – A Quick Summary

$
0
0

What is Akademy?

Akademy is the yearly conference for the KDE community, which is a community devoted to creating free software for desktop and mobile.  Typically, Akademy takes place in a different city each year.  However, due to the pandemic, the conference was online this time around.  September 4-11 marked the dates of Akademy 2020.

KDAB and KDE

KDAB and KDE have a very close relationship. Around 40% of KDAB employees are or have been KDE contributors. In fact, some have been contributing for more than a decade.  So, KDAB has been sponsoring KDE’s Akademy for quite a while.

Overview of Akademy 2020

The first day of the conference, September 4, included a full day of training sessions for KDE contributors. KDAB taught 4 of the 6 training sessions: UI/UX Design in QML for Desktop, Debugging & Profiling on Linux, Introduction to QML, and Multithreading in Qt. These were reduced, half-day versions of our popular training offerings.

Throughout the weekend, the conference brought forth several talks. These covered topics such as QML, static analysis, Qt on Android, licensing, and even how to write better software for dentists and hospitals.

Most of the weekdays featured more work-focused meetings, involving round-table discussion. In addition to that, we managed to have time for community-building activities, like escape rooms and a quiz.

Finally, on the last day of the week, the talk schedule returned with important topics. A couple of the topics the talks covered on this day were maintaining a good work/life balance and knowledge retention within the organization when contributions cease, for instance.

Praise for KDE

To conclude, we want to congratulate KDE for such a successful conference and thank them for the help they offered us with organizing Qt Desktop Days (which happened at the same time of KDE’s Akademy, from September 7-11).  Thank you, KDE!  Keep up the excellent work!

KDE's Akademy

About KDAB

If you like this blog and want to read similar articles, consider subscribing via our RSS feed.

Subscribe to KDAB TV for similar informative short video content.

KDAB provides market leading software consulting and development services and training in Qt, C++ and 3D/OpenGL. Contact us.

The post KDE’s Akademy 2020 – A Quick Summary appeared first on KDAB.

Qt – Life of a Developer: Felgo in the QML Book

The Qt Company Blog: Improved QML Support in Qt for Python 6.0

$
0
0

Since the initial port of PySide to Qt5 (a.k.a PySide2), the interaction with QML was on the list of features we wanted to fully support in our set of bindings, due to the popularity of QML.

With the first official release 5.12, we had cover many use cases for the QML and Python interaction, but also we left out a couple of use cases that were really required by our community. Now that we are developing new features and improvements for Qt6, we wanted to address most of them.

Here you can find a summary of the features that we have done so far for the planned Qt for Python 6.0 release.

Felgo: Release 3.6.0: Modal Dialogs and iOS Storyboard Support for Qt & Felgo

$
0
0

Felgo 3.6.0 adds the new AppModal component to display modal dialogs in your apps. You can use this component for both full-screen and custom-height modal dialogs.
On iOS, you can now use Storyboard files to create your app launch screen. This update also adds new components and several improvements and fixes.


Francesc M. Qt Blog: How to pin tabs in QTabWidget

Latest from LearnPyQt: PyQt5/PySIde2 Book Create GUI Applications with Python & Qt5 (4th Edition) now available

$
0
0

Hello! This morning I released a new update to my PyQt5 book Create GUI Applications, with Python & Qt5. This is an enormous update, expanding it from 258 to 665 pages and adding 211 complete code examples.

To celebrate the milestone the book is available this week with 20% off. Readers get access to all future updates for free, so it's a great time to snap it up!

render_pyqt5_small.png

This is the 4th Edition of the book and adds a number of entirely new chapters including MVC-like model views, SQL database-views, plotting with matplotlib & PyQtGraph, custom widgets & bitmap graphics, concurrent programming with threads and processes and theming Qt applications.

Existing chapters have been expanded with more examples and step-by-step guides to using them. All source code shown in the book is available as standalone, runnable, examples to try out and experiment with.

book-pages.png

Chapters have been updated and expanded with new examples and more detailed explanations and diagrams. Cross-platform screenshots show how your application will look on different systems. Starting from the basic principles of GUI development in Python and building up to more complex topics.

modelviews.png

Learn how to use Qt's MVC-like model views architecture to sync data with widgets, including querying and editing SQL databases from within your applications.

themes.png

Tired of the default application look? Use styles, palettes and Qt Style Sheets to completely customize the look and feel of your applications.

Examples of customised bars.

Discover how to create your own widgets to use in your Qt applications. Starting from the basics of bitmap graphics and building up to a fully-customizable widgets.

concurrent.png

Use threads an processes to perform long-running calculations or communicate with remote services, without locking up your applications. Receive data back from threads and processes and display progress bars. Stop and pause running jobs from your UI.

plotting.png

Visualize data inside your applications, using matplotlib or PyQtGraph. Sync plots with external threads to create live updating dashboards to visualize data in real-time.

There is also a PySide2 edition of the book available, which features largely the same content, with examples converted to "Qt for Python". Purchasing either book gets you access to both editions, including any future updates of either.

Feedback, as always is very welcome!

Get the PyQt5 book
Get the PySide2 book

Francesc M. Qt Blog: New GitQlient v1.1.1 (+ Mac)

$
0
0

There is available a patch for the version 1.1 of GitQlient.

The new patch version solves a problem with new repositories. The problem prevented users to work with repositories that have no commits. That applies to both new repositories (via init new repo) and already created repos that don’t have any commits.

I took advantage of this issue to release also a release for OSX. It’s as functional as I would expect to have it in the next 1.2.0. However, I’d like if people can test it before going into the next release.

The binaries

You can find the binaries for GitQlient v1.1.1 on the release section on the GitHub repo:

New GitQlient v1.1.1 (+ Mac) first appeared on My C++ & Qt blog - Cesc M..

pat26 MARGINALIA blog: Qt & Android: Job Scheduler

$
0
0

Description

This tutorial describes how to schedule tasks in a Qt app for Android using Android's JobScheduler API. If your Android app needs to perform a repetitive task whether the app is active or not, you should consider creating an Android app with a JobService to perform the work.  You can add one or more job services, where each one defines a different task that is performed in the future based upon specified conditions.  The tasks can even survive application restarts and device reboots.


Source code

The following highlights the steps needed to create an Android app using Qt and Android JobScheduling API.  The complete sample app is found in Github at https://github.com/pgulotta/JobServiceExample.

Getting Started 

Create a new Qt Quick empty project naming it JobServiceExample.  The app will link against the Qt Android Extras module, so add this line to the project file:  

   android:QT += androidextras

 

This sample app displays a message box, so you need to add entry to support the QMessageBox class. 

   QT += widgets 


Create Android Package Templates files. This can be easily done with QtCreator. If you are creating the Templates manually and you need assistance, refer to the Qt for Android  documentation. 


Verify the app was generated correctly by building all Android ABI's and deploying your new app onto an  Android device or emulator.  Upon successfully running your new Android app, it's time to make the changes needed to create a job scheduling service. 

 

Update AndroidManifest.xml

In the AndroidManifest.xml, name the package following the normal java package naming conventions. The JobServiceExample package name is com.example.jobserviceexample. The AndroidManifest.xml file should contain an entry similar to this.

  

     xmlns:android="http://schemas.android.com/apk/res/android" 

     android:versionName="100" android:versionCode="100" android:installLocation="auto">


There are several other changes to make to the AndroidManifest.xml and now is as good a time as any.  The job scheduling api requires a minimum android version.  Specify the supported Android sdk, with an entry such as this.

  


In the AndroidManifest.xml file , the JobServiceExample must be declared as a service with the BIND_JOB_SERVICE permission.  The name JobServiceExample is the name of the Java class you will be creating.

  

     android:permission="android.permission.BIND_JOB_SERVICE"/>


Because Android will run the job, per the job's defined schedule, after the system has finished booting, the RECEIVE_BOOT_COMPLETED permission is needed.

  


The sample app scheduled task is to append a line of text composed of a time stamp to a file periodically.  Thus, the permission WRITE_EXTERNAL_STORAGE is required.

  


Please refer to the sample app, if you are unsure as to what the AndroidManifest.xml entries are.


JobScheduler Java API

An Android service is a component that runs in background and has no user interface. The service will continue to run even if the application exits.  The work or task you are scheduling is known as a 'job' and is defined in a Java class which enables Android to perform the work, even when the app is not active. Refer to Android documentation for details. 


Java files belong to the package specified in the AndroidManifest.xml file.  Java files called by Qt application must be placed in a directory which conforms to the path hierarchy defined by the package name.  Create a java class called JobServiceExample.class which extends android.app.job.JobService in the directory ...JobServiceExample/android/src/com/example/jobserviceexample. This Java class is an Android Service that extends the Android JobService class.  Since JobServiceExample class extends the JobService class, a couple of methods must be implemented: onStartJob(), which is called by the system when the job has begun executing, and onStopJob(), which is called by the system if the job is cancelled before finishing. Note, JobServiceExample class runs on the main thread so the task should be run on a  worker thread. 


   @Override

   public boolean onStartJob( JobParameters jobParameters )

  {

     Log.i( TAG, "JobServiceExample.onStartJob : jobParameters.getJobId() = " +

       jobParameters.getJobId() );

     try {

       Thread thread = new Thread( new ExampleWork( this ) );

       thread.start();

       thread.join();

     } catch ( Exception e ) {

       e.printStackTrace();

     }

     return false;  // Returns false from when job has finished. onStopJob will not be invoked

   }


   @Override

   public boolean onStopJob( JobParameters jobParameters )

   {

     // This method is typically not invoked

     Log.i( TAG, "JobServiceExample.onStopJob : jobParameters.getJobId() = " +

       jobParameters.getJobId() );

     return false;  // Returns false to end the job entirely

   }



The class ExampleWork specified above implements Runnable.  For this example, the task is to append a line of text composed of a timestamp to a file.


   @Override

   public void run()

   {

     ...

    doWork( mContext );

     ...

   }

 

public static void doWork( Context context )

 {

  try {

    SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

    String textToAppend = dateFormat.format(new Date());

    File path = Environment.getExternalStoragePublicDirectory( 

      Environment.DIRECTORY_DOWNLOADS );

    String filename =  path.getAbsolutePath() + File.separatorChar + 

    "JobServiceExampleLog.txt";

    Log.i( TAG, "ExampleWork.doWork path =" + filename + " appending text " + textToAppend);

    BufferedWriter writer = new BufferedWriter(new FileWriter(filename, true));

    writer.newLine();

    writer.write(textToAppend);

    writer.close();

  } catch ( IOException e ) {

    e.printStackTrace();

  }

 }


Schedule a job by using Android's JobScheduler java class. Using this class, Android can efficiently batch your job with other jobs that need network access. JobScheduler can request retries and when needed Android will rescheduled the work; the work is guaranteed.  In the sample project JobServiceExample, the class ExampleJobScheduler illustrates scheduling.  A unit of work is encapsulated by a java JobInfo class specifying the scheduling criteria. The JobInfo.Builder class is used to configure how the scheduled task should run.


   public static void scheduleJob (Context context, int intervalinMS )

  {

     handleJob(context, intervalinMS );

  }

   private static void handleJob (Context context, long intervalinMS)

   {

     ...

     ComponentName serviceComponent = 

     new ComponentName( context, JobServiceExample.class );

     ...

     JobScheduler jobScheduler = context.getSystemService( JobScheduler.class );

     ...

     JobInfo.Builder builder = new JobInfo.Builder( JOB_ID, serviceComponent );

     ...

     builder.setPeriodic( intervalinMS );  // job runs within the intervalinMS; API 21

     builder.setPersisted( true ); // persist this job across device reboots; API 21

     builder.setRequiredNetworkType( JobInfo.NETWORK_TYPE_ANY ); //  API 21

     builder.setRequiresDeviceIdle(false); //  API 21

     int result = jobScheduler.schedule( builder.build() );

     String resultText = ( JobScheduler.RESULT_SUCCESS == result ) ? 

     "successfully" : "failed";

     Log.i ( TAG, "ExampleJobScheduler.handleJob scheduled for intervalinMS of " + 

     intervalinMS + " is "  + resultText );

    ...

  }



QML

In this example app, the QML UI, main.qml,  allows the user to schedule how frequently the task is executed.

  ...

  Button {

   text: qsTr("Apply")

   anchors.horizontalCenter: parent.horizontalCenter

   onClicked: Controller.scheduleJobService(scheduleModelId.

     get(scheduleSelectorId.currentIndex).intervalMS)

   }

   ...



Qt & C++

The FrontController C++ class  exposes the job  scheduling function to the QML interface.


Q_INVOKABLE void scheduleJobService(int intervalinMS);

 ...

   void FrontController::scheduleJobService( int intervalinMS )

   {

     QAndroidJniObject::callStaticMethod

     ( "com/example/jobserviceexample/JobServiceExample","scheduleJobService",

       "(Landroid/content/Context;I)V",

       QtAndroid::androidActivity().object(), intervalinMS);

   }


The Permissions C++ class is called when the application starts for check for and request of needed permissions.

   void Permissions::requestExternalStoragePermission()

   {

     ...

     QtAndroid::PermissionResult request = QtAndroid::checkPermission( 

       "android.permission.WRITE_EXTERNAL_STORAGE" );

     if ( request == QtAndroid::PermissionResult::Denied ) {

       QtAndroid::requestPermissionsSync( QStringList() <<  

       "android.permission.WRITE_EXTERNAL_STORAGE" );

       request = QtAndroid::checkPermission( 

       "android.permission.WRITE_EXTERNAL_STORAGE" );

       if ( request == QtAndroid::PermissionResult::Denied ) {

         mPermissionGranted = false;

         if ( QtAndroid::shouldShowRequestPermissionRationale( 

         "android.permission.READ_EXTERNAL_STORAGE" ) ) {

            QAndroidJniObject 

            ( "com/example/jobserviceexample/ShowPermissionRationale",

            "(Landroid/app/Activity;)V",

            QtAndroid::androidActivity().object());

            QAndroidJniEnvironment env;

            if ( env->ExceptionCheck() ) {

              env->ExceptionClear();

        }

     }

   } else {

    mPermissionGranted = true;

   }

  } else {

   mPermissionGranted = true;

  }

  ...

}


The communication between the C++ Qt/QML and Java needs to be specified  in main.cpp.

   #include

   #include "frontcontroller.h"

   #include "permissions.hpp"


   int main(int argc, char *argv[])

   {

   ...

   QQmlApplicationEngine engine;

   FrontController frontController{app.parent()};

   engine.rootContext()->setContextProperty( "Controller", &frontController );

   const QUrl url(QStringLiteral("qrc:/main.qml"));

   QObject::connect(&engine, &QQmlApplicationEngine::objectCreated,

    &app, [url](QObject *obj, const QUrl &objUrl) {

      if (!obj && url == objUrl)

        QCoreApplication::exit(-1);

     }, Qt::QueuedConnection); engine.load(url);


    Permissions permissions;

    permissions.requestExternalStoragePermission();

    if ( permissions.getPermissionResult() ) {

      qInfo( "Successfully obtained required permissions, app starting" );

      return app.exec();

    } else {

      qWarning( "Failed to obtain required permissions, app terminating" );

    }

  }


Test it Out

Upon successfully building the app, run the JobServiceExample app and schedule a job by selecting the "Recording interval" and pressing "Apply".  Quit the app.  On Android, the file /storage/emulated/0/Download/JobServiceExampleLog.txt will be updated at the specified time interval.  Upon rebooting the Android, you can observe, the file /storage/emulated/0/Download/JobServiceExampleLog.txt will continue to be updated at the specified time interval.  The sample app, JobServiceExample, logs often to help you follow along.

     



Conclusion

To code a job scheduler in your QT Android application, there are many small steps, but this is true with all Android app development.  Android job scheduling  is a powerful, performant, robust feature that enables the Android OS to shoulder the burden of executing tasks based upon specific conditions. It's a nice tool to have in your toolbox.






Lorn Potter: 'wasm memory too small' Qt for WebAssembly

$
0
0
Sometimes when I am building a larger project with Qt for WebAssembly, I get this type of message:

wasm-ld: error: initial memory too small, 17553264 bytes needed

and the build fails.

This means that you need to tell Emscripten compiler to allocate more than the standard 1GB initial memory.
Qt allows you to specify to add more initial memory by using QMAKE_TOTAL_MEMORY in your pro file.

So it makes sense to add something like this:

QMAKE_TOTAL_MEMORY=17553264

BUT the result is:

shared:ERROR: For wasm, TOTAL_MEMORY must be a multiple of 64KB, was 17553264

grrrr...  ok, what if we find a multiple of 64?

17553264 / 64 = 274,269.95

We need a whole number, so lets round up to the next whole number.

274270 * 64 = 17553280

But no. That doesn't work either:

shared:ERROR: For wasm, TOTAL_MEMORY must be a multiple of 64KB, was 17553280

WTH?!?

The answer is that a KB is 1024 bytes, so 64 * 1024 = 65536 bytes

So let's find the closest whole number multiple of 64 KB (65536)

17553264 / 65536 = 267.841...

so let's try 268

268 * 65536 = 17563648

QMAKE_TOTAL_MEMORY=17563648

and this now builds, and hopefully runs!


You can also read more about Mobile and Embedded development and Qt for WebAssembly in the book Hands-On Mobile and Embedded Development with Qt 5

Francesc M. Qt Blog: Non closable QDialog

$
0
0

This is quite beginner post but I believe it’s good to share what we learn through experience with the people that are starting. In this entry I’ll show you how to have a UI non closable QDialog but that it can be close from the code.

In GitQlient I’m using this dialog to inform the user that the repository is being loaded. There is some data I’m loading in the cache/background (branches, tags, etc) that can take a bit if the repository is massive. In my case, I’m working with some repos that are quite big (~10 Gb only text files) and old. People usually forget to remove their branches and right know I can see around 2000 branches and close to 500 tags. In that case an async load is needed so the user has to wait.

How to do a non closable QDialog?

We want to prevent the user to close the dialog in all it’s forms:

  • Disabling the window close icon.
  • Disabling the Esc key that in QDialog closes/cancels the dialog.

Acting on the windows title

The way to do that is quite simple. In the first place we hide the close button:

setWindowFlags(windowFlags() & ~Qt::WindowCloseButtonHint);

This will make the close button gone and keep the resize behaviour and dialog title. If you want a dialog only for the text you can do:

setWindowFlags(Qt::FramelessWindowHint);

That will remove the title bar and all it’s components.

Method 1: Acting on the Esc key event

QDialog process the Esc key as close/cancel. Since we don’t want that the user closes our dialog, we need to take care of that behaviour. To do that we need to capture the key press event and avoid processing it:

void WaitingDlg::keyPressEvent(QKeyEvent *e)
{
   if (e->key() == Qt::Key_Escape)
      return;

   QDialog::keyPressEvent(e);
}

The code is quite simple, we check the pressed key and if it’s the one we’re expecting, we don’t process the event.

Additionally, it’s also useful to have full control of another event: the closeEvent. That method plus a custom close method will give us the full behaviour:

void WaitingDlg::closeEvent(QCloseEvent *e)
{
   if (!mPrepareToClose)
      e->ignore();
   else
      QDialog::closeEvent(e);
}

void WaitingDlg::close()
{
   mPrepareToClose = true;

   QDialog::close();
}

In this code I’m using a class member to store when I do want to close the dialog, preventing the dialog to close if it wasn’t me how said it. The result is that this dialog can only be close by calling the close() method.

Method 2: Override reject method

Suggested in the comments by NickD2039 another possibility is to override the reject method, that would prevent the end user from closing the dialog. The code posted has a closeNow method that acts in a similar way to the method 1, it activates a control variable that finall calls accept.

I kind of like a bit more this code, since it’s a bit more elegant and requires you to write less code:

void closeNow()
{
   mPrepareToClose = true;
   accept();
   mPrepareToClose = false;
}

void accept()
{
   if (mPrepareToClose)
      QDialog::accept();
}

void reject()
{
   if (mPrepareToClose)
      QDialog::reject();
}

Where to find the source coude?

You can find the source code for the non closable dialog in my GitHub repository for Qt tips. Please noticed that I name it WaitingDlg since I did the code for GitQlient and that was its purpose.

Non closable QDialog first appeared on My C++ & Qt blog - Cesc M..

KDAB on Qt: How to Build C++ Projects with the Address Sanitizer on Windows

$
0
0

Memory bugs are tricky. Leaks have a clear impact on performance, and quickly become hard to spot when heap allocated objects move too much. Memory access bugs, on the other hand, make your program crash right away, if you’re lucky. In less desirable cases, they may end up corrupting random objects on the heap, and only end up in a crash much later. Or even worse, leading to undetected undefined behaviours or wrong computations.

This is the reason why there are a number of standalone tools (e.g. valgrind, Dr.Memory), as well as compiler extensions (e.g. AddressSanitizer, MemorySanitizer, LeakSanitizer) to help with memory bug detection.

On Windows, and especially using Qt, things are a bit harder, as valgrind is not an option and Dr.Memory often crashes with Qt applications. Unless you use WSL, this only leaves compiler tools on the table.

Memory Sanitizers on Windows

First of all, what are our choices when it comes to using memory sanitizers on Windows?

There are two options:

  1. with Visual Studio’s cl compiler
  2. with LLVM’s clang/clang-cl compilers (also available through Visual Studio)
    • {x86,amd64}/Release builds only – AddressSanitizer only

So using AddressSanitizer (from now on ASAN) is the only viable option for memory bug detection with memory sanitizers on Windows. Also, since the support for the cl compiler is still incomplete, in this post we will be using clang.

Furthermore, we will be using Visual Studios’s bundled clang to make it possible to generate Visual Studio solutions. If you’re using your own clang installation, you should update the following paths accordingly. You can find Visual Studio 2019’s clang in the following directories:

  • For x86: %ProgramFiles(x86)%\Microsoft Visual Studio\2019\Professional\VC\Tools\Llvm\bin
  • For x64: %ProgramFiles(x86)%\Microsoft Visual Studio\2019\Professional\VC\Tools\Llvm\x64\bin

Note that you will need to add the right directory manually to the PATH when using the command line, as there is no vcvars script available for clang tools.

From now on I’m going to assume for all paths that we’re using Visual Studio 2019 Professional and we’re building 64 bit applications.

Compile an executable with the static C++ runtime (/MT)

If you build and link in one go, it is enough to compile with -fsanitize=address

clang-cl -fsanitize=address main.cpp

When doing compilation and linking in separate steps, we need an extra step to provide the ASAN runtime explicitly.

clang-cl -fsanitize=address -c main.cpp -o main.o
# Call vcvars64 here so lld-link will be able to link all the default libraries
lld-link main.o -libpath:"%ProgramFiles(x86)%\Microsoft Visual Studio\2019\Professional\VC\Tools\Llvm\x64\lib\clang\10.0.0\lib\windows" ^
    -wholearchive:clang_rt.asan-x86_64.lib -wholearchive:clang_rt.asan_cxx-x86_64.lib

The linker will implicitly link the program against the static CRT (/MT) and with the static version of clang’s ASAN runtime. Note the use of -wholearchive to force the compiler to include all the symbols from the library, avoiding optimizations.

If the program consumes a library which is also being sanitized, then said library (which should be linked against the dynamic CRT with /MD) should be linked against clang_rt.asan_dll_thunk-x86_64.lib. In this way, the library (or libraries) will use the shadow memory of the statically linked ASAN runtime in the executable.

Compile an executable with the dynamic C++ runtime (/MD)

Again, it is easy to compile and link in one go:

clang-cl -fsanitize=address /MD main.cpp

But now the executable should use a different ASAN runtime when linked:

clang-cl -fsanitize=address -c main.cpp -o main.o
# Call vcvars64 here so all the default libraries are added to lld-link
lld-link main.o -libpath:"%ProgramFiles(x86)%\Microsoft Visual Studio\2019\Professional\VC\Tools\Llvm\x64\lib\clang\10.0.0\lib\windows" ^
    clang_rt.asan_dynamic-x86_64.lib -wholearchive:clang_rt.asan_dynamic_runtime_thunk-x86_64.lib

Note that only clang_rt.asan_dynamic_runtime_thunk-x86_64.lib needs the -wholearchive flag here.

All the included dlls that are also sanitized (which should always be using the dynamic CRT) should be linked against the same ASAN runtime libraries as the executable.

Since clang_rt.asan_dynamic-x86_64.lib is an import lib pointing to clang_rt.asan_dynamic-x86_64.dll, when running a sanitized executable (or loading a sanitized lib) its folder should be in the PATH. You can find the dll alongside the .lib in %ProgramFiles(x86)%\Microsoft Visual Studio\2019\Professional\VC\Tools\Llvm\x64\lib\clang\10.0.0\lib\windows.

Using ASAN with CMake

If you want to use ninja or any other generator other than Visual Studio, just point CMake to your clang-cl installation and add the necessary flags to your targets. Adjust your flags for plain clang if you don’t need or don’t care about clang-cl.

add_executable(exec main.cpp)
target_compile_options(exec PRIVATE -fsanitize=address) # /MD will be used implicitly
target_link_directories(exec PRIVATE "${ENV:PROGRAMFILES(X86)}/Microsoft Visual Studio/2019/Professional/VC/Tools/Llvm/x64/lib/clang/10.0.0/lib/windows")
target_link_libraries(exec PRIVATE clang_rt.asan_dynamic-x86_64 clang_rt.asan_dynamic_runtime_thunk-x86_64)
target_link_options(exec PRIVATE /wholearchive:clang_rt.asan_dynamic_runtime_thunk-x86_64.lib)

CMake will always need to be invoked with -DCMAKE_BUILD_TYPE=Release otherwise the compilation will fail.

To generate a Visual Studio solution, you need to pass a few extra arguments to CMake:

  • Generate a Visual Studio solution: -G "Visual Studio 16 2019"
  • Choose between 32 or 64 bit target: -A {Win32,x64}
  • Use the clang toolkit: -T ClangCL

Or, if you are using Visual Studio Code just select the right entry from the kit list (e.g. Clang 10.0.0 for MSVC with Visual Studio Professional 2019 Release (amd64)).

Conclusions

Using tools to track down memory bugs helps saving time and effort, especially on complex projects. On Windows, the available tooling is limited, and a step by step documentation on how to use what’s available may be hard to come by. With this blog post, it will be hopefully easier for you to leverage the AddressSanitizer on Windows and keep memory bugs in your projects under control.

 

If you’d like to learn more about the tooling available for C++ developers working with Microsoft Windows on the desktop, you might be interested in our Debugging and Profiling C++ Applications on Windows training.

 

About KDAB

If you like this blog and want to read similar articles, consider subscribing via our RSS feed.

Subscribe to KDAB TV for similar informative short video content.

KDAB provides market leading software consulting and development services and training in Qt, C++ and 3D/OpenGL. Contact us.

The post How to Build C++ Projects with the Address Sanitizer on Windows appeared first on KDAB.


KDAB on Qt: Choosing a CPU

$
0
0

When building an embedded systems product, among your earliest decisions is the choice of hardware. More specifically, on what CPU should you base your design? Today’s system-on-chip processors have a huge array of resources to contribute to your solution: multiple cores and on-board DSPs, graphics engines and display controllers, peripheral support and connectivity interfaces, and more. Because a new hardware platform entails a costly investment it makes sense to consider your hardware selection wisely. This whitepaper examines the i.MX 8 processor to see if it makes sense for your project.

The post Choosing a CPU appeared first on KDAB.

KDAB on Qt: Choosing a Software Stack

$
0
0

One of the most difficult choices when starting any new software project is selecting the programming language and framework your team will use to create it. Should you stick with Qt because it’s the best tool for the job? Should you switch to something using web- based technology or designed explicitly for mobile? This whitepaper can help you structure your decision-making process around the software options that might be the best fit for you and give you the necessary guidance to head in that direction.

The post Choosing a Software Stack appeared first on KDAB.

KDAB on Qt: Creating Python bindings for Qt libraries

$
0
0

Python is a handy all-purpose language.  It can make you very productive within a short time period and has powerful expressiveness for data manipulation and processing. Yet, it’s not a great fit for lots of tasks. C++ is far better at achieving anything that needs bare metal performance, deterministic timing, or low-level access. Thankfully, some great tools are available that make it relatively easy to create Python bindings that let Python functions call into C++ code.

In this blog, we’re going to explain the process of creating Python bindings for your Qt library using one of our own open source Qt libraries as an example, KDDockWidgets. You can use the same process to create Python bindings for plain C++ (non-Qt) libraries, too. Once you see how straightforward the process is, you’ll want to add Python accessibility for all of your existing libraries. Consequently, a whole new community of programmers will be able to use them. And if you want a working example to download, you can get KDDockWidgets off GitHub.

Getting started

KDDockWidgets is our open source Qt-based library for adding a sophisticated docking system to Qt applications. (It’s got a number of advanced features that are missing from QDockWidgets.) Making this module accessible from Python requires a number of steps:

  1. Installing PySide. Download the PySide2 package, which gives you access to the Qt5 APIs and data structures, in other words, Qt for Python.
  2. Pointing CMake to PySide2. Create configuration files that tell CMake how to find the new PySide2 libraries and binaries. (The entire process of creating Python bindings uses CMake – check out our CMake whitepaper if you need a primer.)
  3. Creating Shiboken invocation. Build a CMake file that calls the Shiboken with all the proper flags and files. Shiboken is the code generation tool that actually creates our bindings.
  4. Creating a type system file. Create a file that exposes the classes that need to have bindings generated.
  5. Creating a header for classes. Make a simple header file that includes all the C++ classes that the Shiboken tool will need to access during the generation process.
  6. Customizing binding make. Add the CMake code to execute shiboken2 on your C++ headers and source.
  7. Creating a Python module. Create a file that accesses the Python binding classes, allowing them to be used as a standard Python module.
  8. Compiling bindings. Auto-generate and compile the Python and C++ wrappers that transition between the two languages.
  9. Testing the Python package. Try it out and make sure it works!

While this looks like a lot of work, each step is pretty simple, and the tools will do most of the work. Let’s take a more detailed look at each step.

1. Installing PySide

You can do this pretty easily with the Python package installer; however, you must replace [Qt-Version] with the version of Qt you’re using.

~$ python3 -m pip3 install --index-url=http://download.qt.io/snapshots/ci/pyside/[Qt-Version]/latest/ shiboken2-generator pyside2 --trusted-host download.qt.io

The “gotcha” here is that an incompatible version of PySide2 and Qt can cause crashes. For example, if you’re building on Qt 5.15, you need to use this:

~$ python3 -m pip3 install --index-url=http://download.qt.io/snapshots/ci/pyside/5.15/latest/ shiboken2-generator pyside2 --trusted-host download.qt.io

You have to install the Pyside2 package directly from the repository. That’s necessary since the standard pip package doesn’t contain the Shiboken application that generates bindings, while the repository version does.

(If you’re planning on binding a plain C++ library rather than a Qt-specific C++ library, you can eliminate the pyside2 in the above step.)

Note that although Shiboken was created to handle generic C++, it has been tested primarily against Qt libraries. That means that if your library uses language features normally absent from Qt code – such as complex templates or smart pointers – you could possibly run into some issues in the generated bindings. (If you do find any bugs, you may wish to report them.)

2. Pointing CMake to PySide2

The next thing you need to do is use a bit of CMake configuration magic to ensure your build process can find PySide2 and Shiboken when required. We use two CMake scripts to do this – FindPySide2.cmake and FindShiboken.cmake – that run some inline Python code to discover where these modules are in your Python installation and set a handful of CMake variables that point to them for later use.

Thankfully, we’ve done all of that work already – you can grab those two CMake files directly from our GitHub repo and use them unchanged.

3. Creating Shiboken invocation

You could add the requisite CMake variables and configuration to your build process every time you wanted to create Python bindings. However, it’s more convenient to do this with a helper CMake file that allows you to create a Python module with a single macro call. This macro will automatically fill in all the paths, flags, libraries, and variables as needed, making it much easier for any new Python bindings you might need.

We created a CMake macro called CREATE_PYTHON_BINDINGS that sets things up. You’ll need PySide2ModuleBuild.cmake for this – just grab it from our repo and add it to your project.

4. Creating a type system file

The fourth step requires a bit more work than the previous steps, where the work is mostly done for you. This time, you must describe to the Shiboken generator the API that your C++ code will make available to Python. More specifically, you need to outline which C++ classes, methods, and types will be accessible, and what they will look like to a Python caller.

This API definition is called a typesystem specification, and it’s defined in an XML file. There is a great deal of power available; you can rename classes and methods, change or add parameters, modify access levels, even add new functions.

The typesystem definition for KDDockWidgets is:

Code sample 1 – Typesystem_kddockwidgets.xml

Three main blocks are in this definition: your package (for setup and loading other typesystem files), your public types (the enum-types), and your classes (the object-types).

Because we’re providing a straight-through API, our needs are pretty simple. We just need to list the public classes and enums, and then let Shiboken determine the types and method signatures for everything. If you need to provide finer control over how your Python interface looks, you’ll want to consult the reference docs for the XML you’ll need.

5. Creating a header for classes

When you call the CREATE_PYTHON_BINDINGS macro within PySide2ModuleBuild.cmake, you’ll need to pass it a few arguments. One is the location of a header file that includes all the classes that you might need during export. The header itself is simple; you just need to include other headers that Shiboken will need to access all type definitions. In the following example, we’ve included all the headers with the class interfaces that we’re adding to our Python package.

#pragma once

// Make "signals:", "slots:" visible as access specifiers
#define QT_ANNOTATE_ACCESS_SPECIFIER(a) __attribute__((annotate(#a)))

#include 
#include 
#include 
#include 

Code sample 2 – kddockwidgets_global.h

The special QT_ANNOTATE_ACCESS_SPECIFIER line ensures that signals and slots are visible to the generator. (Since this header file is only used by the generator, it’s not harmful to leave it in even if you don’t need it.)

6. Customizing binding make

Here, you set up CMake to invoke Shiboken and add any necessary dependencies. You’ll need to customize this file for your application. Replace all the instances of KDDockWidgets directories, source, and header files with those from your application.

# Auto-Generate files - every class will have a cpp and h file
set(PyKDDockWidgets_SRC
    # individual classes
    ${CMAKE_CURRENT_BINARY_DIR}/KDDockWidgets/kddockwidgets_dockwidgetbase_wrapper.cpp
    ${CMAKE_CURRENT_BINARY_DIR}/KDDockWidgets/kddockwidgets_dockwidgetbase_wrapper.h
    ${CMAKE_CURRENT_BINARY_DIR}/KDDockWidgets/kddockwidgets_dockwidget_wrapper.cpp
    ${CMAKE_CURRENT_BINARY_DIR}/KDDockWidgets/kddockwidgets_dockwidget_wrapper.h
    ${CMAKE_CURRENT_BINARY_DIR}/KDDockWidgets/kddockwidgets_mainwindowbase_wrapper.cpp
    ${CMAKE_CURRENT_BINARY_DIR}/KDDockWidgets/kddockwidgets_mainwindowbase_wrapper.h
    ${CMAKE_CURRENT_BINARY_DIR}/KDDockWidgets/kddockwidgets_mainwindow_wrapper.cpp
    ${CMAKE_CURRENT_BINARY_DIR}/KDDockWidgets/kddockwidgets_mainwindow_wrapper.h
    # namespace wrapper
    ${CMAKE_CURRENT_BINARY_DIR}/KDDockWidgets/kddockwidgets_wrapper.cpp
    ${CMAKE_CURRENT_BINARY_DIR}/KDDockWidgets/kddockwidgets_wrapper.h
    # global module wrapper
    ${CMAKE_CURRENT_BINARY_DIR}/KDDockWidgets/kddockwidgets_module_wrapper.cpp
    ${CMAKE_CURRENT_BINARY_DIR}/KDDockWidgets/kddockwidgets_python.h
)
                                                      
# The includes are needed to parse and build classes specified in our typesystem
set(PyKDDockWidgets_include_paths
    $,${PATH_SEP}>
)

# Set list of paths where shiboken should look for typesystem
set(PyKDDockWidgets_typesystem_paths
    # PySide path, this variable was exposed by FindPySide2.cmake
    ${PYSIDE_TYPESYSTEMS}
)

# Include flags/path that will be set in 'target_include_directories'
set(PyKDDockWidgets_target_include_directories
    ${CMAKE_SOURCE_DIR}
)

# Libraries necessary to link the target for the command 'target_link_libraries'
set(PyKDDockWidgets_target_link_libraries
    KDAB::kddockwidgets
    Qt5::Core
    Qt5::Gui
    Qt5::Widgets
)

# Dependencies - changes on these files should trigger new bindings
set(PyKDDockWidgets_DEPENDS
    ${CMAKE_CURRENT_SOURCE_DIR}/kddockwidgets_global.h
    ${CMAKE_SOURCE_DIR}/src/DockWidgetBase.h
    ${CMAKE_SOURCE_DIR}/src/DockWidget.h
    ${CMAKE_SOURCE_DIR}/src/MainWindowBase.h
    ${CMAKE_SOURCE_DIR}/src/MainWindow.h
)

CREATE_PYTHON_BINDINGS(
    "KDDockWidgets"
    "${PyKDDockWidgets_typesystem_paths}"
    "${PyKDDockWidgets_include_paths}"
    "${PyKDDockWidgets_SRC}"
    "${PyKDDockWidgets_target_include_directories}"
    "${PyKDDockWidgets_target_link_libraries}"
    ${CMAKE_CURRENT_SOURCE_DIR}/kddockwidgets_global.h
    ${CMAKE_CURRENT_SOURCE_DIR}/typesystem_kddockwidgets.xml
    "${PyKDDockWidgets_DEPENDS}"
    ${CMAKE_CURRENT_BINARY_DIR}
)

# Make moduled import from build dir works
configure_file(${CMAKE_CURRENT_SOURCE_DIR}/__init__.py ${CMAKE_CURRENT_BINARY_DIR}/__init__.py)

# install
install(FILES ${CMAKE_CURRENT_SOURCE_DIR}/__init__.py DESTINATION ${Python3_SITELIB}/PyKDDockWidgets)

Code sample 3 – CMakeLists.txt

7. Creating a Python module

If you ran everything right now, you’d have created code that allows Python to call into C++. However, you’d still need to add one small file to the package directory to turn your code into an importable Python package.

__all__ = ['KDDockWidgets']

# Preload PySide2 libraries to avoid missing libraries while loading our module
try:
    from PySide2 import QtCore
except Exception:
    print("Failed to load PySide")
    raise

# Avoid duplicate namespace - the package name would normally look like this:
# PyKDDockWidgets.KDDockWidgets.KDDockWidgets.MainWindow
# (There is a bug PYSIDE-1325 to get this namespace duplication fixed.)
# To avoid this, we use this workaround:
from .KDDockWidgets import KDDockWidgets as _priv
KDDockWidgets = _priv

Code sample 4 – __init__.py

You’ll note we have a small workaround in this example for namespace duplication. If PYSIDE-1325 gets fixed, you won’t need the last several lines of code. In fact, you wouldn’t need any code at all since an empty __init__.py is all that Python requires to turn your code into a package.

8. Running the generator

With everything in place, you can now let the tools do all the work! Invoke CMake, and it will generate the necessary make file. Then, running make will generate and compile your binding files, adding the new Python module to your Python installation.

9. Testing the Python package

Assuming your generation and compilation was successful, you’ve now got a Python package that calls your C++ library. You can import it and start using it, just like a regular Python class.

from PyKDDockWidgets import KDDockWidgets

options = KDDockWidgets.MainWindowOption_None

dock = KDDockWidgets.DockWidget("new dock 1")
dock.resize(600, 600)
dock.show()

Code sample 5 – example.py

Summary

Using PySide and Shiboken to create Python bindings does take a bit of setup, but it’s not anywhere as difficult as doing all the Python/C++ interface work by hand! Steps 1 through 3 of the process need only be done once. After that, for each new C++ library you’ll need a new typesystem XML file, a few customizations to your CMake file, and a new header to include the binding-specific classes – all tasks that can be accomplished in a few minutes.

You’ll be Python-enabled before you know it!

About KDAB

If you like this blog and want to read similar articles, consider subscribing via our RSS feed.

Subscribe to KDAB TV for similar informative short video content.

KDAB provides market leading software consulting and development services and training in Qt, C++ and 3D/OpenGL. Contact us.

The post Creating Python bindings for Qt libraries appeared first on KDAB.

KDAB on Qt: Meeting C++ 2020

$
0
0

 

 

The Schedule, Talks and Speakers are now online for this ever growing ‘partly virtual ‘event for the European C++ community. KDAB is proud to be a Gold sponsor this year.

This year’s online conference features eleven talks in one track. On November 14th you can see a talk from KDAB’s Marc Mutz: Partially-Formed Objects For Fun And Profit.

While the details are on the website, here’s the full talks list and their speakers:

  • How C++20 changes the way we write code by Timur Doumler
  • Hidden Features and Traps of C++ Move Semantics by Nicolai Josuttis
  • C++ Concepts vs Rust Traits vs Haskell Typeclasses vs Swift Protocols by Conor Hoekstra
  • Template Shenanigans: Testing, debugging and benchmarking template code by Jonathan O’Connor
  • Building an Intuition for Composition by Sy Brand
  • The Static Initialization Order Fiasco – How to Properly Initialize Global State by Jonathan Müller
  • Lambdas, uses and abuses by Dawid Zalewski
  • OO Considered Harmful by Phil Nash
  • Calling Functions: A Tutorial by Klaus Iglberger
  • 40 Years Of Evolution from Functions to Coroutines by Rainer Grimm
  • Partially-Formed Objects For Fun And Profit by Marc Mutz

Meeting C++ 2020 is planned as a hybrid event, where a few folks can attend in Berlin according to the COVID-19 rules. The larger part of the conference will happen online.

Sign up for Meeting C++ 2020.

Find out more about Meeting C++ and how it’s grown, from Jens Weller, who created the event.

About KDAB

If you like this blog and want to read similar articles, consider subscribing via our RSS feed.

Subscribe to KDAB TV for similar informative short video content.

KDAB provides market leading software consulting and development services and training in Qt, C++ and 3D/OpenGL. Contact us.

The post Meeting C++ 2020 appeared first on KDAB.

KDAB on Qt: Hotspot Version 1.3.0 Released

$
0
0

We are pleased to announce a new release of our Hotspot Linux perf performance analysis GUI, Hotspot version 1.3.0!

Hotspot is a replacement for perf reportthat takes a perf.data file, parses and evaluates its contents, and then displays the result in a graphical form. You can read a bit more about Hotspot here: https://www.kdab.com/hotspot-gui-linux-perf-profiler/

Overview of New Features and Improvements

With this new release, Hotspot has become much faster at interpreting data for large, complex applications; analyzing perf.data files for firefox,e.g. with its monster libxul, is now magnitudes faster than it was in the previous releases of Hotspot.

On top of that, we now properly support analysis of zstd compressed data files, for example, those obtained via perf record -z. This support feature easily reduces the size of the perf.data files by one to two orders of magnitude.

The timeline has been improved and now shows tick marks on the time axes, as well as smart time unit prefixes when zoomed in:

Hotspot version 1.3.0

You can view the original request for this feature here: https://github.com/KDAB/hotspot/pull/238

Additionally, hotspot can now demangle rustc symbols:

You can view the original request for this feature here: https://github.com/KDAB/hotspot/issues/239

Finally, we’ve updated the perfparser submodule with a plethora of fixes and improvements. Better support for fork-based parallelism is among the numerous improvements that we’ve made.

How can you get the new release?

You can download Hotspot version 1.3.0 from KDAB’s GitHub at this link, where you can also find the list of fixes in this version: https://github.com/KDAB/hotspot/releases/tag/v1.3.0

Try it out and let us know what you think! Please report issues on GitHub, but we are also open to any kind of feedback through the comment field below. If you want to learn more about perf and hotspot, also consider attending one of our Linux specific profiling and debugging training classes.

About KDAB

If you like this blog and want to read similar articles, consider subscribing via our RSS feed.

Subscribe to KDAB TV for similar informative short video content.

KDAB provides market leading software consulting and development services and training in Qt, C++ and 3D/OpenGL. Contact us.

The post Hotspot Version 1.3.0 Released appeared first on KDAB.

Viewing all 15410 articles
Browse latest View live