吾爱破解 - 52pojie.cn

 找回密码
 注册[Register]

QQ登录

只需一步,快速开始

查看: 1517|回复: 24
收起左侧

[求助] QT程序又烦病了,读取INI时,字符串是数字或中文或特殊字符只要不是英文就读取不出来

[复制链接]
冥界3大法王 发表于 2022-8-25 22:39
本帖最后由 冥界3大法王 于 2022-8-25 22:49 编辑

QT程序又烦病了,读取INI时,字符串是数字或中文或特殊字符只要不是英文就读取不出来
[C++] 纯文本查看 复制代码
#include "SimpleTraceDialog.h"
#include "ui_SimpleTraceDialog.h"
#include "Bridge.h"
#include <QMessageBox>
#include "BrowseDialog.h"
#include "MiscUtil.h"
#include <QSettings>
//#pragma execution_character_set("utf-8");
#include <QDir>

SimpleTraceDialog::SimpleTraceDialog(QWidget* parent) :
    QDialog(parent),
    ui(new Ui::SimpleTraceDialog)
{
    ui->setupUi(this);
    setWindowFlags(windowFlags() & ~Qt::WindowContextHelpButtonHint | Qt::MSWindowsFixedSizeDialogHint);
    duint setting;
    if(!BridgeSettingGetUint("Engine", "MaxTraceCount", &setting))
        setting = 50000;
    ui->spinMaxTraceCount->setValue(int(setting));
    ui->editBreakCondition->setPlaceholderText(tr("Example: %1").arg("eax == 0 && ebx == 0"));
    ui->editLogText->setPlaceholderText(tr("Example: %1").arg("0x{p:cip} {i:cip}"));
    ui->editLogCondition->setPlaceholderText(tr("Example: %1").arg("eax == 0 && ebx == 0"));
    ui->editCommandText->setPlaceholderText(tr("Example: %1").arg("eax=4;StepOut"));
    ui->editCommandCondition->setPlaceholderText(tr("Example: %1").arg("eax == 0 && ebx == 0"));
    ui->editSwitchCondition->setPlaceholderText(tr("Example: %1").arg("mod.party(dis.branchdest(cip)) == 1"));
}

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

void SimpleTraceDialog::setTraceCommand(const QString & command)
{
    mTraceCommand = command;
}

static QString escapeText(QString str)
{
    str.replace(QChar('\\'), QString("\\\\"));
    str.replace(QChar('"'), QString("\\\""));
    return str;
}

void SimpleTraceDialog::on_btnOk_clicked()
{
    if(!mLogFile.isEmpty() && ui->editLogText->text().isEmpty())
    {
        QMessageBox msgyn(QMessageBox::Warning, tr("Trace log file"),
                          tr("It appears you have set the log file, but not the log text. <b>This will result in an empty log</b>. Do you really want to continue?"), QMessageBox::Yes | QMessageBox::No, this);
        msgyn.setWindowIcon(DIcon("compile-warning.png"));
        msgyn.setParent(this, Qt::Dialog);
        msgyn.setWindowFlags(msgyn.windowFlags() & (~Qt::WindowContextHelpButtonHint));
        if(msgyn.exec() == QMessageBox::No)
            return;
    }
    auto logText = ui->editLogText->addHistoryClear();
    auto logCondition = ui->editLogCondition->addHistoryClear();
    if(!DbgCmdExecDirect(QString("TraceSetLog \"%1\", \"%2\"").arg(escapeText(logText), escapeText(logCondition)).toUtf8().constData()))
    {
        SimpleWarningBox(this, tr("Error"), tr("Failed to set log text/condition!"));
        return;
    }
    auto commandText = ui->editCommandText->addHistoryClear();
    auto commandCondition = ui->editCommandCondition->addHistoryClear();
    if(!DbgCmdExecDirect(QString("TraceSetCommand \"%1\", \"%2\"").arg(escapeText(commandText), escapeText(commandCondition)).toUtf8().constData()))
    {
        SimpleWarningBox(this, tr("Error"), tr("Failed to set command text/condition!"));
        return;
    }
    auto switchCondition = ui->editSwitchCondition->addHistoryClear();
    if(!DbgCmdExecDirect(QString("TraceSetSwitchCondition \"%1\"").arg(escapeText(switchCondition)).toUtf8().constData()))
    {
        SimpleWarningBox(this, tr("Error"), tr("Failed to set switch condition!"));
        return;
    }
    if(!DbgCmdExecDirect(QString("TraceSetLogFile \"%1\"").arg(escapeText(mLogFile)).toUtf8().constData()))
    {
        SimpleWarningBox(this, tr("Error"), tr("Failed to set log file!"));
        return;
    }
    auto breakCondition = ui->editBreakCondition->addHistoryClear();
    auto maxTraceCount = ui->spinMaxTraceCount->value();
    if(!DbgCmdExecDirect(QString("%1 \"%2\", .%3").arg(mTraceCommand, escapeText(breakCondition)).arg(maxTraceCount).toUtf8().constData()))
    {
        SimpleWarningBox(this, tr("Error"), tr("Failed to start trace!"));
        return;
    }
    accept();
}

void SimpleTraceDialog::on_btnLogFile_clicked()
{
    BrowseDialog browse(this, tr("Trace log file"), tr("Enter the path to the log file."), tr("Log Files (*.txt *.log);;All Files (*.*)"), QCoreApplication::applicationDirPath(), true);
    if(browse.exec() == QDialog::Accepted)
        mLogFile = browse.path;
    else
        mLogFile.clear();
}




void SimpleTraceDialog::on_WriteINI_clicked()
{
    QString strPath = QCoreApplication::applicationDirPath();
    QDir dir(strPath);
    bool bSuccess = dir.cdUp();
    if (bSuccess == true) {
    strPath = dir.path() + "/";
    }
    QSettings *configIniWrite = new QSettings(strPath + "config.ini", QSettings::IniFormat);
    configIniWrite->setValue("/" + ui->XiaoJieName->text() + "/editBreakCondition", ui->editBreakCondition->text());
    configIniWrite->setValue("/" + ui->XiaoJieName->text() + "/editLogText", ui->editLogText->text());
    configIniWrite->setValue("/" + ui->XiaoJieName->text() + "/editLogCondition", ui->editLogCondition->text());
    configIniWrite->setValue("/" + ui->XiaoJieName->text() + "/editCommandText", ui->editCommandText->text());
    configIniWrite->setValue("/" + ui->XiaoJieName->text() + "/editCommandCondition", ui->editCommandCondition->text());
    configIniWrite->setValue("/" + ui->XiaoJieName->text() + "/editSwitchCondition", ui->editSwitchCondition->text());
    configIniWrite->setValue("/" + ui->XiaoJieName->text() + "/NoteName", ui->XiaoJieName->text());


    delete configIniWrite;
}

void SimpleTraceDialog::on_ReadINI_clicked()
{
    QString strPath = QCoreApplication::applicationDirPath();
    QDir dir(strPath);
    bool bSuccess = dir.cdUp();
    if (bSuccess == true) {
        strPath = dir.path() + "/";
    }
    QSettings *configIniRead = new QSettings(strPath + "config.ini", QSettings::IniFormat);
    QString XiaoJieName = configIniRead->value("/" + ui->XiaoJieName->text() + "/NoteName").toString();
    ui->XiaoJieName->setText(XiaoJieName);

    QString editBreakCondition1 = configIniRead->value("/" + ui->XiaoJieName->text() + "/editBreakCondition").toString();
    ui->editBreakCondition->setText(editBreakCondition1);
    QString editLogText1 = configIniRead->value("/" + ui->XiaoJieName->text() + "/editLogText").toString();
    ui->editLogText->setText(editLogText1);
    QString editLogCondition1 = configIniRead->value("/" + ui->XiaoJieName->text() + "/editLogCondition").toString();
    ui->editLogCondition->setText(editLogCondition1);
    QString editCommandText1 = configIniRead->value("/" + ui->XiaoJieName->text() + "/editCommandText").toString();
    ui->editCommandText->setText(editCommandText1);
    QString editCommandCondition1 = configIniRead->value("/" + ui->XiaoJieName->text() + "/editCommandCondition").toString();
    ui->editCommandCondition->setText(editCommandCondition1);
    QString editSwitchCondition1 = configIniRead->value("/" + ui->XiaoJieName->text() + "/editSwitchCondition").toString();
    ui->editSwitchCondition->setText(editSwitchCondition1);
    delete configIniRead;
}

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

 楼主| 冥界3大法王 发表于 2022-8-25 22:48
本帖最后由 冥界3大法王 于 2022-8-25 22:52 编辑

测试config.ini:内容:
image.png
[1]
NoteName=大美女
editBreakCondition=1111111111111111111111111111
editLogText=222222222222222
editLogCondition=2333333333333333
editCommandText=444444444444
editCommandCondition=5555555555555
editSwitchCondition=666666666666666

[美女]
NoteName=▲▲▲▲
editBreakCondition=a
editLogText=b
editLogCondition=c
editCommandText=d
editCommandCondition=e
editSwitchCondition=f

[注册表]
editBreakCondition=aaa
editLogText=bbb
editLogCondition=ccc
editCommandText=dddd
editCommandCondition=eee
editSwitchCondition=ffff
NoteName=MeiMei

[registry]
editBreakCondition=aaa
editLogText=bbb
editLogCondition=ccc
editCommandText=ddd
editCommandCondition=eeee
editSwitchCondition=The key value is: {s: EDX}   the stack address is: {[ESP]}
NoteName=The key value is: {s: EDX}   the stack address is: {[ESP]}
Takitooru 发表于 2022-8-25 22:51
configIniRead->setIniCodec("UTF-8");

免费评分

参与人数 1吾爱币 +4 热心值 +1 收起 理由
冥界3大法王 + 4 + 1 感谢明白人~~

查看全部评分

 楼主| 冥界3大法王 发表于 2022-8-25 23:00
本帖最后由 冥界3大法王 于 2022-8-25 23:08 编辑
Takitooru 发表于 2022-8-25 22:51
configIniRead->setIniCodec("UTF-8");
image.png
@Takitooru  朋友报错啊。


明白了,得插入到两句中间
 楼主| 冥界3大法王 发表于 2022-8-25 23:08


image.png
读取出来不乱码了;但是有的读取出来还是空白,又是啥原因呢?
本人一直使用Delphi,能这个QT知之甚少。
Takitooru 发表于 2022-8-25 23:09
冥界3大法王 发表于 2022-8-25 23:00
@Takitooru  朋友报错啊。

QSettings *configIniRead = new QSettings(strPath + "config.ini", QSettings::IniFormat);
     configIniRead->setIniCodec("UTF-8");
    QString XiaoJieName = configIniRead->value("/" + ui->XiaoJieName->text() + "/NoteName").toString();
    ui->XiaoJieName->setText(XiaoJieName);
qq06314488 发表于 2022-8-25 23:10
已经放弃QT了,改学C#了
我记得以前也遇到过这个问题,就是编码问题。具体怎么解决的忘记了
Takitooru 发表于 2022-8-25 23:11
冥界3大法王 发表于 2022-8-25 23:08
读取出来不乱码了;但是有的读取出来还是空白,又是啥原因呢?
本人一直使用Delphi,能这个QT知之甚 ...

ini保存的编码最好与读取编码一致,ini是utf编码就用utf读取,如此类推
 楼主| 冥界3大法王 发表于 2022-8-25 23:27

@qq06314488 @qq06314488

本帖最后由 冥界3大法王 于 2022-8-26 08:14 编辑

@Takitooruvoid SimpleTraceDialog::on_WriteINI_clicked()
{
    QString strPath = QCoreApplication::applicationDirPath();
    QDir dir(strPath);
    bool bSuccess = dir.cdUp();
    if (bSuccess == true) {
        strPath = dir.path() + "/";
    }
    QSettings *configIniWrite = new QSettings(strPath + "config.ini", QSettings::IniFormat);
    configIniWrite->setIniCodec("UTF-8");
    configIniWrite->setValue("/" + ui->XiaoJieName->text() + "/editBreakCondition", ui->editBreakCondition->text());
    configIniWrite->setValue("/" + ui->XiaoJieName->text() + "/editLogText", ui->editLogText->text());
    configIniWrite->setValue("/" + ui->XiaoJieName->text() + "/editLogCondition", ui->editLogCondition->text());
    configIniWrite->setValue("/" + ui->XiaoJieName->text() + "/editCommandText", ui->editCommandText->text());
    configIniWrite->setValue("/" + ui->XiaoJieName->text() + "/editCommandCondition", ui->editCommandCondition->text());
    configIniWrite->setValue("/" + ui->XiaoJieName->text() + "/editSwitchCondition", ui->editSwitchCondition->text());
    configIniWrite->setValue("/" + ui->XiaoJieName->text() + "/NoteName", ui->XiaoJieName->text());




    delete configIniWrite;
}


void SimpleTraceDialog::on_ReadINI_clicked()
{
    QString strPath = QCoreApplication::applicationDirPath();
    QDir dir(strPath);
    bool bSuccess = dir.cdUp();
    if (bSuccess == true) {
        strPath = dir.path() + "/";
    }
      QSettings *configIniRead = new QSettings(strPath + "config.ini", QSettings::IniFormat);
      configIniRead->setIniCodec("UTF-8");
      QString XiaoJieName = configIniRead->value("/" + ui->XiaoJieName->text() + "/NoteName").toString();
      ui->XiaoJieName->setText(XiaoJieName);




    QString editBreakCondition1 = configIniRead->value("/" + ui->XiaoJieName->text() + "/editBreakCondition").toString();
  这写一次就行吧? configIniRead->setIniCodec("UTF-8");
    ui->editBreakCondition->setText(editBreakCondition1);


    QString editLogText1 = configIniRead->value("/" + ui->XiaoJieName->text() + "/editLogText").toString();
    configIniRead->setIniCodec("UTF-8");
    ui->editLogText->setText(editLogText1);
    QString editLogCondition1 = configIniRead->value("/" + ui->XiaoJieName->text() + "/editLogCondition").toString();
    ui->editLogCondition->setText(editLogCondition1);


    QString editCommandText1 = configIniRead->value("/" + ui->XiaoJieName->text() + "/editCommandText").toString();
    configIniRead->setIniCodec("UTF-8");
    ui->editCommandText->setText(editCommandText1);


    QString editCommandCondition1 = configIniRead->value("/" + ui->XiaoJieName->text() + "/editCommandCondition").toString();
    configIniRead->setIniCodec("UTF-8");
    ui->editCommandCondition->setText(editCommandCondition1);


    QString editSwitchCondition1 = configIniRead->value("/" + ui->XiaoJieName->text() + "/editSwitchCondition").toString();
    configIniRead->setIniCodec("UTF-8");
    ui->editSwitchCondition->setText(editSwitchCondition1);
    delete configIniRead;
}

我试着用界面去写ini
结果生成文件再读取后成了天书:
image.png
[%U6CE8%U518C%U8868]
editBreakCondition=0
editLogText=\x952e\x503c\x662f\xff1a{s: EDX}  \x5806\x6808\x5730\x5740\x662f\xff1a {[ESP]
editLogCondition=0
editCommandText=0
editCommandCondition=0
editSwitchCondition=0
NoteName=\x6ce8\x518c\x8868
 楼主| 冥界3大法王 发表于 2022-8-25 23:42
QT真是狗屎程序,也不知到底好在哪?。。。我那Delphi怎么调教怎么听话好使。
一晚上这么点玩意都没解决彻底。。。还是用Delphi外部功能实现吧,至少节约生命。

免费评分

参与人数 1吾爱币 +1 热心值 +1 收起 理由
weiekko + 1 + 1 widget很拉跨,qml也很拉跨

查看全部评分

您需要登录后才可以回帖 登录 | 注册[Register]

本版积分规则

返回列表

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

GMT+8, 2024-11-25 07:53

Powered by Discuz!

Copyright © 2001-2020, Tencent Cloud.

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