??xml version="1.0" encoding="utf-8" standalone="yes"?>
其实我们要做的不多,只要在pro文g中增加以下的选项卛_Q这样就可以release模式~译了?br />
]]>
因此Q有时候程序没有问题了Q但是QT的字体设定错误也会造成中文q现象Q我们可以不通过qtconfigE序改变默认qtE序的字体,q可以在E序中强制设定程序本w所用的默认字体Q这样也是可以的Q这里只介绍q样的思\Q代码就不脓了,希望大家见的时候有个参考?/span>
]]>
qwt的下载以及动态链接库的编译等q里׃做介l了。在源码目录下可以找到designer目录Q其中有插g的源码,~译完之后就会生成qwtplugin.dll和qwtplugin.libQ把他们拯?font face=Arial>$(QTDIR)\plugins\designer目录下,重新启动QT DesignerQ就会看到ToolBox面板中多ZQwtc,如下图所C。以上在QT3.3.4下测试通过Q?a style="COLOR: red" href="http://www.shnenglu.com/Files/cc/qwtplugin.rar">qwtplugin.dll和qwtplugin.lib下蝲
]]>
问题出现了,那么我们需要怎么解决呢,思\很简单,是转换~码Q然后再q行长度的求取,QT的文档里面有好多函数Q试验了半天Q终于找C个合适的函数Q顺利的完成了我的要求。该函数是fromLocal8Bit ( const char * local8Bit, int len = -1 )
QCString QString::local8Bit () const
以本地指定的格式q回字符丌Ӏ在X11pȝ中,使用QTextCodec::codecForLocale()?在Windowspȝ中,它是一U系l指定的~码方式。在Mac OS X中,使用uft8作ؓ~码方式?br>
有了上面的铺垫,下面问题就好办了,要想求取带中文的字符串的长度Q只需要稍微多C步而已。假设有字符串str="QT你好",那么我们主要q行str.local8Bit().length(),q样l果是6Q符合我们的要求了,事情是q样Q此文完Q?/span>
void MainWindow::closeEvent(QCloseEvent *event)
{
if (maybeSave())
{
writeSettings();
event->accept();
}
else
{
event->ignore();
}
}
QString fileName = QFileDialog::getOpenFileName(this);
if (!fileName.isEmpty())
{
loadFile(fileName);
}
QString fileName = QFileDialog::getSaveFileName(this);
if (fileName.isEmpty())
{
return false;
}
newAct = new QAction(QIcon(":/images/new.png"), tr("&New"), this);
newAct->setShortcut(tr("Ctrl+N"));
newAct->setStatusTip(tr("Create a new file"));
connect(newAct, SIGNAL(triggered()), this, SLOT(newFile()));
openAct = new QAction(QIcon(":/images/open.png"), tr("&Open..."), this);
openAct->setShortcut(tr("Ctrl+O"));
openAct->setStatusTip(tr("Open an existing file"));
connect(openAct, SIGNAL(triggered()), this, SLOT(open()));
saveAct = new QAction(QIcon(":/images/save.png"), tr("&Save"), this);
saveAct->setShortcut(tr("Ctrl+S"));
saveAct->setStatusTip(tr("Save the document to disk"));
connect(saveAct, SIGNAL(triggered()), this, SLOT(save()));
saveAsAct = new QAction(tr("Save &As..."), this);
saveAsAct->setStatusTip(tr("Save the document under a new name"));
connect(saveAsAct, SIGNAL(triggered()), this, SLOT(saveAs()));
exitAct = new QAction(tr("E&xit"), this);
exitAct->setShortcut(tr("Ctrl+Q"));
exitAct->setStatusTip(tr("Exit the application"));
connect(exitAct, SIGNAL(triggered()), this, SLOT(close()));
cutAct = new QAction(QIcon(":/images/cut.png"), tr("Cu&t"), this);
cutAct->setShortcut(tr("Ctrl+X"));
cutAct->setStatusTip(tr("Cut the current selection's contents to the "
"clipboard"));
connect(cutAct, SIGNAL(triggered()), textEdit, SLOT(cut()));
copyAct = new QAction(QIcon(":/images/copy.png"), tr("&Copy"), this);
copyAct->setShortcut(tr("Ctrl+C"));
copyAct->setStatusTip(tr("Copy the current selection's contents to the "
"clipboard"));
connect(copyAct, SIGNAL(triggered()), textEdit, SLOT(copy()));
pasteAct = new QAction(QIcon(":/images/paste.png"), tr("&Paste"), this);
pasteAct->setShortcut(tr("Ctrl+V"));
pasteAct->setStatusTip(tr("Paste the clipboard's contents into the current "
"selection"));
connect(pasteAct, SIGNAL(triggered()), textEdit, SLOT(paste()));
aboutAct = new QAction(tr("&About"), this);
aboutAct->setStatusTip(tr("Show the application's About box"));
connect(aboutAct, SIGNAL(triggered()), this, SLOT(about()));
aboutQtAct = new QAction(tr("About &Qt"), this);
aboutQtAct->setStatusTip(tr("Show the Qt library's About box"));
connect(aboutQtAct, SIGNAL(triggered()), qApp, SLOT(aboutQt()));
fileMenu = menuBar()->addMenu(tr("&File"));
fileMenu->addAction(newAct);
fileMenu->addAction(openAct);
fileMenu->addAction(saveAct);
fileMenu->addAction(saveAsAct);
fileMenu->addSeparator();
fileMenu->addAction(exitAct);
editMenu = menuBar()->addMenu(tr("&Edit"));
editMenu->addAction(cutAct);
editMenu->addAction(copyAct);
editMenu->addAction(pasteAct);
menuBar()->addSeparator();
helpMenu = menuBar()->addMenu(tr("&Help"));
helpMenu->addAction(aboutAct);
helpMenu->addAction(aboutQtAct);
fileToolBar = addToolBar(tr("File"));
fileToolBar->addAction(newAct);
fileToolBar->addAction(openAct);
fileToolBar->addAction(saveAct);
editToolBar = addToolBar(tr("Edit"));
editToolBar->addAction(cutAct);
editToolBar->addAction(copyAct);
editToolBar->addAction(pasteAct);
QSettings settings("Trolltech", "Application Example");
QPoint pos = settings.value("pos", QPoint(200, 200)).toPoint();
QSize size = settings.value("size", QSize(400, 400)).toSize();
QSettings settings("Trolltech", "Application Example");
settings.setValue("pos", pos());
settings.setValue("size", size());
int ret = QMessageBox::warning(this, tr("Application"),
tr("The document has been modified.\n"
"Do you want to save your changes?"),
QMessageBox::Yes | QMessageBox::Default,
QMessageBox::No,
QMessageBox::Cancel | QMessageBox::Escape);
if (ret == QMessageBox::Yes)
return save();
else if (ret == QMessageBox::Cancel)
return false;
cp src/translations/qt_untranslated.ts ./qt_zh_CN.ts
win32:CONFIG += console
./configure -static Q/一定要加static选项
gmake
gmake install
QTextCodec::setCodecForLocale(QTextCodec::codecForName("UTF-8"));
QTextCodec::setCodecForCStrings(QTextCodec::codecForName("UTF-8"));
QTextCodec::setCodecForTr(QTextCodec::codecForName("UTF-8"));
QTextCodec::setCodecForLocale(QTextCodec::codecForName("GBK"));
QTextCodec::setCodecForCStrings(QTextCodec::codecForName("GBK"));
QTextCodec::setCodecForTr(QTextCodec::codecForName("GBK"));
QMessageBox::information(NULL, "信息", "关于本Y件的演示信息", QMessageBox::Ok, QMessageBox::NoButtons);
QApplication::addLibraryPath(strPluginsPath");
TEMPLATE=lib
LIBS += -Lyourdlllibpath -lyourdlllibname
class MyThread : public QThread
{
public:
void run();
};
void MyThread::run()
{
QProcess::execute("notepad.exe");
}
class ...............
{...........
MyThread thread;
............
};
.....................
thread.start();
void myMessageOutput( QtMsgType type, const char *msg )
{
switch ( type ) {
case QtDebugMsg:
//写入文gQ?br> break;
case QtWarningMsg:
break;
case QtFatalMsg:
abort();
}
}
int main( int argc, char** argv )
{
QApplication app( argc, argv );
qInstallMsgHandler( myMessageOutput );
......
return app.exec();
}
<!DOCTYPE RCC><RCC version="1.0">
<qresource>
<file>images/copy.png</file>
<file>images/cut.png</file>
<file>images/new.png</file>
<file>images/open.png</file>
<file>images/paste.png</file>
<file>images/save.png</file>
</qresource>
</RCC>
RESOURCES = res.qrc
...
:images/copy.png
...
QString
QStringcL供了一个Unicode文本和经典的C以零l尾的字W数l的抽象?
QString使用隐含׃nQ这使它非常有效率ƈ且很Ҏ使用?
所有的QString的方法都使用const char *参数Qconst char *被解释ؓl典的C风格的以零结ASCII字符丌Ӏ所以const char *参数?是合法的。如果const char *不是以零l尾的,l果是不定的。把l典的C字符串复制到QString的函数将不会复制l尾?字符。QString的QChar数组Q可以通过unicode()q回Q通常不以零结。如果你需要把QString传递到一个需要C的以零结字符Ԍ请用latin1()?
没有分配M东西的QString是零Q也是长度和数据指针都?。引用空字符Ԍ“”Q一个单一?\0'字符Q的QString是空。零和空q两个QString在方法中都是合法的。把(const char *) 0赋值给QStringl定了一个零QString。ؓ了方便,QString::null是一个零QString。当排序的时候,I字W串在最前面Q然后是非空字符Ԍ然后才是零字W串。我们徏议用if ( !str.isNull() )Q而不是if ( !str )来检非零字W串Q关于解释说明也可以参考operator!()?
注意如果你发C正在混合使用QCString、QString和QByteArrayQ这会D很多不必要的复制q且也许会预C着你正在处理的真实自然数据是不定的。如果数据是以零l尾的八位数据,请用QCStringQ如果它是没有结Q也是包含0Q八位数据,请用QByteArrayQ如果它是文本,请用QString?
字符串列表可以用QStringListcL处理。你可以使用QStringList::split()来把一个字W串分割Z个字W串列表Qƈ且可以用QStringList::join()把一个字W串列表q接成一个用随意间隔符的字W串。你也可以用QStringList::grep()从一个字W串列表中获得包含特定子字符串或者包含匹配特定的regex的字W串列表?
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
QFile
QFilecL一个操作文件的输入/输出讑֤?
QFile是用来读写二q制文g和文本文件的输入/输出讑֤。QFile可以自己单独被用,但是如果和QDataStream或QTextStream一起用将更加方便?
文g名通常可以通过构造函数来传递,但也可以使用setName()来设|。你可以通过exists()来检查一个文件是否存在ƈ且可以通过remove()来移M个文件?
文g可以用open()来打开、用close()来关闭、用flush()来刷新。数据通常可以使用QDataStream或者QTextStreamq行dQ但你也可以使用readBlock()和readLine()来读Q用writeBlock()来写。QFile也支持getch()?ungetch()和putch()?
size()可以q回文g的大。你可以通过使用at()函数得到当前文g位置或者移C个新的文件位|。如果你C文g的末,atEnd()q回真。handle()q回文g句柄?
q里是一个用QTextStream来一行一行地d一个文本文件的代码Dc它会把每一行带上一个行h印出来?
QStringList lines;
QFile file( "file.txt" );
if ( file.open( IO_ReadOnly ) ) {
QTextStream stream( &file );
QString line;
int n = 1;
while ( !stream.eof() ) {
line = stream.readLine(); // 不包?#8220;\n”的一行文?br> printf( "%3d: %s\n", n++, line.latin1() );
lines += line;
}
file.close();
}
写文本也很容易(假设我们有一个行的字W串列表要写Q:
QFile file( "file.txt" );
if ( file.open( IO_WriteOnly ) ) {
QTextStream stream( &file );
for ( QStringList::Iterator it = lines.begin(); it != lines.end(); ++it )
stream << *it << "\n";
file.close();
}
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
QTextStream
QTextStreamcL供了使用QIODeviced文本的基本功能?
文本类的功能界面和标准的C++的iostreamc非常相伹{iostream和QTextStream的不同点是我们的操作在一个很Ҏ被承的QIODevice上,而iostream只能操作一个不能被l承的FILE *指针?
Qt提供了几个和iostream怼的全局函数Q?
bin讄QTextStream来读/写二q制数字
oct讄QTextStream来读/写八q制数字
dec讄QTextStream来读/写十q制数字
hex讄QTextStream来读/写十六进制数?
endl强制换行
flush强制QIODevicehM被缓存的数据
ws作ؓM可用的控制符Q在输入的时候)
reset重新讄QTextStream为它的缺省模式(误reset()Q?
qSetW(int)讄字段宽度作ؓ指定参数
qSetFill(int)讄填充字符作ؓ指定参数
qSetPrecision(int)讄_度作为指定参?
警告Q默认情况下QQTextStream在读取流的时候,会自动地流中的数字是十q制、八q制、十六进制或者二q制格式。具体情冉|Q一个以“0”为开头的数字是八q制的,比如序?#8220;0100”会被解释ؓ64?
QTextStreamc读写文本,它不适合处理二进制数据(而QDataStream是适合的)?
默认情况下,输出的是使用本地8位编码后的Unicode文本Q比如,QStringQ。这些可以用setEncoding()Ҏq行改变。对于输入,QTextStream会自动检标准Unicode“字节序标记?#8221;文本文gQ否则会使用本地8位编码?
QIODevice在构造函C被设|,或者之后在setDevice()中用。如果输入到达了atEnd()Q返回真。数据可以用operator>>()重蝲操作W读到适当cd的变量中Q或者用read()把它作ؓ整个部分d一个单一的字W串中,或者用readLine()把一ơ读一行。用skipWhiteSpace()可以忽略控制W。你可以使用flags()或setf()来设|流的标记。这个流也支持width()、precision()?fill()Q用reset()可以重新恢复默认讄?
也可以参考QDataStream、输?输出和网l和文本相关c?
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
QTextCodec
QTextCodec cL供文本编码之间的转换?br>QT使用Unicode来存储,l制以及操作字符丌Ӏ在很多情况下,你可能想要用不同的~码方式来处理数据。例如大部分的日语文仉被存储在Shift-JIS或?ISO2022的文件中Q而俄|斯的用户常怋用KOI8-R或者CP1251~码方式。QT提供了一个QTextCodec c集合来从Unicode格式转化到相应的格式?/p>
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
代码部分
#include <qstring.h>
#include <qfile.h>
#include <qtextstream.h>
#include <qtextcodec.h>
int main()
{
QFile file("test.txt");
if (file.open(IO_ReadOnly|IO_Raw))
{
QTextStream floStream(&file);
QString line;
QTextCodec *codec=QTextCodec::codecForName("GBK");
floStream.setCodec(codec);
while ( floStream.atEnd()==0 )
{
line = codec->fromUnicode(floStream.readLine());
qWarning(line);
}
file.close();
}
return 0;
}
代码中的主要改动是黄底的部?br>意思就是创立一个中文GBK~码样式Q然后按照这U方式来把读入的文g进行重新编码,q样中文可以顺利输ZQ不信你可以试一试,哈哈Q先介绍q么多,下次有什么下ơ再写啦?br>