PDA

View Full Version : QString -> char*



scwizard
21st February 2007, 19:01
I understand how important internationalization is etc. But I really need some way to convert the QString returned by getOpenFileName to a char* so I can call this function from this library I'm using.

I'm guessing that the way I'm converting the string is incorrect because the "not a map" about box triggers. However I don't know of any function that will echo back a char* C style string to me.

I tried including windows.h and saying: MessageBox(NULL, "A NORMAL STRING DAMINT", "MESSAGE", MB_OK); the way I used to do things when I coded in C. But I get "cannot convert const char* to const WCHAR*"

What the hell is a WCHAR???


void MainWindow::open() {
MPQHANDLE *MPQarchive;

QString FileString = QFileDialog::getOpenFileName(this, tr("Open File"), "/",
tr("Starcraft maps (*.scm *.scx *.chk);;All Files (*.*)"));
if(FileString.endsWith(".scm") || FileString.endsWith(".scx")) {
if(!SFileOpenArchive((char*)FileString.data(), 1, 0, MPQarchive)) {
QMessageBox::about(this, tr("Not a map?"), FileString);
}
}
}

jpn
21st February 2007, 19:11
How can I convert a QString to char* and vice versa ? (http://www.trolltech.com/developer/knowledgebase/faq.2007-01-30.9032238253/)

scwizard
21st February 2007, 19:47
Thanks, stuff works now.

But out of curiosity is there a way to echo back a char* using some kind of messagebox?

wysota
21st February 2007, 20:41
But out of curiosity is there a way to echo back a char* using some kind of messagebox?


const char *string = "xxxxxxxxxxxxxxxx";
QMessageBox::information(0, "title", string);

jacek
21st February 2007, 20:55
I tried including windows.h and saying: MessageBox(NULL, "A NORMAL STRING DAMINT", "MESSAGE", MB_OK); the way I used to do things when I coded in C. But I get "cannot convert const char* to const WCHAR*"

What the hell is a WCHAR???
WCHAR is a wide character used in Unicode strings. Qt uses Unicode internally, so if you include any windows' header, it will also have Unicode support turned on.

Try:
MessageBox(NULL, _T("A NORMAL STRING DAMINT"), _T("MESSAGE"), MB_OK);(you might need to include tchar.h or something similar).