문제

I was trying to convert a QString to char* type by the following methods, but they don't seem to work.

//QLineEdit *line=new QLineEdit();{just to describe what is line here}

QString temp=line->text();
char *str=(char *)malloc(10);
QByteArray ba=temp.toLatin1();
strcpy(str,ba.data());

Can you elaborate the possible flaw with this method, or give an alternative method?

도움이 되었습니까?

해결책

combobox 또는 에 대해 멤버 변수를 만들 수 있습니다.

CComboBox* pBoxOne;
pBoxOne = (CComboBox*) GetDlgItem(IDC_COMBO1);

  CString str, str2;
  int n;
  for (int i=0;i < pBoxOne->GetCount();i++)
  {
    n = pBoxOne->GetLBTextLen( i );
    pBoxOne->GetLBText( i, str.GetBuffer(n) );
    str.ReleaseBuffer();

    str2.Format(_T("item %d: %s\r\n"), i, str.GetBuffer(0));
    afxDump << str2;
   }
.

옵션 문자열은 자원 파일 자체에 저장됩니다.나는 옵션을 1로 추가했습니다. 2, 3 및 리소스 파일 항목은 입니다.

IDD_MFC_DIALOG_DIALOG DLGINIT
BEGIN
    IDC_COMBO1, 0x403, 2, 0
0x0031, 
    IDC_COMBO1, 0x403, 2, 0
0x0032, 
    IDC_COMBO1, 0x403, 2, 0
0x0033 
END
.

다른 팁

Maybe

my_qstring.toStdString().c_str();

or safer, as Federico points out:

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

It's far from optimal, but will do the work.

:) 내 문제가 해결됩니다.

다음을 수행했습니다 :

  1. control_yourtemplatename.html에 사용자 정의 JS 파일 등록 파일 (control_banner).

    $includeScript(this.url, "~sitecollection/_catalogs/masterpage/Display Templates/HWScripts/Banner.js");

  2. 바디 태그 후 첫 번째 Div 아래 에이 세 라인을 추가하십시오. yourtempatename.html (banner.html) 파일.

     <!--#_  
     ctx.OnPostRender = [];
     ctx.OnPostRender.push(function(){ 
        CustomMethodWhichIsIncludedInTheCustomJSFile();
     });
     _#-->  
    

    그러나 더 나은 솔루션은 아래에 제공됩니다 :

    PeranaCodiceTag 코드를 직접 정의하는 것을 건너 뛸 수 있습니다.CBS 디스플레이 템플릿에는이 기능이 있습니다 :

    <!--#_
       AddPostRenderCallback(ctx, function(){
           alert(ctx.Title + "finished rendering!");
       });
    _#-->
    
    .

    OnPostRender 도 있습니다.

David's answer works fine if you're only using it for outputting to a file or displaying on the screen, but if a function or library requires a char* for parsing, then this method works best:

// copy QString to char*
QString filename = "C:\dev\file.xml";
char* cstr;
string fname = filename.toStdString();
cstr = new char [fname.size()+1];
strcpy( cstr, fname.c_str() );

// function that requires a char* parameter
parseXML(cstr);

EDITED

this way also works

QString str ("Something");

char* ch = str.toStdString().C_str();

Your string may contain non Latin1 characters, which leads to undefined data. It depends of what you mean by "it deosn't seem to work".

the Correct Solution Would be like this

   QString k;
   k = "CRAZYYYQT";
   char ab[16];
   sprintf(ab,"%s",(const char *)((QByteArray)(k.toLatin1()).data()) );
   sprintf(ab,"%s",(const char *)((QByteArray)(k.toStdString()).data()));  
   sprintf(ab,"%s",(const char *)k.toStdString().c_str()  );
   qDebug()<<"--->"<<ab<<"<---";

If your string contains non-ASCII characters - it's better to do it this way: s.toUtf8().data() (or s->toUtf8().data())

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top