Qt - System colors?

Linux specific forum
wombats
Enthusiast
Enthusiast
Posts: 663
Joined: Thu Dec 29, 2011 5:03 pm

Qt - System colors?

Post by wombats »

Hi,

Is it possible to obtain system colors with the Qt subsystem somehow?

The styleSheet properties of windows and gadgets appears to be empty, and I can't see to be able to do anything with the palette property. The following doesn't work:

Code: Select all

Debug QtScript("window(0).palette.color(10)")
wombats
Enthusiast
Enthusiast
Posts: 663
Joined: Thu Dec 29, 2011 5:03 pm

Re: Qt - System colors?

Post by wombats »

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.pro

Code: 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.h

Code: 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.h

Code: 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.cpp

Code: 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) );
}
freak
PureBasic Team
PureBasic Team
Posts: 5929
Joined: Fri Apr 25, 2003 5:21 pm
Location: Germany

Re: Qt - System colors?

Post by freak »

You should be able to access the QApplication from PB via QCoreApplication::instance().

https://doc.qt.io/qt-5.9/qcoreapplication.html#instance
quidquid Latine dictum sit altum videtur
wombats
Enthusiast
Enthusiast
Posts: 663
Joined: Thu Dec 29, 2011 5:03 pm

Re: Qt - System colors?

Post by wombats »

Thank you. After a fair amount of trial and error, I managed to get access to PB's QApplication.
Post Reply