reggie
reggie

Reputation: 626

Is there a way in Qt to get all widgets on a form?

For example I have 20 widgets on a form. I need to resize them based on screen resolution so here's my method

newHeight=(desktopHeight * ui->widget1->height())/768;
newWidth=(desktopWidth * ui->widget1->width())/1024;
newY=(desktopHeight * ui->widget1->y())/768;
newX=(desktopWidth * ui->widget1->x())/1024;
ui->widget1->setGeometry(newX,
              newY,
              newWidth,
              newHeight);
newFontSize=(desktopHeight * ui->widget1->font().pointSize())/768;
ui->widget1->setFont(QFont ("Ubuntu",newFontSize, QFont::Bold));

And I will repeat this method for the remaining 19 widgets. Is there a way to get all of the widgets and create a do while statement and create a function that the widgets are the parameter?

Upvotes: 2

Views: 14461

Answers (2)

mdoran3844
mdoran3844

Reputation: 688

Are all the widgets attached to a form or window?

You can just grab all the child widgets from their UI parent widget and iterate over the collection of children.

Depending on your widget hierarchy you should just do something like

QObjectList *widgetList = parentWidget->findChildren();

In your specific case:

QObjectList *widgetList = ui->centralWidget->findChildren();

Edit: Without the rest of your code I have no idea what ui represents, hence my generic answers. I was assuming your ui was a MainWindow as follows in my code

MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
{
        ui->setupUi(this);

        QObjectList widgetList = ui->centralWidget->children();
        qDebug() << "Amount of children found :" << widgetList.count();

}

Upvotes: 4

NG_
NG_

Reputation: 7181

reggie! Don't you think that you are doing it wrong?

About layouts

Your approach is used for very specific cases, because there are ready-to-use from the box solution in Qt, called Layout management.

Here you can read about it: Layout Management, also, see example of usage and intuitive how-to use it in Qt designer

About fonts: There is QApplication::setFont, so you can change font program-wide. But in official documentation you can find:

This function lets you override the default font; but overriding may be a bad idea because, for example, some locales need extra large fonts to support their special characters.

Upvotes: 3

Related Questions