Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Converting char const* to QString
Forum Update on Monday, May 27th 2025

Converting char const* to QString

Scheduled Pinned Locked Moved General and Desktop
4 Posts 3 Posters 1.2k Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • C Offline
    C Offline
    chronix
    wrote on 22 Jan 2015, 08:45 last edited by
    #1

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

    1 Reply Last reply
    0
    • R Offline
      R Offline
      raven-worx
      Moderators
      wrote on 22 Jan 2015, 08:57 last edited by
      #2

      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

      --- SUPPORT REQUESTS VIA CHAT WILL BE IGNORED ---
      If you have a question please use the forum so others can benefit from the solution in the future

      1 Reply Last reply
      0
      • S Offline
        S Offline
        SGaist
        Lifetime Qt Champion
        wrote on 22 Jan 2015, 08:59 last edited by
        #3

        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 ?

        Interested in AI ? www.idiap.ch
        Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

        1 Reply Last reply
        0
        • C Offline
          C Offline
          chronix
          wrote on 22 Jan 2015, 09:05 last edited by
          #4

          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]

          1 Reply Last reply
          0

          1/4

          22 Jan 2015, 08:45

          • Login

          • Login or register to search.
          1 out of 4
          • First post
            1/4
            Last post
          0
          • Categories
          • Recent
          • Tags
          • Popular
          • Users
          • Groups
          • Search
          • Get Qt Extensions
          • Unsolved