Change the size of Qt dialogs depending on the platform
I'开发者_JS百科ve got some QDialog subclasses that were designed on Windows and which are now being ported over to Mac OS X. The problem is that the default font on Mac OS X appears to be much bigger, so the dialogs look quite cramped.
What's the best way of making the dialogs bigger on Mac OS X than on Windows? (The size must remain fixed on each platform and they must look native.)
An example is the dialogs in Perforce's P4V.
Thanks.
I had the same problem when porting from Win32 to Mac OS X, especially with:
a) Buttons: Their height (in pixels) has to be different in order to look the same.
b) Labels: The font-size (in points) has to be different in order to look the same.
I tried to create an -as possible- generic solution, following these rules:
I performed all form and widget layout editing ONLY in one environment (Windows XP) and transfered the source to other (OS X) only for compilation & test.
I created a generic OS-Dependend function to modify Button Height and Label's font-size at runtime (see bellow) and I called this function from every custom dialog constructor, after setupUI() like this:
someDialog::someDialog(QWidget *parent) : QDialog(parent)
{
setupUi(this);
genAdjustWidgetAppearanceToOS(this);
// ...
}I introduced an exception list in genAdjustWidgetAppearanceToOS(this) function, and put in it the names of all controls that I will not want to affect (nothing is perfect).
Here it's my generic function to check and see if it can be of any help to you: (!remember to modify at least the "DoNotAffect" list and append your labels/buttons names)
// ======================================================
// Adjust specific Widget children according to O/S
// => Set Buttons height
// => Set labels font size
// ======================================================
void genAdjustWidgetAppearanceToOS(QWidget *rootWidget)
{
if (rootWidget == NULL)
return;
QObject *child = NULL;
QObjectList Containers;
QObject *container = NULL;
QStringList DoNotAffect;
// Make an exception list (Objects not to be affected)
DoNotAffect.append("aboutTitleLabel"); // about Dialog
DoNotAffect.append("aboutVersionLabel"); // about Dialog
DoNotAffect.append("aboutCopyrightLabel"); // about Dialog
DoNotAffect.append("aboutUrlLabel"); // about Dialog
DoNotAffect.append("aboutLicenseLabel"); // about Dialog
// Set sizes according to OS:
#ifdef Q_OS_MAC
int ButtonHeight = 32;
int LabelsFontSize = 12;
#else // Win XP/7
int ButtonHeight = 22;
int LabelsFontSize = 8;
#endif
// Append root to containers
Containers.append(rootWidget);
while (!Containers.isEmpty())
{
container = Containers.takeFirst();
if (container != NULL)
{
for (int ChIdx=0; ChIdx < container->children().size(); ChIdx++)
{
child = container->children()[ChIdx];
if (!child->isWidgetType() || DoNotAffect.contains(child->objectName()))
continue;
// Append containers to Stack for recursion
if (child->children().size() > 0)
Containers.append(child);
else
{
// Cast child object to button and label
// (if the object is not of the correct type, it will be NULL)
QPushButton *button = qobject_cast<QPushButton *>(child);
QLabel *label = qobject_cast<QLabel *>(child);
if (button != NULL)
{
button->setMinimumHeight(ButtonHeight); // Win
button->setMaximumHeight(ButtonHeight); // Win
button->setSizePolicy(QSizePolicy::Fixed,
button->sizePolicy().horizontalPolicy());
}
else if (label != NULL)
{
QFont aFont = label->font();
aFont.setPointSize(LabelsFontSize);
label->setFont(aFont);
}
}
}
}
}
}
I've done two things in the past to deal with oddities like that (and when you got to the point of porting to mobile devices, it gets much worse):
1) use scaled fonts based on the original:
QFont font = widget.font();
font.setSize(3 * font.size() / 2);
widget.setFont(font);
But this probably won't quite do it for you.
2) use, sadly, ifdefs to do it on a per-platform basis:
#ifdef Q_OS_MAC
// change font here
#endif
A full list of OS defines can be found here
精彩评论