This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <QtWidgets> | |
#include <QtSql> | |
//to ensure the database connection is closed and removed at the end | |
//of the write/read func call | |
class SQLConnectionRAIIWrapper{ | |
public: | |
SQLConnectionRAIIWrapper(QString fileName){ | |
//use a unique connection name, to ensure thread safety | |
connectionName = QString("SQLSETTINGS%1").arg((int)QThread::currentThreadId()); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <QtWidgets> | |
//QThread wrapper for safe destruction | |
//see http://stackoverflow.com/a/19666329 | |
class Thread : public QThread { | |
using QThread::run; // final | |
public: | |
Thread(QObject * parent = 0) : QThread(parent) {} | |
~Thread() { quit(); wait(); } | |
}; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <QtWidgets> | |
#include <QtNetwork> | |
class SyncTcpSocket : public QTcpSocket{ | |
Q_OBJECT | |
public: | |
explicit SyncTcpSocket(QObject* parent= nullptr):QTcpSocket(parent){ | |
setSocketOption(QAbstractSocket::LowDelayOption, 1); | |
heartbeatTimer.setInterval(100); | |
connect(&heartbeatTimer, &QTimer::timeout, |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include "qthreadutils.h" | |
#include <QtCore> | |
//the following file shows an example usage for CallByWorker | |
//a thread that can be destroyed at any time | |
//see http://stackoverflow.com/a/25230470 | |
class SafeThread : public QThread{ | |
using QThread::run; | |
public: |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#ifndef DATASTREAM_ENUM_H | |
#define DATASTREAM_ENUM_H | |
#include <type_traits> | |
//a function that can serialize any enum into QDataStream | |
//it stores the enum in a qint64 | |
template<typename Enum, | |
typename = typename std::enable_if<std::is_enum<Enum>::value>::type> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//from Howard Hinnant's answer https://stackoverflow.com/a/18303787 | |
#include <random> | |
#include <chrono> | |
#include <iostream> | |
std::random_device eng; //can be used to generate random input for test cases | |
std::uniform_int_distribution<std::size_t> random(500, 1000); | |
//a function template that executes a given function object and returns |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//from Howard Hinnant's answer https://stackoverflow.com/a/28003328 | |
#include <cstdlib> | |
#include <cstddef> | |
#include <iostream> | |
std::size_t allocated = 0; | |
void* operator new(std::size_t sz) { | |
void* p = std::malloc(sz); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <QtCore> | |
#include <QtNetwork> | |
//some custom data structure depending on QList | |
using DataStructure = QList<QPair<QString, QString>>; | |
//Q_DECLARE_METATYPE(DataStructure) | |
/*QDataStream& operator<<(QDataStream& stream, const DataStructure& ds) { | |
stream << (quint32) ds.size(); | |
for(auto e : ds) stream << e; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//based on https://doc.qt.io/qt-5/qsqlquerymodel.html#fetchMore | |
#include <QtWidgets> | |
#include <QtSql> | |
#include <type_traits> | |
//comment out the next line to disable eager loading | |
#define QSQLMODEL_EAGER_LODING 1 | |
//a class template that can be used to disable lazy loading | |
//in any QAbstractTableModel subclass |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <windows.h> | |
#include <memory> | |
#include <string> | |
//returns the serial number of the first physical drive in a std::string or an empty std::string in case of failure | |
//based on http://codexpert.ro/blog/2013/10/26/get-physical-drive-serial-number-part-1/ | |
std::string getFirstHddSerialNumber() { | |
//get a handle to the first physical drive | |
HANDLE h = CreateFileW(L"\\\\.\\PhysicalDrive0", 0, FILE_SHARE_READ|FILE_SHARE_WRITE, NULL, OPEN_EXISTING, 0, NULL); | |
if(h == INVALID_HANDLE_VALUE) return {}; |
OlderNewer