Important: Please read the Qt Code of Conduct -

Bit shifts

  • Hi,

    How can I do a bit shift in Qt? The analogue in c++ is presented bellow as a part of code:

    @while(file >> i)

    Thanks in advance.

  • Qt libs are usually used with C++. So you can do this as you do in C++.
    Or do you mean something specific?

  • The special staff is that in Qt file is @QFile@ and i is @int@

  • Lifetime Qt Champion


    You are not very clear. Are you trying to get data from a file or do bit shifting ?

  • I am trying to calculate the elements in the file. For more clarity see the code below:

    @int kmean_algorithm::calculateElementsInFile()
    int numberOfElements = 0;
    int fileIterator
    QFile file("...\iris.csv");
    QMessageBox::warning(0, "Error", file.errorString());
    while(file >> fileIterator)
    numberOfElements ++;

    The code provided is wrong and I realize it, as >> is from fstream (if I loaded the file with standard c++ as follows ifstream file(filename); there would be no problem), as I load the file using QFile it means that file >> fileIterator is impossible w.r.t the following error about types inequality:

    bq. error: no match for 'operator>>' (operand types are 'QFile' and 'int')

    Q: How can I make >> in my case to work? Any suggestion? Alternatives?

  • Lifetime Qt Champion

    Since it's a csv, you have a file text you could use "QTextStream":

    But, if I have understood you correctly you want to count all the elements contained in your csv. You could read your file line by line in a QString, and use split with the delimiter to get the actual count of the elements per line.

  • create a subclass of QFile with the bit shift operators.

    An easier way to get the count of either lines, or entries per line would do what SGaist suggested where you read the file in line by line into multiple buffers or the whole file into one giant buffer. Then use QString's split function with either the , or \n as your delimiter. The split function returns a QStringList which contains a count.

Log in to reply