Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Adding widgets to a vector outside of a class constructor
QtWS25 Last Chance

Adding widgets to a vector outside of a class constructor

Scheduled Pinned Locked Moved Unsolved General and Desktop
2 Posts 2 Posters 381 Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • D Offline
    D Offline
    DevonS
    wrote on last edited by
    #1

    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 clicks addField or removeField

    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 the removeField button also works just fine. But if I try to add a row by clicking addField, the program acts as if I have pushed a new sectionRowFrame 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 click removeField again, I have to click it until all the invisible frames have been removed, then click again before a visible frame gets removed

    How can I get the new rows to be added and to be visible?

    jsulmJ 1 Reply Last reply
    0
    • D DevonS

      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 clicks addField or removeField

      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 the removeField button also works just fine. But if I try to add a row by clicking addField, the program acts as if I have pushed a new sectionRowFrame 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 click removeField again, I have to click it until all the invisible frames have been removed, then click again before a visible frame gets removed

      How can I get the new rows to be added and to be visible?

      jsulmJ Offline
      jsulmJ Offline
      jsulm
      Lifetime Qt Champion
      wrote on last edited by
      #2

      @DevonS Call show() on the new sectionRowFrame

      https://forum.qt.io/topic/113070/qt-code-of-conduct

      1 Reply Last reply
      3

      • Login

      • Login or register to search.
      • First post
        Last post
      0
      • Categories
      • Recent
      • Tags
      • Popular
      • Users
      • Groups
      • Search
      • Get Qt Extensions
      • Unsolved