Results 1 to 6 of 6

Thread: Problem in conversion of QChar to char

  1. #1
    Join Date
    Apr 2010
    Posts
    4
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Problem in conversion of QChar to char

    Hi Everyone,

    I know this topic has been discussed a lot in last decade but believe me I am posting my problem after reading all that stuff.

    Motive of my program:
    Fetch a file (basically image file) from the directory which I am doing using getOpenNewFile, which returns the name of file in QString format.
    I want to convert that QString name in char* format.

    Now the Problem
    I have applied all techniques:
    1) str[i] = static_cast<char>(Qstr.at(i).toAscii())
    2) str[i] = static_cast<char>(Qstr.at(i).toLatin1())
    and similarly for whole string.
    but no success. My program is taking the file name successfully but returns segmentation fault just after that.

    Please do tell me if there's any other method for doing this.
    Thanks in advance.

    Regards,
    Prashant

  2. #2
    Join Date
    Mar 2008
    Location
    Kraków, Poland
    Posts
    1,536
    Thanked 284 Times in 279 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Problem in conversion of QChar to char

    Please show us real code.

  3. #3
    Join Date
    Apr 2010
    Posts
    4
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: Problem in conversion of QChar to char

    // fetching the file from directory
    QString file = QFileDialog::getOpenFileName(this,tr("open file"),QDir::currentPath());
    // showing file path on QLineEdit
    file_path->setText(file);
    // the file path is in form of QString
    QString qt_file_name = file_path->text();
    char *file_name;

    for(int i=0;i<qt_file_name.length();i++)
    {
    // used each statement one by one
    file_name[i] = static_cast<char>(qt_file_name.at(i).toAscii()); // causing segmentation fault
    file_name[i] = static_cast<char>(qt_file_name.at(i).toLatin1()); //causing segmentation fault
    }

  4. #4
    Join Date
    Apr 2010
    Posts
    4
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: Problem in conversion of QChar to char

    now I have replaced for loop with this line
    strcpy(file_name,qt_file_name.toAscii());
    but still the answer is same ,i.e, SEGMENTATION FAULT

  5. #5
    Join Date
    Apr 2010
    Posts
    4
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: Problem in conversion of QChar to char

    could anyone please tell what's the reason of segmentation fault
    because according to Qt's documentation the above statements should work fine

    whenever I am making the conversion statements a comment, the program is running fine
    but as soon as I include those statements, all the havoc starts happening

  6. #6
    Join Date
    Mar 2008
    Location
    Kraków, Poland
    Posts
    1,536
    Thanked 284 Times in 279 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Problem in conversion of QChar to char

    Because You don't initialise file_name variable. If You realy need this conversion (I don't see any reason for this) You must do this like that
    Qt Code:
    1. char *file_name = new char[200];
    To copy to clipboard, switch view to plain text mode 
    or
    Qt Code:
    1. char file_name[200];
    To copy to clipboard, switch view to plain text mode 
    It is C++ abc-book

Similar Threads

  1. Char* <-> QString implicit conversion
    By kingfinn in forum Qt Programming
    Replies: 3
    Last Post: 15th March 2010, 08:26
  2. char* to QString: conversion
    By abghosh in forum Qt Programming
    Replies: 9
    Last Post: 8th March 2010, 09:32
  3. Conversion Char Array to string
    By anafor2004 in forum Newbie
    Replies: 6
    Last Post: 6th May 2008, 14:35
  4. Conversion from unsigned char* to unsigned char
    By santosh.kumar in forum General Programming
    Replies: 1
    Last Post: 6th August 2007, 13:12
  5. Problem in converting QString to QChar array?
    By KaKa in forum Qt Programming
    Replies: 2
    Last Post: 19th March 2007, 00:38

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.