LoginSignup
1
1

More than 3 years have passed since last update.

Qt C++ コードいろいろメモ

Last updated at Posted at 2019-06-01

Qt C++ コードいろいろメモ

時々しか使わないので書き方思い出すためのメモ

■ charからQStringへの変換

QTextCodec* tc = QTextCodec::codecForLocale();

char *chr = "日本語";
QString qStr = QString(tc->toUnicode(chr));

■ QStringからcharへの変換

方法1
QString string = "moji";
char* pChar = string.toUtf8().data();

方法2
QString str ("Something");
char* ch = str.toStdString().C_str();

方法3
std::string str = my_qstring.toStdString();
const char* p = str.c_str();

方法4
QString str1 = "Test";
QByteArray ba = str1.toLocal8Bit();
const char *c_str2 = ba.data();

■ Qtでビルドして作成した.exeのあるパスの取得

QString exeFileDir= QApplication::applicationDirPath();

■ 外部Exeファイルの呼び出し

//exeファイル起動
QProcess *process_tmp = new QProcess(this);
QString exeFileDir= QApplication::applicationDirPath();
QString exefile = exeFileDir + "/---.exe";
process_tmp->start(exefile);

■ 2次元配列

//配列の作成
count = 10;
int **tmpAry = new int*[count];
for(int j = 0; j < count; j++){
    tmpAry[j] = new int[count]();
}

//配列の利用
for(int k = 0; k < count; k++){
    for(int j = 0; j < count; j++){
            tmpRound[j][k] = 1;
        }
    }
}

//配列開放
for(int j = 0; j < count; j++){
    delete[] tmpAry[j];
}
delete[] tmpAry;

■ 3次元配列

//配列の作成
int ***tmpAry = new int**[cntX];
for(int l = 0; l < cntX; l++){
    tmpAry[l] = new int*[cntY];
    for(int m = 0; m < cntY; m++){
        tmpAry[l][m] = new int[cntZ]();
    }
}

//配列の利用
for(int n = 0; n < cntZ; n++){
    for(int m = 0; m < cntY; m++){
        for(int l = 0; l < cntX; l++){
            if(tmptmpAry[int(double(l) * ratioMax / ratioX)][int(double(m) * ratioMax / ratioY)][int(double(n) * ratioMax / ratioZ)] == 1){
                tmpAry[l][m][n] = 1;
            }
        }
    }
}

//配列開放
for(int l = 0; l < cntX; l++){
    for(int m = 0; m < cntY; m++){
        delete[] tmpAry[l][m];
    }
    delete[] tmpAry[l];
}
delete[] tmpAry;
1
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
1