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. [Solved] Help needed in using qstringlist with qfile and qbytearray !!!
QtWS25 Last Chance

[Solved] Help needed in using qstringlist with qfile and qbytearray !!!

Scheduled Pinned Locked Moved General and Desktop
6 Posts 2 Posters 1.8k 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.
  • S Offline
    S Offline
    srikanth.koorma
    wrote on last edited by
    #1

    Hi everyone, am a newbie to Qt Development. Actually I wanted to develop an application with the below requirement.

    Copying file contents which consists of some integer id's like 2201, 2255, 2386 etc. to a qstringlist and comparing each single element with a qbytearray function i.e qbytearray::contains. Can anyone of you help me with this please.

    I have written some code below with no error but couldn't get output, can't understand where the problem is.

    main.cpp file
    @
    #include "tcpsocket.h"
    #include <QApplication>

    int main(int argc, char *argv[])
    {
    QApplication a(argc, argv);
    tcpSocket w;
    w.show();

    // tcpSocket tcpsocket;
    // tcpsocket.Connect();

    return a.exec&#40;&#41;;
    

    }
    @

    demo.cpp file
    @#include "demo.h"
    #include "ui_demo.h"
    #include <QTcpSocket>
    #include <QAbstractSocket>
    #include <QFile>
    #include <QString>
    #include <QTextCodec>
    #include <QLabel>

    QFile ids("/home/chocky/Desktop/ids.txt");
    QFile ids_list("/home/chocky/Desktop/ids_list.txt");

    qint16 list_size,i;

    demo::demo(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::demo)
    {
    QString file;
    ids.open(QIODevice::ReadWrite | QIODevice::Text);
    file = QTextCodec::codecForMib(1015)->toUnicode(ids.readAll());
    QStringList list = file.split(".",QString::SkipEmptyParts);
    list_size=list.size();
    ui->label->setText(QString::number(list_size));
    ids.close();

    ui->label_2->setText(list.at(1).toUtf8());
    

    }

    demo::~demo()
    {
    delete ui;
    }
    @

    Application Output:
    Starting /home/chocky/Desktop/QtProjects/build-socket-Desktop_Qt_5_3_GCC_64bit-Debug/socket...
    The program has unexpectedly finished.
    /home/chocky/Desktop/QtProjects/build-socket-Desktop_Qt_5_3_GCC_64bit-Debug/socket crashed

    Compile Output:
    19:12:03: Running steps for project socket...
    19:12:03: Configuration unchanged, skipping qmake step.
    19:12:03: Starting: "/usr/bin/make"
    make: Nothing to be done for `first'.
    19:12:03: The process "/usr/bin/make" exited normally.
    19:12:03: Elapsed time: 00:00.

    1 Reply Last reply
    0
    • dheerendraD Offline
      dheerendraD Offline
      dheerendra
      Qt Champions 2022
      wrote on last edited by
      #2

      What is the content of file ? Can you paste it ? Any reason you are doing unicode etc ? Here either file or list is null. If your intent is just read and split according to ",", you can read the contents using readLine() and split the string using split function.

      Dheerendra
      @Community Service
      Certified Qt Specialist
      http://www.pthinks.com

      1 Reply Last reply
      0
      • S Offline
        S Offline
        srikanth.koorma
        wrote on last edited by
        #3

        Thanks for the reply Dheerendra, my ids.txt file contents are mentioned below.

        ids.txt
        @2103, 2204, 2296, 7485, 9375@

        As I have mentioned previously am a newbie to this c++ coding and all can you give any info regarding these unicode, binary and ascii etc., please.

        1 Reply Last reply
        0
        • dheerendraD Offline
          dheerendraD Offline
          dheerendra
          Qt Champions 2022
          wrote on last edited by
          #4

          So your error is very clear then. Look at your split. You are doing split through "."(dot). You should split your line through ","(comma)

          Try the following two lines. Replace your split @line 22 with following two lines.

          @ qDebug() << " Line =" << file <<endl;
          QStringList list = file.split(",",QString::SkipEmptyParts);@

          After this, let me know what happens.

          Dheerendra
          @Community Service
          Certified Qt Specialist
          http://www.pthinks.com

          1 Reply Last reply
          0
          • S Offline
            S Offline
            srikanth.koorma
            wrote on last edited by
            #5

            Thanks Dheerender it worked amazing. Actually mine was a typo error from typing ',' I typed a '.' . So now the issue is solved thanks alot. :)

            1 Reply Last reply
            0
            • dheerendraD Offline
              dheerendraD Offline
              dheerendra
              Qt Champions 2022
              wrote on last edited by
              #6

              cool. Move this issue to SOLVED state by modifying the subject line of this issue.

              Dheerendra
              @Community Service
              Certified Qt Specialist
              http://www.pthinks.com

              1 Reply Last reply
              0

              • Login

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