QFontDialog: get font color - qt

On mac, the QFontDialog is mapped to a nice system font dialog.
It is possible to choose between every system fonts and it is also possible to choose the font colour and effects (underline, strikethrough etc...)
How can I get those attributes?
bool ok;
QFont font = QFontDialog::getFont(&ok, QFont("Helvetica", 10), this);
if (ok) {
// do something
}

From the Qt's docs it does not seem to be possible to get color property when using QFontDialog.
When using Mac you get the Mac native panel which also displays the color. It is recommended not to use the native panel on Mac:
Use Qt's standard font dialog on the Mac instead of Apple's native font panel. (Currently, the native dialog is never used, but this is likely to change in future Qt releases.)
Note, the above part (that it is likely to change in future release) was present since version qt4 on the docs...
You can use option property to use Qt's dialog: fontDialog->setOption(QFontDialog::DontUseNativeDialog);
For the underline, strikethrough and other font-related properties, you can access them through QFont API, e.g., QFont::underline().

Related

How to dynamically change font family based on different languages using react-native

I am developing a mobile app using react-native. The app needs to handle multiple languages (English and Sinhala). Some mobile phones support Sinhala fonts and display them correctly. But some of not support the Sinhala language. For that, I want to load the font using the font folder within the app.
I add font folder to the app and linking them using the react-native link. But font not loading correctly.
I want to know how to load custom fonts to react-native
How to load font family dynamically, when user change language
I might be a little late but I found this solution that you can use to dynamically change the font based on the language.
1.
import NativeModules from 'react-native'
2.
Then use this inline if to change the language when the user changes the device locale.
fontFamily: ((NativeModules.I18nManager.localeIdentifier == 'az_AZ') ? 'Alice' : 'DesirasNonCommercial')
If you have more than two languages, you can create a module and implement a function to check the locale and return the corresponding font. Then you can simply use the returned value in the fontFamily.
Also If you have a button to manually change the language, you can modify the function above and add an event listener to return the appropriate fontFamily when the language changing button is triggered.

How do "theme" icons work in Qt Creator Designer?

When I create the "Command link button" (QCommandLinkButton) it has relatively nice green arrow icon.
I would like to see what other nice icons can I choose. When I try to change the icon, [Theme] appears instead of path or some GUI selection dialog:
I also noticed the context menu:
When I click Set icon from theme, again expecting some GUI selection list, I get just a text field:
What I was imagining:
Where's the list of icons from which the green arrow was taken?
QIcon::fromTheme works under specific conditions.
If it can find it in the QIcon::themeSearchPaths() for the QIcon::themeName()
If the desired icon isn't there, Qt Designer won't be able to do any of the from theme, named icons.
But... if you check your target system for the theme search paths and set the theme name, you are more likely to have success.
Example
On linux, I wanted to get a plus and a minus icon.
I found list-add.png and list-remove.png fit the bill.
https://github.com/GNOME/adwaita-icon-theme/tree/master/Adwaita/16x16/actions
http://standards.freedesktop.org/icon-theme-spec/icon-theme-spec-latest.html
http://standards.freedesktop.org/icon-naming-spec/icon-naming-spec-latest.html
I did a locate on my system and found these:
/usr/share/icons/gnome/16x16/actions/list-add.png
...
/usr/share/icons/gnome/32x32/actions/list-add.png
/usr/share/icons/gnome/scalable/actions/list-add.svg
/usr/share/icons/oxygen/16x16/actions/list-add.png
...
Forcing with fallback icon in QIcon::fromTheme
Find the icon on the filesystem:
ui->toolButton->setIcon(QIcon::fromTheme("list-add",
QIcon("/usr/share/icons/gnome/16x16/actions/list-add.png")));
Find the icon in the qt resource system...
Add the icon in a qrc file in your build, then reference it's path.
ui->toolButton->setIcon(QIcon::fromTheme("list-add",
QIcon(":/list-add.png")));
Overriding the current icon theme
qDebug() << "themeSearchPaths:" << QIcon::themeSearchPaths() << QIcon::themeName();
// themeSearchPaths: ("/usr/local/share/icons", "/usr/share/icons", ":/icons") "hicolor"
The default theme for the system, and for the target deployment machine, likely didn't have the icons in it I wanted... but the gnome or oxygen icon desktop theme installed would almost always have it...
QIcon::setThemeName("oxygen");
Note that you won't see the preview in Qt Designer necessarily because it doesn't set the theme until runtime of your code.
The gnome icon library has 1100+ icons in it. Here is one list:
https://gist.github.com/peteristhegreat/c0ca6e1a57e5d4b9cd0bb1d7b3be1d6a
This works as long as you know what themes are available on the target system.
The list from freedesktop.org has 286 icons listed.
Use icons included in Qt
Just like #peppe pointed out, Qt includes 70 standard icons, too.
widget->setIcon(widget->style()->standardIcon(QStyle::SP_BrowserReload));
http://doc.qt.io/qt-5/qstyle.html#StandardPixmap-enum
Conclusion
Using a stock library on your target system is probably the fastest. Using the Qt built-ins is fast to figure out and use, but is fairly limited. Using a resource file is probably the most robust method, and gives unlimited options on what icon to use.
Be sure to pick a standard icon pack, and think about licensing and attributions, and some other things like that.
And there is no shortage of icons available online:
https://www.quora.com/What-is-the-best-icon-library
https://www.google.com/search?q=open+source+icon+library
Hope that helps.
I don't think that's the function you want to use. The "theme" name there corresponds to the QIcon::fromTheme functionality, which uses icons named according to the FDO specification
http://standards.freedesktop.org/icon-theme-spec/icon-theme-spec-latest.html
http://standards.freedesktop.org/icon-naming-spec/icon-naming-spec-latest.html
And they're not really supported on non-FDO platforms (Windows, Mac, ...) unless you deploy your own theme files.
Now some stock icons are shipped with Qt itself; I don't know how to set them from Designer, but from code you can use QStyle::standardIcon:
widget->setIcon(widget->style()->standardIcon(QStyle::SP_BrowserReload));
If the icon you need is not provided by Qt, you'll need to ship it. In that case the Resource System is a convenient way to bundle it alongside your executable.
Last, but not least, from a UX point of view you should consider using QToolButtons unless you're really building a Vista-like wizard.

How do you get System default font settings in Qt?

I am building a desktop app using Qt, my dev machine is win 7 x64 with japanese locale, standard system font is Meiryo. Most of win 7 UI is in this font, though classic/older programs such as ui font customization window itself uses different font which is MS UI Gothic.
This doesn't bother me until I found that QtCreator builds my app with MS UI Gothic in one place, and Meiryo in the other. For example, qlabels, qlineedits, qcombobox all uses MS UI Gothic, but a custom completer with a qtableview i add later uses Meiryo.
I made most of the UI in QtCreator's Designer, and the completer I added in code. If I change all widgets' font to Meiryo in The Designer, then of course the app will use Meiryo therefore look right, but in this case I'd rather Qt pick whatever system's default font automatically for me because win 7 will not be the only platform I'll use this program.
What raised my concern even more is that QApplication::font() returns MS UI Gothic, which is false in my case. Of course I can set app-wide font using QApplication::setFont() but that defeats the whole purpose of having the native look-n-feel without micromanaging fonts.
So my question is,
how do Qt determine system default font and,
if this is a Qt Bug how do I go about getting around it?
how should I use .ui files and still have my UI use default system font in runtime?
Some clarifications and facts I found
I want my app to use system default font for EVERY text.
This discussion said that Designer will add font info regardless whether you want it or not. So Qt will honor this info rather than system default font. At least someone mentioned removing this information manually should make Qt pick system default font in runtime.
In my dev machine QApplication::font() returns WRONG default font. BUT how come a QTableView I add later in code uses RIGHT font? Where did it get this info?
So if I find where QTableView finds this info, I can get it in main, and set it app wide with QApplication::setFont(). Then what's left is manually removing all font info, then HOPEFULLY it will work. But this is like why we use Qt in the first place isn't it?
I can think of two possible solution:
You could pack with your application a font as a resource file, this way all platforms will use that font regardless the current systems default font.
The QFont class has a method called defaultFamily(). Using this you could manually set the default font for your whole QApplication.
An example (main method):
QApplication application(argc, argv);
QFont font;
font.setFamily(font.defaultFamily());
application.setFont(font);
...rest of your code...
Qt will call Windows API SystemParametersInfo to get fonts for QMenu, QMessageBox etc. , and use GetStockObject to get default system font. Some widgets have special font which is different to system default one. I think Qt is doing the right thing, but the default Japanese/Chinese serif font looks bad on HiDPI monitors.
Just use QFont QApplication::font ( const char * className ) to get the right font (Meryo in your case), such as qApp->setFont(QApplication::font("QMenu"))

GTK3: Getting a style's class property

I'm trying to get the text color of a class of a GTK style.
I have a GtkStyleContext with the widget path and the classes that I want to get their properties.
GtkWidgetPath* widgetPath = gtk_widget_path_new ();
gtk_widget_path_append_type(widgetPath, GTK_TYPE_WINDOW);
gtk_widget_path_iter_set_name(widgetPath, -1 , "UnityPanelWidget");
GtkStyleContext *context = gtk_style_context_new();
gtk_style_context_set_path(context, widgetPath);
gtk_style_context_add_class(context, "gnome-panel-menu-bar");
gtk_style_context_add_class(context, "unity-panel");
I used gtk_style_context_lookup_color and it gets the normal text color but not the text color in gnome-panel-menu-bar or unity-panel.
I tried using GtkCssProvider but it gets the generic style without the classes I added.
I'm using it in a Qt application to get a native GTK3 look and feel.
This code is working fine in a GTK application.
Thanks!
As far as I know you won't able to use the code above because Qt uses Gtk2 and not Gtk3. This means that you will get a core dump with a message similar to:
Gtk-ERROR **: GTK+ 2.x symbols detected. Using GTK+ 2.x and GTK+ 3 in
the same process is not supported
You might be able to do something hacky by accessing the Gtk 3 libs from a diff process, but is a hack.

Show standard warning icon in Qt4

I'm trying to display a "warning" icon next to a QLineEdit if it contains invalid data. I was trying to use QStyle::standardIcon(QStyle::SP_MessageBoxWarning) to get a standard pixmap and place it inside a QLabel, and in some cases this seems to work. When running Gnome the icon is displayed correctly, but when running under KDE no icon is shown. I assume that the warning icon is simply not included in the style used under KDE.
What is the preferred way to display a "standard" warning icon in Qt? Does there exist some list which shows which icons are included in every style? How can I get an icon from a style that I know includes the warning icon?
The last time I had a similar problem, I found this Qt labs discussion useful. It informed me that QIcon now (since 4.6 I believe) has a QIcon::fromTheme function that allows you to load an icon based on the Freedesktop.org Icon Naming Specification, and in addition provide a fallback icon to be used if the current theme does not have the icon in question.
What I did was then to include some very basic icons for use as fallback, and in general specify icons only by their Freedesktop names. This gave a theme-consistent look almost always, and the program still worked in cases where people were missing icons.
As for the warning icon, I'm guessing/hoping that every theme must have the one named "dialog-warning", and that it's what you're looking for.
I hope this helps.
Edit: Oh and, in case you don't know, it can be useful to look at for example the Tango icon set to get a rough idea of what the Freedesktop names correspond to (although it is of course theme-dependent).
Qt does bundle a number of images that are resources that you can use in your own code. These images are a superset of those available via standardIcon() You may want to verify that the particular image is included in the versions of Qt you're targeting.
The end result could look like the following:
QPixmap pixmap(":/trolltech/styles/commonstyle/images/up-128.png");
// use pixmap as needed
For anyone who wants to know how to do this in a Windows environment you can:
Create a qLabel in your custom class, and then in the constructor of that class create a QIcon with the style you want, convert it into a pixmap and use the QLabel::setPixmap() function to apply it to the one you created:
QIcon icon = style()->standardIcon(QStyle::SP_MessageBoxWarning); //or
//whatever icon you choose
QPixmap pixmap = icon.pixmap(QSize(60, 60));
ui->iconLabel->setPixmap(pixmap);
ui->iconLabel->setScaledContents(true); //you can set this to fill the
//dimensions of your qLabel if you wish.

Resources