using QSizePolicy to set size according to content size



  • i'm trying to make start/pause menu over the game screen
    it has different text before the game starts and during the pause so i want to change its size depends on situation
    my code is:

        QWidget *menu;
        QVector <QLabel*> menu_cursor_lines;
        QVector <QLabel*> menu_items_lines;
        QHBoxLayout *menu_layout;
        QVBoxLayout *menu_items_layout;
        QVBoxLayout *menu_cursor_layout;
        int menu_lines = 3;
    
    ********************************************************
    
        menu = new QWidget(grview);
        //menu->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);
    
        for (int i = 0; i < menu_lines; i++)
        {
            QLabel *menu_cursor_line = new QLabel;
            menu_cursor_line->setFont(f);
            //menu_cursor_line->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);
            menu_cursor_lines.append(menu_cursor_line);
            QLabel *menu_item_line = new QLabel;
            menu_item_line->setFont(f);
            //menu_item_line->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);
            menu_items_lines.append(menu_item_line);
        }
    
        for (int i = 0; i < menu_lines; i++)
        {
            menu_cursor_lines[i]->setText("");
            //menu_cursor_lines[i]->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);
            //menu_items_lines[i]->setText("                                             ");
            menu_items_lines[i]->setText("");
            //menu_items_lines[i]->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);
        }
        menu_cursor_lines[0]->setText("➜");
    
        menu_cursor_layout = new QVBoxLayout;
        menu_items_layout = new QVBoxLayout;
        menu_layout = new QHBoxLayout;
    
        menu_cursor_layout->setSpacing(20);
        for (int i = 0; i < menu_cursor_lines.size(); i++)
            menu_cursor_layout->addWidget(menu_cursor_lines[i]);
        menu_items_layout->setSpacing(20);
        for (int i = 0; i < menu_items_lines.size(); i++)
            menu_items_layout->addWidget(menu_items_lines[i]);
        menu_layout->addLayout(menu_cursor_layout);
        menu_layout->addLayout(menu_items_layout);
        menu->setLayout(menu_layout);
        menu->setStyleSheet("background-color: Grey;");
    
    

    commented SetSizePolicies is where i've tried them
    i've tried all possible flags and result was always the same - see pictures
    0_1510305487145_menu1.png
    0_1510305505568_menu2.png

    so is it possible to do this using QSizePolicy or not and if it is so how to do it?


  • Moderators

    @DrageFabeldyr Isn't the menu resized automatically if text length changes?



  • @jsulm no, as you see on pictures doesn't matter what text is written the size is minimal
    and as i understand it uses this text to set size:

        for (int i = 0; i < menu_lines; i++)
        {
            menu_cursor_lines[i]->setText("");
            menu_items_lines[i]->setText("");
        }
        menu_cursor_lines[0]->setText("➜");
    

    so as you see from pictures the size is set with the first SetText mention and then doesn't change

    only solution i've found is to write it this way:

        for (int i = 0; i < menu_lines; i++)
        {
            menu_cursor_lines[i]->setText("");
            menu_items_lines[i]->setText("                                             ");
        }
    

    and now it's ok with the game start:
    0_1510307785529_menu 3.png
    but not ok with pause (it's too large):
    0_1510307707246_menu 4.png


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.