PDA

View Full Version : Using a QProxyStyle to resize icons within a toolbar and a QFileDialog instance



RK117
4th September 2015, 04:27
Hello everyone,

I use Qt5 (5.4.1) to build an application under Linux. My problem concerns the size of icons. When I create a toolbar, after compiling, I got icons too small. I know I can use QToolbar::setIconSize(). And I used it in a first time. But now I have exactly the same problem with the icons displayed by an instance of QFileDialog::getSaveFileName. Thus I decided to write my own QProxyStyle and to load it via QApplication::setStyle(). The size of icons in the toolbar are right but not in the QFileDialog. I thought that QFileDialog inherits the style from the QApplication. I'm wrong. I join my code.



int main(int argc, char **argv)
{
QApplication::setStyle(new TMProxyStyle);
QApplication *application = new QApplication(argc,argv);
TMWindow *mainwindow = new TMWindow();
mainwindow->show();
return application->exec();
}


My customized proxystyle :



class TMProxyStyle: public QProxyStyle
{
public:
int pixelMetric(PixelMetric, const QStyleOption * =NULL,const QWidget * =NULL) const;
};

int TMProxyStyle::pixelMetric(PixelMetric metric, const QStyleOption *options,const QWidget *widget) const
{
if(metric==QStyle::PM_ToolBarIconSize) return ICONSIZE;
if(metric==QStyle::PM_SmallIconSize) return ICONSIZE;
if(metric==QStyle::PM_LargeIconSize) return ICONSIZE;
return QProxyStyle::pixelMetric(metric,options,widget);
}


Here is a snapshot of the concerned part of my QFileDialog
11350

Can you help me ? Where is my error ?

Thank you very much.

anda_skoa
4th September 2015, 09:44
For the file dialog it will depend which file dialog is being used.

By default, if you use the static "get" methods, Qt will try to invoke the platform's file dialog for better integration with the system.
This is usually an external component and not subject to any application styling (it should look like the dialog of any other application after all).

If you want control over the dialog on the expense of platform integration, you can use the built-in dialog by passing QFileDialog::DontUseNativeDialog as an Option flag.

Cheers,
_

RK117
5th September 2015, 06:28
Thank you for your answer. Now I understand better how styling is applied.

Cheers,