Results 1 to 13 of 13

Thread: how to copy QString content into array

  1. #1
    Join Date
    Nov 2007
    Posts
    57
    Thanks
    36
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Windows

    Default 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!

  2. #2
    Join Date
    Oct 2007
    Location
    Munich, Bavaria
    Posts
    144
    Thanks
    1
    Thanked 19 Times in 19 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default 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

  3. #3
    Join Date
    Nov 2007
    Posts
    57
    Thanks
    36
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Windows

    Default thanks DeepDiver

    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.

  4. #4
    Join Date
    Oct 2007
    Location
    Munich, Bavaria
    Posts
    144
    Thanks
    1
    Thanked 19 Times in 19 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: thanks DeepDiver

    What do you want to do with the array?
    QString already offers almost all manipulation operations.

  5. #5
    Join Date
    Nov 2007
    Posts
    57
    Thanks
    36
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Windows

    Default 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.

  6. #6
    Join Date
    Oct 2007
    Location
    Munich, Bavaria
    Posts
    144
    Thanks
    1
    Thanked 19 Times in 19 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default 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.....

  7. #7
    Join Date
    Nov 2007
    Posts
    57
    Thanks
    36
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Windows

    Default 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.

  8. #8
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: how to copy QString content into array

    How about:
    Qt Code:
    1. QString qstr = "xyz";
    2. std::string str = std::string(qstr.toLocal8Bit().constData());
    To copy to clipboard, switch view to plain text mode 

    or even:
    Qt Code:
    1. QString qstr = "xyz";
    2. std::string str = std::string(qPrintable(qstr));
    To copy to clipboard, switch view to plain text mode 

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

  9. The following user says thank you to wysota for this useful post:

    eric (14th November 2007)

  10. #9
    Join Date
    Feb 2006
    Location
    Oslo, Norway
    Posts
    6,264
    Thanks
    36
    Thanked 1,519 Times in 1,389 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows Symbian S60 Maemo/MeeGo

    Default Re: how to copy QString content into array

    Btw, QString has a specialized method for this:
    Qt Code:
    1. std::string str = qstr.toStdString();
    To copy to clipboard, switch view to plain text mode 
    J-P Nurmi

  11. The following user says thank you to jpn for this useful post:

    eric (14th November 2007)

  12. #10
    Join Date
    Nov 2007
    Posts
    57
    Thanks
    36
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Windows

    Default 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();

    }
    }

  13. #11
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: how to copy QString content into array

    Define "not doing anything"... What does "qDebug() << myqstring" output to the console (remember to include QtDebug)?

  14. #12
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    5,372
    Thanks
    28
    Thanked 976 Times in 912 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: how to copy QString content into array

    Quote Originally Posted by eric View Post
    The below code compiles but is not doing anything.
    You have two name variables and you change only one of them.

  15. The following user says thank you to jacek for this useful post:

    eric (15th November 2007)

  16. #13
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default 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:
    Qt Code:
    1. char name[50];
    2. QString str = "xxx";
    3. strncpy(name, str.toLocal8Bit().constData(), 50);
    To copy to clipboard, switch view to plain text mode 

  17. The following user says thank you to wysota for this useful post:

    eric (15th November 2007)

Similar Threads

  1. how to copy part of QString to anothe QString
    By nass in forum Qt Programming
    Replies: 1
    Last Post: 26th March 2007, 19:05
  2. Problem in converting QString to QChar array?
    By KaKa in forum Qt Programming
    Replies: 2
    Last Post: 19th March 2007, 00:38
  3. Convert from iso-8859-1 to... Something else :-)
    By Nyphel in forum Qt Programming
    Replies: 4
    Last Post: 7th March 2007, 17:59

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.