6

I've seen several other posts about converting QString to std::string, and it should be simple. But somehow I'm getting an error.

My code is compiled into a VS project using cmake (I'm using VS express), so there's no issue with the QT libraries, and the GUI that I wrote works besides this part.

I have a QComboBox cb that holds the names to some objects, and a QLineEdit lineEdit that allows me to specify the name of the object that I am looking for. It should run a function that is tested and working when I press a go button, with the input from the QComboBox and the lineEdit as arguments.

Here's the code for when the go button is clicked:

void gui::on_go_clicked(){
    std::string str(cb->currentText().toStdString());
    //std::cout << str << "\n";
    //QString qstr = lineEdit->text();
    //std::cout<<lineEdit->text().toStdString();
    updateDB(str, lineEdit->text().toStdString());
}

The first line, creating str, works fine. I.E. there's no problem with library functions or toStdString(). But when it executes my function, the program breaks, and it's not becuase of the function, it's because of the part where it tries to convert lineEdit->text().toStdString().

This is only when I write the word "test" in the lineEdit box. I've seen other answers talking about unicode, which I tried briefly, but I can assume that the user will not be putting any special characters in the lineEdit box, barring '_' and '.', which shouldn't be unicode.

Steve The Squid
  • 63
  • 1
  • 1
  • 5
  • Also, I don't know if it matters, but I totally forget if I'm using QT4 or QT5. I got hopelessly messed up trying to get different versions of VS, Cmake, and QT to all work together (I'm an intern and had to set all this up myself). In my cmakelists.txt it says `FIND_PACKAGE(Qt4 REQUIRED)`. Actually, I don't think anything I'm doing is different between QT4 and QT5... – Steve The Squid Aug 19 '13 at 16:30
  • Okay I don't know what happened. Earlier when I tried `toUtf8().constData()`, it didn't work and I didn't want to use it because I didn't need unicode. I was just writing "test". But now when I use `toUtf8().constData()`, it works fine, and gets to my function. But does anyone know why I can't use `toStdString()`? I'd rather keep this simple. – Steve The Squid Aug 19 '13 at 18:07
  • I'm assuming the cout statement printed the wrong string? – Werner Erasmus Aug 25 '13 at 19:14

1 Answers1

9

The first line, creating str, works fine. I.E. there's no problem with library functions or toStdString(). But when it executes my function, the program breaks, and it's not becuase of the function, it's because of the part where it tries to convert lineEdit->text().toStdString().

Simplify your test and verify that QString.toStdString() does what you expect:

Therefore:

QString text = "Precise text that user would input";
std::cout << text.toStdString() << std::endl; //or...
qDebug() << text.toStdString().c_str();

Is the expected result produced?

If this is the case, it means your function has a problem. How does the updateDB look (function signature?)? What are its inputs?

From Qt help files:

The Unicode data is converted into 8-bit characters using the toUtf8() function.

Therefore, if you haven't changed the locale of your widget (lineEdit) or it's parents, things should just work (or you should see something with information loss). I've used this function many times without trouble...

Werner Erasmus
  • 3,988
  • 17
  • 31