冥界3大法王 发表于 2022-8-25 22:39

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

本帖最后由 冥界3大法王 于 2022-8-25 22:49 编辑

QT程序又烦病了,读取INI时,字符串是数字或中文或特殊字符只要不是英文就读取不出来
#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:内容:


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


editBreakCondition=aaa
editLogText=bbb
editLogCondition=ccc
editCommandText=ddd
editCommandCondition=eeee
editSwitchCondition=The key value is: {s: EDX}   the stack address is: {}
NoteName=The key value is: {s: EDX}   the stack address is: {}

Takitooru 发表于 2022-8-25 22:51

configIniRead->setIniCodec("UTF-8");

冥界3大法王 发表于 2022-8-25 23:00

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

Takitooru 发表于 2022-8-25 22:51
configIniRead->setIniCodec("UTF-8");
@Takitooru朋友报错啊。


明白了,得插入到两句中间{:301_1008:}

冥界3大法王 发表于 2022-8-25 23:08




读取出来不乱码了;但是有的读取出来还是空白,又是啥原因呢?
本人一直使用Delphi,能这个QT知之甚少。{:301_974:}

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#了{:1_918:}
我记得以前也遇到过这个问题,就是编码问题。具体怎么解决的忘记了{:1_925:}

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
结果生成文件再读取后成了天书:

[%U6CE8%U518C%U8868]
editBreakCondition=0
editLogText=\x952e\x503c\x662f\xff1a{s: EDX}\x5806\x6808\x5730\x5740\x662f\xff1a {
editLogCondition=0
editCommandText=0
editCommandCondition=0
editSwitchCondition=0
NoteName=\x6ce8\x518c\x8868

冥界3大法王 发表于 2022-8-25 23:42

QT真是狗屎程序,也不知到底好在哪?。。。我那Delphi怎么调教怎么听话好使。
一晚上这么点玩意都没解决彻底。。。还是用Delphi外部功能实现吧,至少节约生命。
页: [1] 2 3
查看完整版本: QT程序又烦病了,读取INI时,字符串是数字或中文或特殊字符只要不是英文就读取不出来