Such a question, I add the following components dynamically to the form:

struct sbj_window_data{ QGroupBox *sbj; QLabel *sbj_time,*sbj_locate,*sbj_type,*sbj_name,*sbj_week; }; QVector<sbj_window_data*> dyn_container; 

I write them into a structure, and put it in a vector. The sbj_locate should be on the right in the Group box, and for this I added a resizeEvent, which goes over the vector, and exposes the desired Label on the right in the GroupBox.

 void MainWindow::resizeEvent(QResizeEvent*){ for(int i=0;i<dyn_container.size();i++){ dyn_container[i]->sbj_locate->move(dyn_container[i]->sbj->width()-10-dyn_container[i]->sbj_locate->width(),10); } 

} But when this Event is used only for those Label that are located in the current tab, when switching to other tabs, everything changes only when the window is changed. Therefore, I added the following signal, which when switching to another tab, performs the same as resizeEvent ();

 QObject::connect(my_tab_widget, SIGNAL(currentChanged(int)), this, SLOT(tabSelected())); void MainWindow::tabSelected(){ for(int i=0;i<dyn_container.size();i++){ dyn_container[i]->sbj_locate->move(dyn_container[i]->sbj->width()-10-dyn_container[i]->sbj_locate->width(),10); } 

}

Everything works, but they are tormented by doubts that this is not correct. Please tell me if this code is correct, and if not, how best to do?

  • 2
    Use better layout: QVBoxLayout, QHBoxLayout or QGridLayout instead of manually placing it on the form via move - gil9red
  • I use, I need to place a label in a certain form in a QGroupBox. - Astalavista
  • I do not see the use of the layout (layout) in question :) - gil9red

0