Qt Forum

    • Login
    • Search
    • Categories
    • Recent
    • Tags
    • Popular
    • Users
    • Groups
    • Search
    • Unsolved

    QVector: no match for 'operator+'

    General and Desktop
    4
    8
    3448
    Loading More Posts
    • 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.
    • S
      srivatsa last edited by

      I am passing a QVector from one window to another, I want to append the value present in QVector from previous window to a QString in present window. I get the error when I perform the addition @no match for 'operator+'@

      Here is my code:

      Window1.cpp

      @void SelectOS::processNextButton()
      {
      if(ui->win32->isChecked()){
      QString path;
      path = qApp->applicationDirPath()+"/WIN/32Bit";
      so->osName.push_back(path);
      SelectSoftware *ss = new SelectSoftware();
      this->hide();
      ss->show();
      }
      }

      QVector<QString> SelectOS::getosName(){
      so = new SelectOS();
      return so->osName;
      }@

      Window2.cpp

      @void SelectSoftware::getSoftwareDetails()
      {

      SelectOS *so = new SelectOS();
      SelectSoftware *ss = new SelectSoftware();
      ss->os = so->getosName();
      
      QString fileName = ss->os + "/" +SOFTWARELIST; // Here I get the error...
      
      QFile file&#40;fileName&#41;;
      if (!file.open(QIODevice::ReadOnly | QIODevice::Text)){
          QString msg = "Could not find the file " + fileName;
          errorExit(msg);
      }
      
      QTextStream in(&file);
      while (!in.atEnd()) {
          QString line = in.readLine();
          processLine(line.toLower());
      }
      

      }@

      1 Reply Last reply Reply Quote 0
      • Q
        QtTony last edited by

        If SOFTWARELIST is defined as a C-style string, you can just leave out the plus sign, and the compiler will concatenate them:

        QString fileName = ss->os + "/" SOFTWARELIST;

        1 Reply Last reply Reply Quote 0
        • raven-worx
          raven-worx Moderators last edited by

          according to the docs QVector only has the operator+ defined for other vectors and not for it's types it is holding.

          There are 3 possibilities for you now ;)

          1. use the operator<<
          2. use the append-Method of QVector
          3. or write your own operator+

          Something like this (not tested):

          @
          QVector<T> & operator+ (QVector<T> &vector, const T &value)
          {
          return (vector << value);
          }
          @

          --- SUPPORT REQUESTS VIA CHAT WILL BE IGNORED ---
          If you have a question please use the forum so others can benefit from the solution in the future

          1 Reply Last reply Reply Quote 0
          • P
            prady_80 last edited by

            If you want to add to QVector use qppend() or if your intention is to to add to a vector operator[] with appropriate index.
            In your case,
            ss->os[0] + "/" +SOFTWARELIST;

            By the way, why are you returning a vector?? If its simple QString You could very well use a QStringList.

            1 Reply Last reply Reply Quote 0
            • S
              srivatsa last edited by

              I want to pass list of String variables from one Window to another, which one i need to use? QList type or QString or QVector?

              For example, SOFTWARELIST is a simple .txt file, the list of directories will be iterated from that list and will be displayed, each directory having checkbox[i] and combobox[i]. I need to send this list to new window.. How can I do this??

              1 Reply Last reply Reply Quote 0
              • P
                prady_80 last edited by

                QStringList objListofDirs;
                while you have not reached the end of file {
                Get a file line in var QString strLine;
                objListofDirs<<strLine; // This will append to QStringList

                }

                So return QStringList or a reference to it from your function which will be input to the other window function which can then iterate the list and give you values

                Iteration should be
                for (int i = 0; i < objListofDirs.size(); ++i) {
                //Do whatever with the directory
                }

                1 Reply Last reply Reply Quote 0
                • S
                  srivatsa last edited by

                  Well, in the other WIndow, the constructor must be of QStringList type, is it? Or I can pass to any Constructor? Actually in my application, there are total of 8 Windows, except first window, each and every window has Back and Next actions, and I am calling new Class of Each window when moving backward and forward, if the argument is missing or empty, the application is crashing...

                  1 Reply Last reply Reply Quote 0
                  • P
                    prady_80 last edited by

                    You have a list of strings that you can use any way.
                    If it suits your application logic pass a const QStringList reference in the constructor and access the data by iteration to either suitably store in your data structure or as is.
                    Since your application crashes you need to put memory guards / checks before accessing any memory

                    1 Reply Last reply Reply Quote 0
                    • First post
                      Last post