how to copy QString content into array
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!
Re: how to copy QString content into array
It's always a good starting point to have a look at the Qt Documentation
Especially at the class you are interested: QString
Good luck
Re: how to copy QString content into array
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.
Re: how to copy QString content into array
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.....
Re: how to copy QString content into array
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.
Re: how to copy QString content into array
How about:
Code:
std::string str = std::string(qstr.toLocal8Bit().constData());
or even:
Code:
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...
Re: how to copy QString content into array
Btw, QString has a specialized method for this:
Code:
std::string str = qstr.toStdString();
Re: how to copy QString content into array
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();
}
}
Re: how to copy QString content into array
Define "not doing anything"... What does "qDebug() << myqstring" output to the console (remember to include QtDebug)?
Re: how to copy QString content into array
Quote:
Originally Posted by
eric
The below code compiles but is not doing anything.
You have two name variables and you change only one of them.
Re: how to copy QString content into array
...and the other is not even a string. To copy the content of QString into the buffer, you'll need something like this:
Code:
char name[50];
strncpy(name, str.toLocal8Bit().constData(), 50);