PDA

View Full Version : how to copy QString content into array



eric
13th November 2007, 19:39
Hello!

I have a simple dialog which asks user to input text. The dialog is pretty much straight from the assistant (http://doc.trolltech.com/4.3/qinputdialog.html):

bool ok;
QString text = QInputDialog::getText(this, tr("QInputDialog::getText()"),
tr("User name:"), QLineEdit::Normal,
QDir::home().dirName(), &ok);
if (ok && !text.isEmpty())
textLabel->setText(text);

My question is: How do you copy what's in the "text" into a character array (say: myArray) so that it could be used and manipulated by the program.

Have a good day!

DeepDiver
13th November 2007, 20:53
It's always a good starting point to have a look at the Qt Documentation (http://doc.trolltech.com/4.3/index.html)
Especially at the class you are interested: QString

Good luck

eric
13th November 2007, 21:30
Thanks, DeepDiver, for your help

But I have looked through the QString documentation. I was mostly experimenting with:

int QString::toWCharArray ( wchar_t * array ) const

but never got it to works since I'm a beginner. Sorry.

DeepDiver
13th November 2007, 21:35
What do you want to do with the array?
QString already offers almost all manipulation operations.

eric
13th November 2007, 21:53
I'd like the user to type in a name and then I use this name as a file name to open a new file. So far I've been able to type in the name and display it on the screen using QLabel but I can't put the same text in my character array. Thanks again.

DeepDiver
13th November 2007, 21:55
Again: why would you need the text in a char array?
Qt has file io classes. Filenames are taken as QString.

Please tell us more.....

eric
13th November 2007, 22:27
Ok, let's just assume that I want to put whatever is typed in into a string. Let's forget about opening of new files (you are right, there are simpler ways of doing it). What I want to do is to make a string for for my own programming purpose. For example: reverse the order of letters and display that, or something else of that kind. I'm just trying to learn here.
What is the easiest way to catch the text and put it into a string?
Sorry about the confusion.

wysota
14th November 2007, 00:19
How about:

QString qstr = "xyz";
std::string str = std::string(qstr.toLocal8Bit().constData());

or even:

QString qstr = "xyz";
std::string str = std::string(qPrintable(qstr));

BTW. You can reverse the order of letters "or do stuff like that" on a QString as well...

jpn
14th November 2007, 07:43
Btw, QString has a specialized method for this:


std::string str = qstr.toStdString();

eric
14th November 2007, 20:35
Thanks to all who have helped me.
The below code compiles but is not doing anything. I must have misunderstood totally how to do std::string str = qstr.toStdString();


char name[50];
QString myqstring; // this is in the "private" of the MyWidget class


void MyWidget::getNewName()
{
bool ok;
filenametext = QInputDialog::getText(this, tr("Hello"),
tr("Enter"), QLineEdit::Normal,
QDir::home().dirName(), &ok);

if (ok && !filenametext.isEmpty())
{
fileLabel->setText(filenametext);
myqstring = filenametext;
std::string name = myqstring.toStdString();

}
}

wysota
14th November 2007, 22:39
Define "not doing anything"... What does "qDebug() << myqstring" output to the console (remember to include QtDebug)?

jacek
14th November 2007, 22:55
The below code compiles but is not doing anything.
You have two name variables and you change only one of them.

wysota
14th November 2007, 23:13
...and the other is not even a string. To copy the content of QString into the buffer, you'll need something like this:

char name[50];
QString str = "xxx";
strncpy(name, str.toLocal8Bit().constData(), 50);