Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

QT5.15.2 on Android: How to disable Qt::AA_ForceRasterWidgets for child dialogs?



  • I've made an application for Android where I need to set the attribute Qt::AA_ForceRasterWidgets to get the native screen resolution without any scaling. This is working as expected. However. I have a settings dialog created with QTDesigner also. This dialog looks really ugly now because the font is too big and the child's are too small (look at the picture blow). Is there a way to disable the consequences derived from the attribute Qt::AA_ForceRasterWidgets just for such a dialog?

    Screenshot

    The code I use:

    int qtmain(int argc, char **argv, TPageManager *pmanager)
    {
        pageManager = pmanager;
    
    #ifdef Q_OS_ANDROID
        QApplication::setAttribute(Qt::AA_ForceRasterWidgets);
    #endif
    
        QApplication app(argc, argv);
    ...
    

    And for the dialog:

    void MainWindow::settings()
    {
        TQtSettings *dlg_settings = new TQtSettings(this);
    #ifdef __ANDROID__
        dlg_settings->setAttribute(Qt::WA_NativeWindow);   // <-- Doesn't work!!
    #endif
        int ret = dlg_settings->exec();
    
        if (ret && dlg_settings->hasChanged())
            writeSettings();
        else if (!ret && dlg_settings->hasChanged())
        {
            TConfig cf(TConfig::getConfigPath() + "/" + TConfig::getConfigFileName());
        }
    }
    


  • I found a solution. The answer is simply to calculate a scale factor and resize everything. The dialog window as well as all elements in this dialog. Here is an example of how to do this:

    void TQtSettings::doResize()
    {
        // The main dialog window
        QSize size = this->size();
        QRect rect = this->geometry();
        size.scale(scale(size.width()), scale(size.height()), Qt::KeepAspectRatio);
        this->resize(size);
        this->move(scale(rect.left()), scale(rect.top()));
        QWidget *parent = this->parentWidget();
    
        if (parent)
        {
            rect = parent->geometry();
            this->move(rect.center() - this->rect().center());
        }
    
        // labels
        QFont font = ui->label_Channel->font();
        font.setPointSizeF(12.0);
        size = ui->label_Channel->size();
        size.scale(scale(size.width()), scale(size.height()), Qt::KeepAspectRatio);
        ui->label_Channel->resize(size);
        rect = ui->label_Channel->geometry();
        ui->label_Channel->move(scale(rect.left()), scale(rect.top()));
        ui->label_Channel->setFont(font);
    [...]
    }
    
    int TQtSettings::scale(int value)
    {
        if (value <= 0 || mScaleFactor == 1.0)
            return value;
    
        return (int)((double)value * mScaleFactor);
    }
    

    There may be a more elegant way by iterating through the elements instead of writing spaghetti code. However. The above code just demonstrate how to do it.
    This results in a dialog like this (for the colours blame the poor theme on the VM):
    dialog window