I made a shared library that returns the colors I need, but it crashes if I use it in code that uses the Qt subsystem. It works perfectly fine if I don't create any windows. I presume because I have to create a QApplication in the library and it clashes with the PureBasic one, but I don't know. Maybe I didn't do the shared library correctly. If anyone has experience with this kind of thing and could take a look, here is the code. I used Qt Creator on Ubuntu.
untitled.proCode: Select all
QT += widgets
TEMPLATE = lib
DEFINES += UNTITLED_LIBRARY
CONFIG += c++11
# The following define makes your compiler emit warnings if you use
# any Qt feature that has been marked deprecated (the exact warnings
# depend on your compiler). Please consult the documentation of the
# deprecated API in order to know how to port your code away from it.
DEFINES += QT_DEPRECATED_WARNINGS
# You can also make your code fail to compile if it uses deprecated APIs.
# In order to do so, uncomment the following line.
# You can also select to disable deprecated APIs only up to a certain version of Qt.
#DEFINES += QT_DISABLE_DEPRECATED_BEFORE=0x060000 # disables all the APIs deprecated before Qt 6.0.0
SOURCES += \
untitled.cpp
HEADERS += \
untitled_global.h \
untitled.h
# Default rules for deployment.
unix {
target.path = /usr/lib
}
!isEmpty(target.path): INSTALLS += target
untitled_global.hCode: Select all
#ifndef UNTITLED_GLOBAL_H
#define UNTITLED_GLOBAL_H
#include <QtCore/qglobal.h>
#if defined(UNTITLED_LIBRARY)
#define UNTITLED_EXPORT Q_DECL_EXPORT
#else
#define UNTITLED_EXPORT Q_DECL_IMPORT
#endif
#endif // UNTITLED_GLOBAL_H
untitled.hCode: Select all
#ifndef UNTITLED_H
#define UNTITLED_H
#include <QApplication>
#include <QMainWindow>
#include <QPalette>
#include <QBrush>
#include <QColor>
#include <QMap>
#include "untitled_global.h"
class UNTITLED_EXPORT Untitled
{
public:
Untitled();
char* getColors();
private:
QApplication a;
};
#endif // UNTITLED_H
untitled.cppCode: Select all
#include "untitled.h"
char* Untitled::getColors()
{
int argc = 1;
char appname[] = "colorApp";
char* argv[] = {appname, NULL};
QApplication a(argc, argv);
a.exec(); // Necessary?
QPalette p = a.palette();
a.exit(); // Necessary?
QMap<QString, QBrush> map;
map["Window"] = p.color(QPalette::Window);
map["WindowText"] = p.color(QPalette::WindowText);
map["Foreground"] = p.color(QPalette::Foreground);
map["Base"] = p.color(QPalette::Base);
map["AlternateBase"] = p.color(QPalette::AlternateBase);
map["ToolTipBase"] = p.color(QPalette::ToolTipBase);
map["ToolTipText"] = p.color(QPalette::ToolTipText);
map["PlaceholderText"] = p.color(QPalette::PlaceholderText);
map["Text"] = p.color(QPalette::Text);
map["Button"] = p.color(QPalette::Button);
map["ButtonText"] = p.color(QPalette::ButtonText);
map["BrightText"] = p.color(QPalette::BrightText);
QString returnStr;
returnStr.append("{");
int count;
QMapIterator<QString, QBrush> i(map);
while (i.hasNext()) {
i.next();
QBrush brush = i.value();
returnStr.append("\"" + i.key() + "\":{");
returnStr.append("\"r\":" + QString::number (brush.color().red()) + ",");
returnStr.append("\"g\":" + QString::number (brush.color().green()) + ",");
returnStr.append("\"b\":" + QString::number (brush.color().blue()));
count += 1;
if (count < map.size()) {
returnStr.append("},");
} else {
returnStr.append("}");
}
}
returnStr.append("}");
QByteArray text = returnStr.toLocal8Bit();
char *data = new char[text.size() + 1];
strcpy(data, text.data());
return ( const_cast<char *> (data) );
}