Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/qt/6.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/ruby/25.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
根据平台更改Qt对话框的大小_Qt_Macos_Qdialog - Fatal编程技术网

根据平台更改Qt对话框的大小

根据平台更改Qt对话框的大小,qt,macos,qdialog,Qt,Macos,Qdialog,我有一些QDialog子类,它们是在Windows上设计的,现在被移植到Mac OS X上。问题是Mac OS X上的默认字体看起来要大得多,所以对话框看起来很拥挤 让Mac OS X上的对话框比Windows上的对话框大的最好方法是什么?(每个平台上的大小必须保持不变,并且必须看起来是本地的。) Perforce的P4V中的对话框就是一个例子 谢谢。我过去做过两件事来处理这种奇怪的事情(当你要移植到移动设备上时,情况会变得更糟): 1) 使用基于原始字体的缩放字体: QFont font =

我有一些QDialog子类,它们是在Windows上设计的,现在被移植到Mac OS X上。问题是Mac OS X上的默认字体看起来要大得多,所以对话框看起来很拥挤

让Mac OS X上的对话框比Windows上的对话框大的最好方法是什么?(每个平台上的大小必须保持不变,并且必须看起来是本地的。)

Perforce的P4V中的对话框就是一个例子


谢谢。

我过去做过两件事来处理这种奇怪的事情(当你要移植到移动设备上时,情况会变得更糟):

1) 使用基于原始字体的缩放字体:

QFont font = widget.font();
font.setSize(3 * font.size() / 2);
widget.setFont(font);
但这对你来说可能不太合适

2) 遗憾的是,请使用ifdef在每个平台上执行此操作:

#ifdef Q_OS_MAC
// change font here
#endif

可以找到操作系统定义的完整列表

从Win32移植到Mac OS X时,我遇到了同样的问题,尤其是:

a) 按钮:它们的高度(以像素为单位)必须不同才能看起来相同

b) 标签:字体大小(以点为单位)必须不同才能看起来相同

我尝试创建一个尽可能通用的解决方案,遵循以下规则:

  • 我只在一个环境(WindowsXP)中执行了所有表单和小部件布局编辑,并将源代码传输到另一个环境(OSX)中进行编译和测试

  • 我创建了一个依赖于操作系统的通用函数,用于在运行时修改按钮高度和标签的字体大小(请参见下文),并在setupUI()之后从每个自定义对话框构造函数调用此函数,如下所示:

    someDialog::someDialog(QWidget*父对象):QDialog(父对象)
    {
    setupUi(这个);
    genAdjustWidgetAppearanceToOS(此);
    //…
    }

  • 我在genAdjustWidgetAppearanceToOS(this)函数中引入了一个异常列表, 并将我不想影响的所有控件的名称放入其中(没有什么是完美的)

  • 这是我的通用功能,用于检查它是否对您有帮助: (!请记住至少修改“DonotEffect”列表并附加标签/按钮名称)

    //======================================================
    //根据O/S调整特定小部件子项
    //=>设置按钮高度
    //=>设置标签字体大小
    // ======================================================
    void genAdjustWidgetAppearanceToOS(QWidget*rootWidget)
    {
    if(rootWidget==NULL)
    返回;
    QObject*child=NULL;
    QoObjectList容器;
    QObject*container=NULL;
    QStringList-DoNotAffect;
    //制作例外列表(不受影响的对象)
    DoNotAffect.append(“aboutTitleLabel”);//关于对话框
    DoNotAffect.append(“aboutVersionLabel”);//关于对话框
    DoNotAffect.append(“AboutCoyRightLabel”);//关于对话框
    DoNotAffect.append(“aboutUrlLabel”);//关于对话框
    DoNotAffect.append(“aboutLicenseLabel”);//关于对话框
    //根据操作系统设置大小:
    #ifdef Q_OS_MAC
    int ButtonHeight=32;
    int LabelsFontSize=12;
    #else//winxp/7
    int按钮高度=22;
    int LabelsFontSize=8;
    #恩迪夫
    //将根附加到容器
    Containers.append(rootWidget);
    而(!Containers.isEmpty())
    {
    container=Containers.takeFirst();
    if(容器!=NULL)
    {
    对于(int-ChIdx=0;ChIdxchildren().size();ChIdx++)
    {
    child=container->childs()[ChIdx];
    如果(!child->isWidgetType()| | donotafact.contains(child->objectName())
    继续;
    //将容器附加到堆栈以进行递归
    如果(子对象->子对象().size()>0)
    容器。附加(子);
    其他的
    {
    //将子对象强制转换为按钮和标签
    //(如果对象的类型不正确,则该对象将为NULL)
    QPushButton*button=qobject_cast(子对象);
    QLabel*label=qobject\u cast(子对象);
    如果(按钮!=NULL)
    {
    按钮->设置最小高度(按钮高度);//获胜
    按钮->设置最大高度(按钮高度);//赢
    按钮->设置策略(QSizePolicy::已修复,
    按钮->sizePolicy().horizontalPolicy());
    }
    else if(标签!=NULL)
    {
    QFont aFont=标签->字体();
    aFont.setPointSize(LabelFontSize);
    标签->设置字体(aFont);
    }
    }
    }
    }
    }
    }
    
    Hi。谢谢你的回复。我希望尽可能避免以任何方式更改字体,而是增加对话框本身的大小。我可以创建依赖于平台的.ui文件,但我有很多对话框,如果可能的话,我更喜欢更优雅的。谢谢。您可以随时在对话框上调用setSize()来增加它们的大小?
    // ======================================================
    // 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);
                        }
                    }
                }
            }
        }
    }