Wednesday 28 October 2015

Qt: Updating styles based on dynamic properties

#include <QApplication>
#include <QMainWindow>
#include <QVBoxLayout>
#include <QLabel>
#include <QPushButton>
#include <QStyle>
#include <QDebug>

class App : public QObject
{
    Q_OBJECT
public:
    enum Alert
    {
        None,
        Warning,
        Critical
    };
    Q_ENUM(Alert);

    App(int argc, char** argv)
        : app(argc, argv)
    {}

    int exec()
    {
        window.setCentralWidget(&main);

        label.setText("hello world");
        label.setProperty("alert", QVariant::fromValue(None));

        button.setText("alert");

        QObject::connect(&button, &QPushButton::clicked, [this]()
            {
                label.setProperty("alert", QVariant::fromValue(Critical));
                label.setProperty("foo",   false);

                label.style()->unpolish(&label);
                label.style()->polish(&label);
                label.update();
            });

        layout.addWidget(&label);
        layout.addWidget(&button);
        main.setLayout(&layout);

        app.setStyleSheet(R"(
            QLabel[alert="None"]                { color: green; } 
            QLabel[alert="Warning"]             { color: blue;  }
            QLabel[alert="Critical"][foo=true]  { color: red;     }
            QLabel[alert="Critical"][foo=false] { color: magenta; }
        )");

        window.show();
        return app.exec();
    }

    QApplication app;
    QMainWindow  window;
    QWidget      main;
    QLabel       label;
    QPushButton  button;
    QVBoxLayout  layout;
};

int main(int argc, char** argv)
{
    return App(argc, argv).exec();

}


Tuesday 6 October 2015

Gnome - change default application for text files

xdg-mime query default text/plain
xdg-mime default sublime_text.desktop text/plain

xdg-mime query filetype application/x-shellscript
xdg-mime query default application/x-shellscript