How to convert HBufC to QString

From Qt Wiki
Jump to navigation Jump to search

Template:Abstract For more information about converting between string types see Using Qt and Symbian C++ Together#Strings

Template:ArticleMetaData

Understanding HBufC[edit | edit source]

We can use HBufC when we don't know the size of data that we want to have in the descriptor. Letter 'C' in HBufC stands for constant that means the data is constant but it can also be changed in two ways . First using the assignment operator and another by using the Modifiable pointer descriptor. This is more information on HBufC in Fundamentals of Symbian C++/Descriptors


Source File[edit | edit source]

  1. include <QtGui>
  2. include <QApplication>
  3. include <qstring.h>
  4. include <QLabel>
  5. include <QVBoxLayout>
  6. include <QWidget>

int main(int argc, char *argv[]) {

   QApplication a(argc, argv);
   QWidget *win = new QWidget;
   QLabel *label = new QLabel();
   QVBoxLayout *layout = new QVBoxLayout;
   _LIT(KMsg,"Hello");
   HBufC* buf = KMsg().Alloc();
   QString qString((QChar*)buf->Des().Ptr(),buf->Length());
   label->setText(qString);
   layout->addWidget(label);
   win->setLayout(layout);
   win->show();
   delete buf;
   return a.exec();

}

zh-hans:如何转换HBufC为QString