吾爱破解 - LCG - LSG |安卓破解|病毒分析|www.52pojie.cn

 找回密码
 注册[Register]

QQ登录

只需一步,快速开始

查看: 1040|回复: 23
收起左侧

[求助] QT编译咋还是乱码?

[复制链接]
冥界3大法王 发表于 2022-1-2 16:27
test.pro
内容如下:
[C++] 纯文本查看 复制代码
#-------------------------------------------------
#
# Project created by QtCreator 2021-12-30T20:59:13
#
#-------------------------------------------------

QT       += core gui

greaterThan(QT_MAJOR_VERSION, 4): QT += widgets

TARGET = test2
TEMPLATE = app

# The following define makes your compiler emit warnings if you use
# any feature of Qt which as been marked as deprecated (the exact warnings
# depend on your compiler). Please consult the documentation of the
# deprecated API in order to know how to port your code away from it.
DEFINES += QT_DEPRECATED_WARNINGS

# You can also make your code fail to compile if you use deprecated APIs.
# In order to do so, uncomment the following line.
# You can also select to disable deprecated APIs only up to a certain version of Qt.
#DEFINES += QT_DISABLE_DEPRECATED_BEFORE=0x060000    # disables all the APIs deprecated before Qt 6.0.0


SOURCES += main.cpp\
        mainwindow.cpp

HEADERS  += mainwindow.h

FORMS    += mainwindow.ui

win32: LIBS += -lpsapi



MainWindow.h
[Asm] 纯文本查看 复制代码
#ifndef MAINWINDOW_H
#define MAINWINDOW_H

#include <QMainWindow>

namespace Ui {
class MainWindow;
}

class MainWindow : public QMainWindow
{
    Q_OBJECT

public:
    explicit MainWindow(QWidget *parent = 0);
    ~MainWindow();

private slots:
    void on_pushButton_clicked();

    void on_pushButton_2_clicked();

    void on_comboBox_currentIndexChanged(const QString);

private:
    Ui::MainWindow *ui;
};

#endif // MAINWINDOW_H


main.cpp
[Asm] 纯文本查看 复制代码
#include "mainwindow.h"
#include <QApplication>
#include <QTextCodec>


int main(int argc, char *argv[])
{
    QTextCodec *codec = QTextCodec::codecForName("UTF-8");           =======================>加了,编译还是乱码,坑爹啊。
    QTextCodec::setCodecForLocale(codec);
    QApplication a(argc, argv);
    MainWindow w;
    w.show(); 

    return a.exec();
}


mainwindows.cpp
[Asm] 纯文本查看 复制代码
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <windows.h>
#include <psapi.h>
#include <QClipboard>

MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
{
    ui->setupUi(this);
}

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

void MainWindow::on_pushButton_clicked()
{
    DWORD pid;
        char buf[MAX_PATH];
        HWND h=GetForegroundWindow();
        GetWindowThreadProcessId(h,&pid);
        HANDLE hProc=OpenProcess(PROCESS_ALL_ACCESS,false,pid);
        GetModuleFileNameExA(hProc,(HMODULE)0,buf,sizeof(buf));
        CloseHandle(hProc);
        ui->lineEdit->setText(buf);
        setWindowTitle(buf);
        qApp->clipboard()->setText(buf);
}

void MainWindow::on_pushButton_2_clicked()
{
        ui->comboBox->addItem("中国");
        ui->comboBox->addItem("美国");
        ui->comboBox->addItem("日本");
}

void MainWindow::on_comboBox_currentIndexChanged(const QString)
{
//ui->comboBox->currentText();
    ui->lineEdit->setText(ui->comboBox->currentText()); //把获取到的comboBox值传给lineEdit
}


免费评分

参与人数 1吾爱币 +1 收起 理由
xouou + 1 你把工程打包发上来我们试试

查看全部评分

发帖前要善用论坛搜索功能,那里可能会有你要找的答案或者已经有人发布过相同内容了,请勿重复发帖。

only998 发表于 2022-1-2 16:35
其实cpp文件本身的编码也很重要,看下cpp的编码是utf8的没。你可以试下直接给一个utf8编码的const char* 看看怎么样。
亡骨 发表于 2022-1-2 16:54
beavailable 发表于 2022-1-2 17:01
duohappy 发表于 2022-1-2 17:57
1. 用qt creator
2. 在pro 文件中加上
win32-msvc* {
    QMAKE_CXXFLAGS += /source-charset:utf-8 /execution-charset:utf-8
}
3. 所有源文件加上pro文件统一使用 utf-8 无bom

基本不会遇到乱码问题
endriver 发表于 2022-1-2 18:19
乱码也是我当初玩QT时,经常出现的问题
qwwpr 发表于 2022-1-2 18:55
编码问题?
 楼主| 冥界3大法王 发表于 2022-1-2 19:05
duohappy 发表于 2022-1-2 17:57
1. 用qt creator
2. 在pro 文件中加上
win32-msvc* {

@duohappy 按道友说的
加了,win10上还是乱码。
 楼主| 冥界3大法王 发表于 2022-1-2 19:09
亡骨 发表于 2022-1-2 16:54
可以试试使用
QString::fromLocal8Bit("中国");
@亡骨
换成QT的不会改,求现成的。

乱码也是我当初玩QT时,经常出现的问题
@endriver
是啊,这个狗屎工具不是驴不走,就是磨不转。
若是换成Delphi一上午搞的就差不多有点意思了,这个浪费了很多的时间。
 楼主| 冥界3大法王 发表于 2022-1-2 19:22
image.png
双击也不转到报错行,什么破工具。
您需要登录后才可以回帖 登录 | 注册[Register]

本版积分规则 警告:本版块禁止回复与主题无关非技术内容,违者重罚!

快速回复 收藏帖子 返回列表 搜索

RSS订阅|小黑屋|处罚记录|联系我们|吾爱破解 - LCG - LSG ( 京ICP备16042023号 | 京公网安备 11010502030087号 )

GMT+8, 2024-4-24 19:14

Powered by Discuz!

Copyright © 2001-2020, Tencent Cloud.

快速回复 返回顶部 返回列表