Converting char const* to QString



  • @MainWindow::~MainWindow()
    {
    delete ui;
    char const *TempName;
    TempName = getenv("USER");
    Name = QString(QLatin1String(TempName));
    //Name = QString::fromLocal8Bit(TempName);
    ui->label->setText(Name);
    }@

    I wrote the above code bt it dosent seem to work.QLable dosent change. Is there anything wrong I'm doing? I also tried the commented out line bt it also dosent work.
    Thanks.


  • Moderators

    this code doesn't run at all...

    when ui is a member of MainWindow, whats the sense setting the text of the label in the destructor?!

    in the first line you delete ui variable, but in the last line you are still accessing it

    Since this posted coded would definitely crash, i think you are running an old build of your application

    Edit: ;P


  • Lifetime Qt Champion

    Hi,

    QLatin1String is meant to be used with string literals which is not what TempName is.

    However, to simplifiy your code you can use qgetenv

    [edit: synchronous again:D]

    By the way, raven-worx's right, why do this in the desctructor ?



  • You are rite man. My bad I didn't see. So dumb of me. :(
    [quote author="raven-worx" date="1421917039"]this code doesn't run at all...

    when ui is a member of MainWindow, whats the sense setting the text of the label in the destructor?!

    in the first line you delete ui variable, but in the last line you are still accessing it

    Since this posted coded would definitely crash, i think you are running an old build of your application[/quote]


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.