2017年3月21日 星期二

Qt中文亂碼問題

Problem

   若想直接在Qt上打中文,常會遇到亂碼問題。嘗試了網路上蠻多方法都失敗,本文總結出解決亂碼的方法。

Solution

情境1

想在 Source Code (*.cpp) 裡面寫中文;但當其顯示在UI上時,卻呈現亂碼。
如:
QString newInfo("你好");
this->displayPanel->myQLabel->setText(newInfo);

解決方法請見這篇文章


情境2

如果有一已編譯好的函示庫(假設為輸入員工ID,則該函示庫會輸出員工姓名),該函示庫的輸出型態為std::string,其編碼為BIG-5(以下皆假設在Windows作業系統下)。若欲將此型態轉為QString,則需以下面方式即可:
namespace QtExtensions
{
    QString ConvertToUTF8(const std::string& big5FormatString_)
    {
        QString result = QString::fromLocal8Bit(big5FormatString_.c_str());

        return result;
    }
}//End of namespace

而在使用時:
std::string GetStaffName(const std::string& staffID_);

int main()
{
    std::string staffNameInBig5Format = ::GetStaffName("01234567");
    QString staffNameInUTF8 = QtExtensions::ConvertToUTF8(staffNameInBig5Format);
    staffInfoWindow.SetStaffName(staffNameInUTF8);
}

Can't create 64bits virtual machine on VirtualBox

Problem

    I have a exported ubuntu 14.10 64bits that run on VirtualBox.  After I upgrade my host OS (i.e. win7 to win10), that exported ubuntu will crash when I try to boot it.
    Furthermore, when I want to create a new virtual machine, it does not have the "64bits" options.  I know this issue!  Normally, this problem can be solved by Enable the Virtualization Settings in BIOS.  However, I found the Virtualization Settings is still enabled...

Solution

    After several searches, I found this article.  What causes this problem is: Only one virtual machine manager (hypervisor) is valid at a time, but we have Two (one is our VirtualBox, the other is Hyper-V that used by Microsoft (I still wonder if it has to do with the virtual machine of my HoloLens?)).  Although I can't change the settings as the above article did (my OS is win10), the comments of that article provide the solution to turn off the Hyper-V:

bcdedit /set hypervisorlaunchtype off
After typing the above commands in command line and reboot, the VirtualBox can create the 64bits OS and execute it successfully.  However, I found that I should type the above command everytime  I reboot my computer.  Finally, I solve this by removing the "Android-" something that relates to the VisualStudio...

No picture on display (black screen) after Updating Win10

Problem

    After the first update of win 10 (Professional, maybe), I got No Picture On Display (even when I wait for a long time). While the computer seems to be still running, though.  By forcing the computer to reboot (usually, I need to reboot many times), the computer can recover to its last status (i.e. I can use my computer again, but the update seems not applied at all...).  This bothers me for a long time...

Solution

    Recently, I can't stand anymore and begin to face the problem.  Finally, I found it might be caused by the driver issue of my Graphic Card (the win7 driver not compatible with and win10?).  Anyway, by removing the Graphic Card and reboot, I saw the updating procedure after reboot.  When it finished, I put my Graphic Card back.  And Everything works fine.

The History of my Computer

    I start from win7, and install win10 Home (by buying from vendor).  After then, I buy win10 Professional online (to enable the development of HoloLens).

My Hardware

CPU: Intel R. i7-4790K
Motherboard: GIGABYTE Z97MX-Gaming 5
Graphics Card: MSI GTX780Ti