Results 1 to 2 of 2

Thread: QLabel ui->name_pc->setText(nam);

  1. #1
    Join Date
    Jul 2012
    Location
    Cape Town, South Africa
    Posts
    1
    Thanks
    1
    Qt products
    Qt4 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default QLabel ui->name_pc->setText(nam);

    Hi All,
    Apologies for the simple issue, but this is my first "outing" with Qt, and I haven't written any C code for about 7 years.
    Not sure if I have a C problem or a Qt usage problem.

    I'm getting the PC netbios name and then wanting to display it on mu ui.
    When I use hostname, I get an error relating to incorrect data type. The debugger does show that hostname contains my PC name.
    I then (think that I) point a QSting variable to the address of hostname, and only the first character of hostname appears on my ui.

    Please can someone spot my deliberate mistake.

    Thanks & regards,
    Zoltan.

    #include "mainwindow.h"
    #include "ui_mainwindow.h"
    #include "windows.h"

    MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
    {
    ui->setupUi(this);
    WCHAR hostname[12];
    QString nam = "";
    DWORD len;
    len = 12;
    hostname[11] = '\0';
    GetComputerName(hostname,&len);
    nam = (char *)&hostname[0];
    ui->name_pc->setText(nam);
    }

    MainWindow::~MainWindow()
    {
    delete ui;
    }

  2. #2
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,729
    Thanks
    13
    Thanked 1,610 Times in 1,537 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Default Re: QLabel ui->name_pc->setText(nam);

    You are mixing wide characters (WCHAR) and 8-bit characters (char). Casting the point from WCHAR* to char* does not change the data in the array. So, the name "FRED" appears in the hostname memory as bytes (chars and hex):
    Qt Code:
    1. F R E D \0
    2. 46 00 52 00 45 00 44 00 00 00
    To copy to clipboard, switch view to plain text mode 
    if you treat that as an 8-bit char string the best you will get is "F". You need to produce your QString from the WCHAR array: try QString::fromWCharArray().

    You may also find that QHostInfo::localHostName() gives you a usable name (though different) for your purpose.
    "We can't solve problems by using the same kind of thinking we used when we created them." -- Einstein
    If you are posting code then please use [code] [/code] tags around it - makes addressing the problem easier.

  3. The following user says thank you to ChrisW67 for this useful post:

    Zoltan (16th July 2012)

Similar Threads

  1. QLabel->setText() cause memory incerase
    By gozdemir in forum Qt for Embedded and Mobile
    Replies: 1
    Last Post: 11th July 2011, 11:14
  2. QLabel->setText() sometimes not working
    By cass in forum Newbie
    Replies: 8
    Last Post: 16th May 2010, 13:12
  3. QLabel setText Problem
    By pmabie in forum Qt Programming
    Replies: 10
    Last Post: 1st November 2007, 23:32
  4. Problem with QLabel and setText
    By jambrek in forum Qt Programming
    Replies: 7
    Last Post: 31st October 2007, 16:02
  5. QLabel::setText() how to
    By freak in forum Qt Programming
    Replies: 1
    Last Post: 30th October 2006, 17:19

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.