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. Problem with QString storing file path
Forum Updated to NodeBB v4.3 + New Features

Problem with QString storing file path

Scheduled Pinned Locked Moved Unsolved General and Desktop
5 Posts 3 Posters 396 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
    MyNameIsQt
    wrote on last edited by
    #1

    This program manages and stores multiple image files.
    Select and open multiple files via QFiledialog. This file is passed to the background object created by list.
    At this time, the path of each file is also passed and saved.

        QFileDialog dialog(this, tr("Open File"));
        initializeImageFileDialog(dialog, QFileDialog::AcceptOpen);
        if(dialog.exec() == QDialog::Accepted){
            QStringList fileNames = dialog.selectedFiles();
            QString message = "Selected files: ";
            for (const QString& fileName : fileNames) {
               message += fileName + "\n";
            }
            for (const QString& fileName : fileNames){
                if(!m_pPubManager->AddList(fileName, *setImageBuffer(fileName))){
                     QMessageBox::information(nullptr, "File Open", "Error: This should be wrong dirname value. \n" + fileName);
                }
            }
    

    WPubManager manages the background as a list. list<CBackground *> m_pList;
    And add:

    bool WPubManager::AddList(const QString &fileName, const QImage &img)
    {
        m_properties.count = cnt_list++;
        if(!fileName.isNull()){
            m_pBkgnd = new CBackground(cnt_list, fileName, img);
            if(m_pBkgnd){
                m_pList.push_back(m_pBkgnd);
                return true;
            }
        }
        return false;
    }
    

    The background constructor saves the passed file path and outputs it for testing in the constructor function. The output is correct.

    CBackground::CBackground(unsigned int cnt, const QString &fileName, const QImage &img) :
        m_pBkgndHeader{0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},  
        m_strBkgndImagePath(&fileName)
    {
       ....
        cout << cnt << "\t" << m_strBkgndImagePath->toStdString() << "\t" << size.width() << endl;  // output correct file path value
    }
    

    However, if you try to output the file path while traversing the object as follows, the file path is broken. An unknown character value is output.

    void WPubManager::GetNext()
    {
        if (++pos != m_pList.end()) {
            m_pBkgnd = *(pos);        
            cout << m_pBkgnd->getBkgndPath().toStdString() <<  endl;
        } else {
            cout << "Reached the end of the book." <<  endl;
            pos--;
        }   
    }
    

    Is this a unicode related issue? how can i solve it?

    jsulmJ Christian EhrlicherC 2 Replies Last reply
    0
    • M MyNameIsQt

      This program manages and stores multiple image files.
      Select and open multiple files via QFiledialog. This file is passed to the background object created by list.
      At this time, the path of each file is also passed and saved.

          QFileDialog dialog(this, tr("Open File"));
          initializeImageFileDialog(dialog, QFileDialog::AcceptOpen);
          if(dialog.exec() == QDialog::Accepted){
              QStringList fileNames = dialog.selectedFiles();
              QString message = "Selected files: ";
              for (const QString& fileName : fileNames) {
                 message += fileName + "\n";
              }
              for (const QString& fileName : fileNames){
                  if(!m_pPubManager->AddList(fileName, *setImageBuffer(fileName))){
                       QMessageBox::information(nullptr, "File Open", "Error: This should be wrong dirname value. \n" + fileName);
                  }
              }
      

      WPubManager manages the background as a list. list<CBackground *> m_pList;
      And add:

      bool WPubManager::AddList(const QString &fileName, const QImage &img)
      {
          m_properties.count = cnt_list++;
          if(!fileName.isNull()){
              m_pBkgnd = new CBackground(cnt_list, fileName, img);
              if(m_pBkgnd){
                  m_pList.push_back(m_pBkgnd);
                  return true;
              }
          }
          return false;
      }
      

      The background constructor saves the passed file path and outputs it for testing in the constructor function. The output is correct.

      CBackground::CBackground(unsigned int cnt, const QString &fileName, const QImage &img) :
          m_pBkgndHeader{0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},  
          m_strBkgndImagePath(&fileName)
      {
         ....
          cout << cnt << "\t" << m_strBkgndImagePath->toStdString() << "\t" << size.width() << endl;  // output correct file path value
      }
      

      However, if you try to output the file path while traversing the object as follows, the file path is broken. An unknown character value is output.

      void WPubManager::GetNext()
      {
          if (++pos != m_pList.end()) {
              m_pBkgnd = *(pos);        
              cout << m_pBkgnd->getBkgndPath().toStdString() <<  endl;
          } else {
              cout << "Reached the end of the book." <<  endl;
              pos--;
          }   
      }
      

      Is this a unicode related issue? how can i solve it?

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

      @MyNameIsQt Do you paths contain any non-ascii characters?

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

      M 1 Reply Last reply
      0
      • jsulmJ jsulm

        @MyNameIsQt Do you paths contain any non-ascii characters?

        M Offline
        M Offline
        MyNameIsQt
        wrote on last edited by
        #3

        @jsulm The files are randomly selected. There can be several types.

        jsulmJ 1 Reply Last reply
        0
        • M MyNameIsQt

          @jsulm The files are randomly selected. There can be several types.

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

          @MyNameIsQt This does not answer my question...

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

          1 Reply Last reply
          2
          • M MyNameIsQt

            This program manages and stores multiple image files.
            Select and open multiple files via QFiledialog. This file is passed to the background object created by list.
            At this time, the path of each file is also passed and saved.

                QFileDialog dialog(this, tr("Open File"));
                initializeImageFileDialog(dialog, QFileDialog::AcceptOpen);
                if(dialog.exec() == QDialog::Accepted){
                    QStringList fileNames = dialog.selectedFiles();
                    QString message = "Selected files: ";
                    for (const QString& fileName : fileNames) {
                       message += fileName + "\n";
                    }
                    for (const QString& fileName : fileNames){
                        if(!m_pPubManager->AddList(fileName, *setImageBuffer(fileName))){
                             QMessageBox::information(nullptr, "File Open", "Error: This should be wrong dirname value. \n" + fileName);
                        }
                    }
            

            WPubManager manages the background as a list. list<CBackground *> m_pList;
            And add:

            bool WPubManager::AddList(const QString &fileName, const QImage &img)
            {
                m_properties.count = cnt_list++;
                if(!fileName.isNull()){
                    m_pBkgnd = new CBackground(cnt_list, fileName, img);
                    if(m_pBkgnd){
                        m_pList.push_back(m_pBkgnd);
                        return true;
                    }
                }
                return false;
            }
            

            The background constructor saves the passed file path and outputs it for testing in the constructor function. The output is correct.

            CBackground::CBackground(unsigned int cnt, const QString &fileName, const QImage &img) :
                m_pBkgndHeader{0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},  
                m_strBkgndImagePath(&fileName)
            {
               ....
                cout << cnt << "\t" << m_strBkgndImagePath->toStdString() << "\t" << size.width() << endl;  // output correct file path value
            }
            

            However, if you try to output the file path while traversing the object as follows, the file path is broken. An unknown character value is output.

            void WPubManager::GetNext()
            {
                if (++pos != m_pList.end()) {
                    m_pBkgnd = *(pos);        
                    cout << m_pBkgnd->getBkgndPath().toStdString() <<  endl;
                } else {
                    cout << "Reached the end of the book." <<  endl;
                    pos--;
                }   
            }
            

            Is this a unicode related issue? how can i solve it?

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

            @MyNameIsQt said in Problem with QString storing file path:

            CBackground::CBackground(unsigned int cnt, const QString &fileName, const QImage &img) :
            m_pBkgndHeader{0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
            m_strBkgndImagePath(&fileName)

            Hello dangling pointer... c++ basics.

            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
            2

            • Login

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