Windowsで設計され、現在Mac OS Xに移植されているQDialogサブクラスがいくつかあります。問題は、Mac OS Xのデフォルトのフォントがはるかに大きく見えるため、ダイアログが非常に窮屈に見えることです。
WindowsよりもMacOSXでダイアログを大きくする最良の方法は何ですか?(サイズは各プラットフォームで固定されたままで、ネイティブに見える必要があります。)
例として、PERFORCEのP4Vのダイアログがあります。
ありがとう。
Win32からMacOSXに移植するときに、特に次の場合に同じ問題が発生しました。
a)ボタン:同じように見えるようにするには、ボタンの高さ(ピクセル単位)を変える必要があります。
b)ラベル:同じように表示するには、フォントサイズ(ポイント単位)が異なる必要があります。
次のルールに従って、可能な限り一般的なソリューションを作成しようとしました。
すべてのフォームとウィジェットのレイアウト編集を1つの環境(Windows XP)でのみ実行し、コンパイルとテストのためにのみソースを他の環境(OS X)に転送しました。
実行時にボタンの高さとラベルのフォントサイズを変更する汎用OS依存関数を作成し(以下を参照)、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);
}
}
}
}
}
}
私は過去にそのような奇妙なことに対処するために2つのことをしました(そしてあなたがモバイルデバイスへの移植のポイントに達したとき、それははるかに悪化します):
1)オリジナルに基づいて拡大縮小されたフォントを使用します。
QFont font = widget.font();
font.setSize(3 * font.size() / 2);
widget.setFont(font);
しかし、これはおそらくあなたのためにそれを完全に行うことはありません。
2)悲しいことに、プラットフォームごとにそれを行うにはifdefsを使用します。
#ifdef Q_OS_MAC
// change font here
#endif
OS定義の完全なリストはここにあります