Home > other >  QHBoxLayout removeWidget don't take effect
QHBoxLayout removeWidget don't take effect

Time:05-08

Statement: QHBoxLayout * hboxProgress; QLabel * white_label; QProgressBar * remember_label; Constructors in the initialization: hboxProgress=new QHBoxLayout (); HboxProgress - & gt; AddWidget (white_label); HboxProgress - & gt; AddStretch (1); HboxProgress - & gt; SetSpacing (40); HboxProgress - & gt; SetContentsMargins (0, 0, 0, 0); White_label=new QLabel (); White_label - & gt; SetFixedSize (160, 10); Remember_label=new QProgressBar (); Remember_label - & gt; SetFixedSize (160, 10); Remember_label - & gt; SetRange (0100); Remember_label - & gt; SetValue (0); M_state=false; The login button corresponding functions: void LoginDialog: : verify () {if (! M_state) {title_label - & gt; SetText (tr (" 111 "));//is always valid hboxProgress - & gt; RemoveWidget (white_label); HboxProgress - & gt; AddWidget (remember_label);//switch effectively for the first time, after the invalid hboxProgress - & gt; Invalidate ().//and still invalid hboxProgress - & gt; The update ();//and still invalid m_state=true; } else {title_label - & gt; SetText (tr (" 222 "));//is always valid hboxProgress - & gt; RemoveWidget (remember_label); HboxProgress - & gt; AddWidget (white_label);//the problem here: never worked (or at least has never been shown) hboxProgress - & gt; Invalidate ().//and still invalid hboxProgress - & gt; The update ();//and still invalid m_state=false; }} in fact, I just want to do a login box, but at the time of validation of user data, showing a progress bar, or display a Label, but QHBoxLayout dynamic Settings when the Widget is not successful, please instruct greatly, thank you very much!

CodePudding user response:

Show/hide don't you?

CodePudding user response:

I put you said this effect achieved, close test and correct... In the UI white_label and remember_label you said in the same layout, switch display code is as follows: the Widget: : widgets (QWidget * parent) : QWidget (parent), UI (new UI: : Widget) {UI - & gt; SetupUi (this); flag=false; The UI - & gt; HorizontalLayout - & gt; RemoveWidget (UI - & gt; Label);//hide the label init. UI - & gt; The label - & gt; SetVisible (false); }//here I use a slot to realize switching display void Widget: : on_pushButton_clicked () {if (! Flag) {UI - & gt; HorizontalLayout - & gt; RemoveWidget (UI - & gt; ProgressBar); The UI - & gt; HorizontalLayout - & gt; AddWidget (UI - & gt; Label); The UI - & gt; ProgressBar - & gt; SetVisible (false); The UI - & gt; The label - & gt; SetVisible (true); } else {UI - & gt; HorizontalLayout - & gt; RemoveWidget (UI - & gt; Label); The UI - & gt; HorizontalLayout - & gt; AddWidget (UI - & gt; ProgressBar); The UI - & gt; ProgressBar - & gt; SetVisible (true); The UI - & gt; The label - & gt; SetVisible (false); } flag=! Flag; }

CodePudding user response:

Thank you upstairs, according to your inspiration, I also added the setVisible function, originally because of it, but this is enough trouble, the switch is not enough, also manually changing control reality, don't know QT has its reason to do so? But as a developing KDE software library, ought to admire it, rather than doubt,,, PS, maybe can consider to use QStackedLayout

CodePudding user response:

Qlayout is responsible for managing the qwidget size, location, not to control component is visible or not

CodePudding user response:

After removing the widget from the layout, also need to this widget using setParent (null)
  • Related