Adding widgets to a vector outside of a class constructor
-
Hi there, first post here
I have a simple application I'm making and I'm trying to set the behavior of buttons to where the user can add or remove text entry fields as needed. To facilitate this, I have a vector, and each row of fields that the user can add or delete is a class:
sectionRowFrame
, that is pushed to or popped from the vector when the user clicksaddField
orremoveField
In the constructor of its parent class, I have this loop:
list = std::vector<sectionRowFrame*>(); for (int i = 0; i < 5; i++){ addRow(); }
and at the end of the constructor, I have this block of code which handles the adding/removing of rows and connections between buttons and behaviour:
//some previous code in the class constructor QObject::connect(removeField, SIGNAL(clicked()), this, SLOT(removeRow())); QObject::connect(addField, SIGNAL(clicked()), this, SLOT(addRow())); } void textEnterFrame::removeRow(){ if (this->list.size() == 0){ return; } delete this->list.at(this->list.size() - 1); this->list.pop_back(); } void textEnterFrame::addRow(){ int index = this->list.size(); this->list.push_back(new sectionRowFrame(this)); this->list.at(index)->setGeometry(5, (index + 1) * 35, 500, 30); }
My problem is this: when calling
addRow()
in the parent constructor, everything works fine, and the window appears with the five sections like I expect it to. Removing rows by clicking theremoveField
button also works just fine. But if I try to add a row by clickingaddField
, the program acts as if I have pushed a newsectionRowFrame
into the vector, and I can see that through print statements in the console when I debug, but nothing actually appears in the window. Then when I clickremoveField
again, I have to click it until all the invisible frames have been removed, then click again before a visible frame gets removedHow can I get the new rows to be added and to be visible?