marcelpetrick
Qt: custom-class and specialities for QVariant and QSettings
While implementing some custom-class (C++17; Qt5.12) some specialities became (painfully) noticeable.
The guide at the Qt-source is quite well written.
- ctor, copy-ctor and dtor are all required and have to be public
- add Q_DECLARE_METATYPE(CustomClass)
- QDebug-stream-operator in case of simple debugging-messages
- if the QVariant-version of the CustomClass shall be written to QSettings, then implement also the two streaming operators: << and >> (else crash with Assert “unable to save type”): howto
block ads on music videos on youtube
Task: youtube has annyoing ads at certain music-videos, which are set as playback-version for last.fm. I like to stream some music while programming, but I hate getting conditioned by advertisments.
Situation: Firefox 71.0, 64 bit, Win10
Solution: install “AdBlocker for YouTube™von AdblockLite” and configure it like this:
MicroPython (uPython) – the future is here
My activity with the ESP8266/ESP32 boards had somehow fallen asleep after I set up one of the ESP8266 as Wifi-repeater. It worked, but creating own devices was too cumbersome. Firing up the ArduinoStudio took ages, building and downloading the firmware in C++ was error-prone, took ages (seriously, this is a tiny program, what the hell happens in the background?).
But I knew there exists a path, which could save some time: µPython (micropython). It runs a firmware, you just deploy your “code”. In my case now a tiny hello-world-like program.
I used this and that tutorial and the uPyCraft-IDE. Got it working with an ESP8266 in minutes.
Seriously: goodbye crappy, non-structured and slow-to-build C/C++ for my microcontrollers. The future is here o/
By the way: the motivation also comes from my current daily practice of Python (of course, I still contribute to C++/Qt-based projects), but my current flame is Python (for Project Euler and daily coding challenges).
C++ name demangling
Like described in “Today I learned” (TIL): c++filt is quite helpful for demangling:
But what if you have none readily available? -> c++filtjs
Three ways to find the location of a certain DLL at Windows
Common problem: just using the current working directory is not sufficient, because especially for unit-tests started from the Visual Studio-testrunner the opriginal, relative path is not fitting anymore. Fallback would be check an environment variable , which is set during installation or using the WinAPI (ugh).
versioned code can be found at: github/cppcCollection
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 |
// +++ ATTENTION! +++ // This is just a snippet to show how to get the path for a certain DLL on the Windows-platform. // First absed on the "current working directory", then the environment variable "PATH", then via WinAPI-call. // Of course, this does not work for any other OS. Nor is this epxected to survive any Windows-makeover (or patch set :X) // +++ ATTENTION! +++ #if defined(Q_OS_WIN) || defined(Q_OS_CYGWIN) // for the plugin-path #include <windows.h> #include <libloaderapi.h> #endif bool properSdkPath(QString const path) { #if defined(Q_OS_WIN) || defined(Q_OS_CYGWIN) // Check for the existance of a very, very, very important DLL. return QFileInfo(path + "/SDK.dll").exists(); #endif #ifdef Q_OS_MAC // macOS has SDK-support, so using the current application-path is sufficient return true; #endif } // Return the path to the hardware-plugins. // First check is based on the current working directory. // If this fails, check the contents of the environment-variable 'PATH' (set at install time). // If this fails, then check via Win-API. QString getPluginPath() { QString pathToSDK; // Try first the current working directory pathToSDK = QCoreApplication::applicationDirPath(); #if defined(Q_OS_WIN) || defined(Q_OS_CYGWIN) // If this failed (has no SDK.dll), indagnate the PATH environment variable. // Which is set during installation. if(!properSdkPath(pathToSDK)) { // Get the directory from the PATH (is set during APPLICATION SDK installation). auto const path = QString::fromLocal8Bit(qgetenv("PATH")); auto const splitPath = path.split(";", QString::SkipEmptyParts); for(auto const& item : splitPath) { if(item.contains("APPLICATION SDK")) { pathToSDK = QFileInfo(item).absoluteFilePath(); break; } } // If even this failed, then fall back to GetModuleFileName (happens with VS-testrunner). if(!properSdkPath(pathToSDK)) { // get the handle of the DLL HMODULE handleModule = GetModuleHandleA("SDK.dll"); LPWSTR modulePath = nullptr; DWORD bufLen = 4096; // reserve enough memory if(!(modulePath = (TCHAR*)malloc(sizeof(TCHAR) * (size_t)bufLen))) { TRACE_INFO("Could not allocate 4 KB memory. Please consider an hardware upgrade.") } // retrieve the path DWORD pathSize = 4096; pathSize = GetModuleFileName(handleModule, modulePath, pathSize); if(pathSize) { QString outputPath = QString::fromWCharArray(modulePath, static_cast<int>(pathSize)); pathToSDK = outputPath.replace("SDK.dll", ""); } else { TRACE_INFO("GetModuleFileName failed:" << GetLastError()); } } } #endif if(!properSdkPath(pathToSDK)) { TRACE_INFO("Could not determine proper plugin-path."); pathToSDK.clear(); // reset } // convert to proper separators and append the suffix for the plugin-location QString const hardwarePluginPath = QFileInfo(pathToSDK).filePath() + "/Plugins"; return hardwarePluginPath; } |
Visual Studio 2015: error LINK1158 rc.exe not found
Challenge: Visual Studio 2015, Windows Kit 8 and 10 installed, test application (as solution) for a SDK not buildable
Solution:
copy rc.exe and rcdll.dll from
C:\Program Files (x86)\Windows Kits\8.1\bin\x64
to
C:\Program Files (x86)\Microsoft Visual Studio 14.0\VC\bin
SSH/SCP quick help
Challenge was to reconfigure a device, which has a Linux running on a TQ-board. Some files had to be adjusted.
Connect to the device:
1 |
ssh <usernam>:<ip> |
(Then enter password.)
List amount of free space:
1 |
df -K . |
Show target of symlink:
1 |
readlink <file> |
quick git stats
(Another way without external tools.)
1 2 3 |
export LC_ALL='C' git ls-tree -r HEAD | sed -re 's/^.{53}//' | while read filename; do file "$filename"; done | grep -E ': .*text' | sed -r -e 's/: .*//' | while read filename; do git blame -w "$filename"; done | sed -r -e 's/.*\((.*)[0-9]{4}-[0-9]{2}-[0-9]{2} .*/\1/' -e 's/ +$//' | sort | uniq -c |
Visual Studio testrunner still running … and stopping you from accessing DLLs for linking
Problem Challenge:
Several instances of vstest.executionengine.clr20.exe are still running, despite closing that nice Visual Studio 2015 and therefore the access to compile and link several DLLs with QtCreator is blocked.
Closing them one by one with the taskmanager is annoying.
Solution:
* CMD as admin
* $ taskkill /IM vstest.executionengine.clr20.exe /T /F
* or: $ wmic process where name=’vstest.executionengine.clr20.exe’ delete
QTest: increase the timeout duration for a test-case
Set via environment variable QTEST_FUNCTION_TIMEOUT a higher timeout duration.
qputenv sets them temporary for the run of the single test case (put this at the beginning of the test case).
1 2 3 4 5 6 7 |
std::string duration("1200000"); // 1200 seconds -> 20 min QByteArray timeoutDuration(duration.c_str(), static_cast<int>(duration.length())); qputenv("QTEST_FUNCTION_TIMEOUT", timeoutDuration); { // just for checking .. auto result = qgetenv("QTEST_FUNCTION_TIMEOUT"); qDebug() << "timeout set to:" << result << "ms"; } |
nota bene: This block has to be put before qExec is ran, so put it into the constructor of the test-suite!