conversion from 'QString' to non-scalar type 'std::string' requested

I tried a bunch of different things and I still can not figure out how to get my code working. Pretty much what I have is a gui that is going to encrypt a .txt file that I open and display that into a textEdit box. Here is the code I have and my output issues (only really line 57 I neeed help with):

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include "time.h"
#include "stdlib.h"
#include "string"
#include "iostream"

using namespace std;
string encrypt(string old)
{
    int iSecret;



    srand (time(0));



    iSecret = rand() % 100000 + 1;


    string crypted;
    crypted.resize(old.length());
int i;
    for(i = 0; i < old.length(); ++i)
    {
        if(old[i] >64 && old[i] < 123)
            crypted[i] = old[i]*iSecret+iSecret*3/iSecret-iSecret/4+250;
    }
    return crypted;
}

MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
{
    ui->setupUi(this);

}

MainWindow::~MainWindow()
{
    delete ui;
}
int MainWindow::on_pushButton_clicked()
{
    QString file = QFileDialog::getOpenFileName(this,"Open a file");
    if(!file.isEmpty()){
        QFile sFile(file);
        if(sFile.open(QFile::ReadOnly | QFile::Text)){
            mFilename = file;
            QTextStream in(&sFile);
            QString text = in.readAll();

            QString words = in.readLine();

            ui->textEdit->setPlainText(encrypt(words));
            sFile.close();

            return(0);





        }

    }

}


I get this message when I try and build. It's on line 57
conversion from 'QString' to non-scalar type 'std::string' requested
also get an "issue" but doesn't seem to affect it and that is on line 25 and says
Comparison between signed and unsigned integer expressions

Thanks in advance.
regarding your QString error. on line 57 'words' is a QString. But the function 'encrypt' takes a std::string as a parameter.

std::string and QString are two different and unrelated types. So this is causing an error. You'll either need to change the function to take a QString, or you'll need to convert 'words' to a std::string when you pass it to the function.

I'm not familiar with Qt, but from a quick check of the docs, I found this routine to convert from QString to std::string:
http://qt-project.org/doc/qt-4.8/qstring.html#toStdString


As for the warning about signed/unsigned.... the warning pretty much explains it. 'i' is a signed integer, and old.length() is an unsigned size_t, so there's a slight risk of loss of information during the comparison (though it would only happen if the length is super crazy big).

You can probably safely ignore that warning... or cast old.length() to an int to silence it. But if you want to do a proper fix, then change 'i' to be a size_t instead of an int.
Thanks and I changed up my encryption formula so that it would take char instead but I can't figure out how to convert QString into char I can only turn it into a char* by using this code:
char ch[] = {content.toAscii().data()};
EDIT:
If you could help me get the characters from inside the QString content to turn into "char" I would appreciate I can only figure out how to convert to "char*" been trying for like 3 hours now getting annoying.


btw whats the difference between (or different purposes of) Ascii, Latin1, and Utf8?

thanks.

ohh and the new encryption code is:

1
2
3
4
5
6
7
8
9
10
11
char ch[] = {content.toAscii().data()};
ch[0] = ch[0]+3;

/*IF SOMEONE ELSE WANTS TO USE THIS AND THEY HAVE MORE THAN ONE
THING IN THEIR ARRAY THEY WILL HAVE TO USE A LOOP LIKE THIS:*/

char ch[] = {contents.toAscii().data()};
int i;
for(i=0;i<sizeof(contents)/sizeof(contents[0]);++i){
ch[i] = ch[i] +3;
}
Last edited on
Well to make the ch a * and get rid of the error all I had to do was put
char * ch[]
BUUUUUUUTTTT then my function doesn't work because it needs the characters not what ever the char* is I think its a pointer so doesn't actually get the characters from the array. I think what I need to do is change the contents.toAscii().todata()} to something else if anyone could help would appreciate
I changed up my encryption formula so that it would take char instead


Why? Strings are easier to to work with.

If you could help me get the characters from inside the QString content to turn into "char" I would appreciate I can only figure out how to convert to "char*" been trying for like 3 hours now getting annoying.


A "char" is a single character. You cannot fit an entire string into a single character.

A "char*" can point to a series of characters, hence why there are conversions to char* but not to char.

If you don't understand the difference (or even if you do), save yourself a headache and just use strings.

btw whats the difference between (or different purposes of) Ascii, Latin1, and Utf8?


They're different character encodings. ASCII is the base character set (codes 0-127) which has the Latin/English alphabet, numerals, and common punctuation. But if you want foreign characters like japenese glyphs, Greek letters, etc, etc, you have to go beyond that set. So there are other encodings each which cover different areas.

Utf8 is the "Unicode" format, which is all-inclusive (that is, all existing glyphs can be represented in a UTF8 string).


for(i=0;i<sizeof(contents)/sizeof(contents[0]);++i){


This will not work. You should never use sizeof() to get the lenght of a string. It's even a poor choice to get the size of an array.

Again. Do yourself a favor and just use strings. When dealing with string data, it only makes sense to use strings.
Last edited on
I would use strings but I don't know how to get them to work into my function I would have to put them into my array but it only lets me put char's into arrays the function is something like this
char ch[2]={content};

so what would I put instead of {}?

thanks again for the help
oh and char* is defineitly a pointer and not array because if I cout that it shows a random number and if I cout a normal char it shows the letter
I would use strings but I don't know how to get them to work into my function


You can use the [] operator with strings, just as you do with a char array.

1
2
3
4
5
6
std::string foo = "this is a string";

foo[0] = 'T';
foo[1] = 'S';

cout << foo;  // prints "TSis is a string" 


You can also get the length of the string with foo.length() and can append things to the end of a string with + and += operators. It's extremely intuitive.

so what would I put instead of {}?


Nothing. There is no way to assign an existing string variable/object to a char array on construction. You'd have to do a strcpy() to copy the string data over... but even then that means you'd have to dynamically allocate the char array because you don't know how big the string data is at compile time. Which means you run risk of buffer overflows, memory corruption, memory leaks, etc. It's a can of worms and is extremely error prone.

std::string does all of that automatically. It's very easy to use and I'm sure it does everything you'd need it to do. Just use it.
Okay I'm going to see if I can try and get this working by using a loop in place of the 0 and 1.

hopefully I can get this program finally working lol making a gui is harder than it seems.
OKay sweet thanks I get how to put the string into the array now, I rewrote my encryption code to this:

1
2
3
4
5
6
7
8
9
10
11
12
13
#include "iostream"

int main()
{
    std::string std1 = "Hello World!";
    int i;
    std::cout << "origonal text: " << std1 << std::endl;
    for(i=0; i<std1.length(); i++){
        std1[i] = std1[i]+1;
    }
    std::cout << "encrypted text: " << std1 << std::endl;
    return(0);
}



hopefully I can just put my something like this content.toStdString() in place of "hello world!" I'll let you know if it works, thanks again for all the help


EDIT:
well the formula you gave me works on putting new strings into the encrypter now I just have to convert that string value which was a Qstring that I made into a string back into a QString so I can use it in my gui lol pain in the ass having to do all these conversions
Last edited on
Got it working thanks again!! I really appreciate if anyone wants my code here it is:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
void MainWindow::on_Encrypt_released()
{
    QString filename = QFileDialog::getOpenFileName();

    QFile file(filename);
     if (!file.open(QIODevice::ReadOnly | QIODevice::Text))
         return;

    QString content = file.readAll();
    file.close();
    ui->text1->setText(content);






    std::string std1 = content.toStdString();

    int i;
  
    for(i=0; i<std1.length(); i++){
        std1[i] = std1[i]+1;
    }
    QString text = std1.c_str();
  

  
    ui->text2->setText(text.toUtf8());


}

Topic archived. No new replies allowed.