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. Accessing value of vector only once
QtWS25 Last Chance

Accessing value of vector only once

Scheduled Pinned Locked Moved Solved General and Desktop
headervectorperformancefunction
3 Posts 3 Posters 406 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.
  • M Offline
    M Offline
    MEsc
    wrote on last edited by
    #1

    Hello, I have a Question.

    For example:
    void loadfrom() loads values from file into a vector

    I have another function MainWindow::on_start_clicked()
    When this Function get called (here via button),
    values should load in vec.

    QVector<QString> vec = loadfrom();

    And in another function
    MainWindow::on_answer_returned()
    At the moment everytime this function get called the vector loads all values everytime. But I want access the vector in this function from MainWindow::on_start_clicked()

    How can I do this?

    Christian EhrlicherC 1 Reply Last reply
    0
    • C Offline
      C Offline
      ChrisW67
      wrote on last edited by
      #3

      @MEsc said in Accessing value of vector only once:

      I have another function MainWindow::on_start_clicked()
      When this Function get called (here via button),
      values should load in vec.
      QVector<QString> vec = loadfrom();

      That looks like vec is a variable local to the slot attached to the button. You load it and then it is destroyed when the slot exits. If you want the vec object to have a longer lifetime then you need to arrange that. As @Christian-Ehrlicher says, making vec a private member variable of the MainWindow class is probably the right approach. So,

      class MainWindow: public QMainWindow {
      ...
      private:
        QVector<QString> m_vec
      };
      
      void MainWindow::on_start_clicked() {
      ...
        m_vec = loadfrom();
      ...
      }
      
      void MainWindow::on_answer_returned() {
      ...
        // do stuff with m_vec
      ...
      }
      

      This is basic C++ knowledge and not Qt specific.

      1 Reply Last reply
      2
      • M MEsc

        Hello, I have a Question.

        For example:
        void loadfrom() loads values from file into a vector

        I have another function MainWindow::on_start_clicked()
        When this Function get called (here via button),
        values should load in vec.

        QVector<QString> vec = loadfrom();

        And in another function
        MainWindow::on_answer_returned()
        At the moment everytime this function get called the vector loads all values everytime. But I want access the vector in this function from MainWindow::on_start_clicked()

        How can I do this?

        Christian EhrlicherC Offline
        Christian EhrlicherC Offline
        Christian Ehrlicher
        Lifetime Qt Champion
        wrote on last edited by
        #2

        @MEsc said in Accessing value of vector only once:

        How can I do this?

        Do not load it again when it's already loaded I would guess. Maybe by storing it in a member variable?

        Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
        Visit the Qt Academy at https://academy.qt.io/catalog

        1 Reply Last reply
        1
        • C Offline
          C Offline
          ChrisW67
          wrote on last edited by
          #3

          @MEsc said in Accessing value of vector only once:

          I have another function MainWindow::on_start_clicked()
          When this Function get called (here via button),
          values should load in vec.
          QVector<QString> vec = loadfrom();

          That looks like vec is a variable local to the slot attached to the button. You load it and then it is destroyed when the slot exits. If you want the vec object to have a longer lifetime then you need to arrange that. As @Christian-Ehrlicher says, making vec a private member variable of the MainWindow class is probably the right approach. So,

          class MainWindow: public QMainWindow {
          ...
          private:
            QVector<QString> m_vec
          };
          
          void MainWindow::on_start_clicked() {
          ...
            m_vec = loadfrom();
          ...
          }
          
          void MainWindow::on_answer_returned() {
          ...
            // do stuff with m_vec
          ...
          }
          

          This is basic C++ knowledge and not Qt specific.

          1 Reply Last reply
          2

          • Login

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