Adding layout to Qdialog changes the layout in an other Qdialog



  • Hi,
    I have 2 QDialogs: Additem and Review.

    //Add buttons
               ReviewButton = new QPushButton(tr("&Review"));
    
               ReviewButton->setStyleSheet ("QPushButton{"
                                            "background-color: rgb(0, 255, 0);"
                                            "border-style: outset;"
                                            "border-width: 2px;"
                                            "border-radius: 10px;"
                                            "border-color: beige;"
                                            "font: bold 14px;"
                                            "min-width: 10em;"
                                            "padding: 6px;}"
                                            );
            ReviewButton->setToolTip ("<b><font size='12'><font color = 'blue'>Click to review your entries.</font></b>");
    		ReviewButton->setFixedWidth (100);
    
    
            connect(ReviewButton,SIGNAL(clicked(bool)),this,SLOT(review()));
    
            reset_Button = new QPushButton(tr("&Reset"));
    
            reset_Button->setStyleSheet ("QPushButton{"
                                            "background-color: rgb(255, 0, 0);"
                                            "border-style: outset;"
                                            "border-width: 2px;"
                                            "border-radius: 10px;"
                                            "border-color: beige;"
                                            "font: bold 14px;"
                                            "min-width: 10em;"
                                            "padding: 6px;}"
                                            );
    
            reset_Button->setToolTip ("<b><font size='12'><font color = 'blue'>Click to reset all the fields. This will close the window and clear all your entries.</font></b>");
    		reset_Button->setMaximumWidth (100);
            button_Layout->addWidget (ReviewButton);
            button_Layout->addWidget (reset_Button);
    
            connect (reset_Button,SIGNAL(clicked(bool)),this,SLOT(resetAll()));
    
    //Add title
    
            QLabel* title = new QLabel;
            title->setText("Add a Friend To the Database");
            title->setAlignment(Qt::AlignCenter);
            QFont f( "Arial", 18, QFont::Bold);
            title->setFont(f);
    
    //Add layout
    
            titleLayout->addWidget (title);
    
            scroll->setWidgetResizable (true);
            scroll->setVerticalScrollBarPolicy (Qt::ScrollBarAlwaysOn);
            scroll->setHorizontalScrollBarPolicy (Qt::ScrollBarAlwaysOn);
    
            grid->setColumnMinimumWidth (1,20);
            grid->setVerticalSpacing (15);
            grid->verticalSpacing ();
    
            name_Layout->addWidget (LineEdit_Name);
            name_Layout->addWidget (angry_image_Label);
            name_Layout->addWidget (incorrect_Label);
            name_Layout->insertSpacing (1,15);
            name_Layout->insertSpacing (2,15);
    
            QWidget *viewport = new QWidget(this);
            scroll->setWidget (viewport);
            scroll->setWidgetResizable (true);
            spacer1->setText ("             ");
    
    		grid->addWidget (Label_ID,0,0,1,1,Qt::AlignTop);
            grid->addWidget (ID_Display,0,1,1,1,Qt::AlignTop);
            grid->addWidget (Label_Name,1,0,1,1,Qt::AlignTop);
            grid->addLayout (name_Layout,1,1,1,1,Qt::AlignTop);
    
            grid->addLayout (button_Layout,3,1,1,1,Qt::AlignCenter);
    
            scroll->setLayout (grid);
    
    
            mainLayout->addLayout (titleLayout);
            mainLayout->addWidget (scroll);
    
            Additem::setLayout (mainLayout);
    
    }
    
    void Additem::readAndValidate()
    {
        name = LineEdit_Name->text ();
    
        angry_image_Label->setFixedSize (30,30);
        incorrect_Label->setStyleSheet("QLabel { font-size:18px; color: red; }");
    
                     if(name.length ()<2)
                       {
                          incorrect_Label->setText ("The name is too short!");
    
                          QPixmap pix("C:/Programming/Projects/Folkfriends/icons/angry.png");
    
                           angry_image_Label->setScaledContents (true);
                           angry_image_Label->setPixmap (pix);
                       }
                       else
                       {
                           incorrect_Label->setText ("");
                           QPixmap pix2("C:/Programming/Projects/FolkFriends/icons/Check-icon.png");
                           angry_image_Label->setScaledContents (true);
                           angry_image_Label->setPixmap (pix2);
                       }
    
                   qDebug() << " Name in function: " << name;
    
    }
    
    void Additem::review()
    {
        revName_Label->setText ("<font size = '12' color = 'blue'>"+name+"</font>");
    
        Review->setWindowTitle ("Review Friend's Data");
    
        QFont f( "Arial", 18, QFont::Bold);
        QFont g( "Arial", 16, QFont::Normal);
    
        rev_Title_Lbl->setFont (f);
        rev_Title_Lbl->setText ("Review the Data You Entered");
        rev_Title->addWidget (rev_Title_Lbl,Qt::AlignHCenter);
    
        rev_Grid->addLayout (rev_Title,0,0,1,1,Qt::AlignTop);
        rev_Grid->addWidget (Label_ID,2,0,1,1,Qt::AlignTop);
        rev_Grid->addWidget (ID_Display,2,1,1,1,Qt::AlignTop);
        rev_Grid->addWidget (Label_Name,3,0,1,1,Qt::AlignTop);
        rev_Grid->addWidget (revName_Label,3,1,1,1,Qt::AlignTop);
    
    //    Review->setLayout (rev_VLayout);
    
    //    rev_VLayout->addLayout (rev_Grid);
    
        Review->exec ();
    }
    
    void Additem::resetAll()
    {
       qDebug()  << "Resetting";
    }
    

    When I add

    //    Review->setLayout (rev_VLayout);
    
    //    rev_VLayout->addLayout (rev_Grid);
    

    Review displays everything correctly, but Additem changes its layout:
    name_Layout moves from position 1,1 to 0,0 and covers up Label_ID.
    Please help me figure out why.
    Thank you.


  • Lifetime Qt Champion

    Hi,

    How are you creating these two layouts ?



  • @SGaist
    Hi,
    Using grid layout.



  • Same happens when I use QHBoxLayout in Review which is a QDialog.


  • Lifetime Qt Champion

    Can you share the code where you create and setup these two layouts ?



  • @SGaist
    Hi,
    Here is the code:
    additem.cpp
    Thank you for your help.



  • @gabor53 :

    You change the widget geometry in a way it affects hints. so layout has to be updated.
    From the top of my head it might be enough to call the following updateGeometry() on every affected widget. Aka:

    Actually I take it back.
    If you could show your ui->setupUi(this); code I could probably say more, but as for now it seems you are trying to use the same layout in different dialogs. Make sure you do not.


Log in to reply