我有一些在 Windows 上设计的 QDialog 子类,现在正在移植到 Mac OS X。问题是 Mac OS X 上的默认字体看起来要大得多,所以对话框看起来很拥挤。
在 Mac OS X 上使对话框比在 Windows 上更大的最佳方法是什么?(大小必须在每个平台上保持固定,并且它们必须看起来是原生的。)
一个例子是 Perforce 的 P4V 中的对话框。
谢谢。
从 Win32 移植到 Mac OS X 时,我遇到了同样的问题,尤其是:
a) 按钮:它们的高度(以像素为单位)必须不同才能看起来相同。
b) 标签:字体大小(以磅为单位)必须不同才能看起来相同。
我尝试按照以下规则创建一个尽可能通用的解决方案:
我仅在一个环境(Windows XP)中执行所有表单和小部件布局编辑,并将源代码传输到其他环境(OS X)仅用于编译和测试。
我创建了一个通用的 OS-Dependend 函数来在运行时修改按钮高度和标签的字体大小(见下文),并在 setupUI() 之后从每个自定义对话框构造函数中调用此函数,如下所示:
someDialog::someDialog(QWidget *parent) : QDialog(parent)
{
setupUi(this);
genAdjustWidgetAppearanceToOS(this);
// ...
}
我在 genAdjustWidgetAppearanceToOS(this) 函数中引入了一个例外列表,并将我不想影响的所有控件的名称放入其中(没有什么是完美的)。
这是我的通用功能来检查它是否对您有任何帮助:(!记住至少修改“DoNotAffect”列表并附加您的标签/按钮名称)
// ======================================================
// 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);
}
}
}
}
}
}
我过去做过两件事来处理这样的怪事(当你要移植到移动设备时,情况会变得更糟):
1)在原版的基础上使用缩放字体:
QFont font = widget.font();
font.setSize(3 * font.size() / 2);
widget.setFont(font);
但这可能不太适合你。
2) 可悲的是,使用 ifdefs 在每个平台上执行此操作:
#ifdef Q_OS_MAC
// change font here
#endif
操作系统定义的完整列表可以在这里找到