Skip to content

Commit

Permalink
Gui: [skip ci] workaround to allow to define the link color of the ap…
Browse files Browse the repository at this point in the history
…plication's palette
  • Loading branch information
wwmayer committed Oct 8, 2020
1 parent 34ed859 commit f129f3f
Showing 1 changed file with 51 additions and 0 deletions.
51 changes: 51 additions & 0 deletions src/Gui/Application.cpp
Expand Up @@ -2289,6 +2289,37 @@ void Application::setStyleSheet(const QString& qssFile, bool tiledBackground)
QMdiArea* mdi = mw->findChild<QMdiArea*>();
mdi->setProperty("showImage", tiledBackground);

// Qt's style sheet doesn't support it to define the link color of a QLabel
// or in the property editor when an expression is set because therefore the
// link color of the application's palette is used.
// A workaround is to set a user-defined property to e.g. a QLabel and then
// define it in the .qss file.
//

This comment has been minimized.

Copy link
@luzpaz

luzpaz Oct 2, 2022

Contributor

Hi @wwmayer just a heads up that apparently a FreeCAD restart is also required for this to 'stick' (see https://forum.freecadweb.org/viewtopic.php?p=630033#p630033)

// Example:
// QLabel label;
// label.setProperty("haslink", QByteArray("true"));
// label.show();
// QColor link = label.palette().color(QPalette::Text);
//
// The .qss file must define it with:
// QLabel[haslink="true"] {
// color: #rrggbb;
// }
//
// See https://stackoverflow.com/questions/5497799/how-do-i-customise-the-appearance-of-links-in-qlabels-using-style-sheets
// and https://forum.freecadweb.org/viewtopic.php?f=34&t=50744
static bool init = true;
if (init) {
init = false;
mw->setProperty("fc_originalLinkCoor", qApp->palette().color(QPalette::Link));
}
else {
QPalette newPal(qApp->palette());
newPal.setColor(QPalette::Link, mw->property("fc_originalLinkCoor").value<QColor>());
qApp->setPalette(newPal);
}


QString current = mw->property("fc_currentStyleSheet").toString();
mw->setProperty("fc_currentStyleSheet", qssFile);

Expand All @@ -2312,6 +2343,26 @@ void Application::setStyleSheet(const QString& qssFile, bool tiledBackground)

ActionStyleEvent e(ActionStyleEvent::Clear);
qApp->sendEvent(mw, &e);

// This is a way to retrieve the link color of a .qss file when it's defined there.
// The color will then be set to the application's palette.
// Limitation: it doesn't work if the .qss file on purpose sets the same color as
// for normal text. In this case the default link color is used.
{
QLabel l1, l2;
l2.setProperty("haslink", QByteArray("true"));

l1.show();
l2.show();
QColor text = l1.palette().color(QPalette::Text);
QColor link = l2.palette().color(QPalette::Text);

if (text != link) {
QPalette newPal(qApp->palette());
newPal.setColor(QPalette::Link, link);
qApp->setPalette(newPal);
}
}
}
}

Expand Down

0 comments on commit f129f3f

Please sign in to comment.