📄 qwerty-main-cpp.html
字号:
if ( code >= Codec ) t.<a href="qtextstream.html#db3d00">setCodec</a>( codecList->at(code-Codec) ); else if ( code == Uni ) t.<a href="qtextstream.html#2c5ab2">setEncoding</a>( QTextStream::Unicode ); else if ( code == MBug ) t.<a href="qtextstream.html#2c5ab2">setEncoding</a>( QTextStream::UnicodeReverse ); else if ( code == Lat1 ) t.<a href="qtextstream.html#2c5ab2">setEncoding</a>( QTextStream::Latin1 ); else if ( code == Guess ) { <a href="qfile.html">QFile</a> f(fileName); f.<a href="qfile.html#255995">open</a>(IO_ReadOnly); char buffer[256]; int l = 256; l=f.<a href="qfile.html#8c8ccc">readBlock</a>(buffer,l); <a href="qtextcodec.html">QTextCodec</a>* codec = QTextCodec::codecForContent(buffer, l); if ( codec ) { <a href="qmessagebox.html#66b7c8">QMessageBox::information</a>(this,"Encoding",QString("Codec: ")+codec-><a href="qtextcodec.html#150a9d">name</a>()); t.<a href="qtextstream.html#db3d00">setCodec</a>( codec ); } } e->setText( t.<a href="qtextstream.html#4696a5">read</a>() ); f.<a href="qfile.html#64e640">close</a>(); e->setAutoUpdate( TRUE ); e->repaint(); <a href="qwidget.html#d6a291">setCaption</a>( fileName ); changed = FALSE;}void <a name="5"></a>Editor::openAsEncoding( int code ){#ifndef QT_NO_FILEDIALOG //storing filename (proper save) is left as an exercise... <a href="qstring.html">QString</a> fn = QFileDialog::getOpenFileName( QString::null, QString::null, this ); if ( !fn.<a href="qstring.html#c62623">isEmpty</a>() ) (void) load( fn, code );#endif}bool <a name="6"></a>Editor::save(){#ifndef QT_NO_FILEDIALOG //storing filename (proper save) is left as an exercise... <a href="qstring.html">QString</a> fn = QFileDialog::getSaveFileName( QString::null, QString::null, this ); if ( !fn.<a href="qstring.html#c62623">isEmpty</a>() ) return saveAs( fn ); return FALSE;#endif}void <a name="7"></a>Editor::saveAsEncoding( int code ){#ifndef QT_NO_FILEDIALOG //storing filename (proper save) is left as an exercise... <a href="qstring.html">QString</a> fn = QFileDialog::getSaveFileName( QString::null, QString::null, this ); if ( !fn.<a href="qstring.html#c62623">isEmpty</a>() ) (void) saveAs( fn, code );#endif}void <a name="8"></a>Editor::addEncoding(){#ifndef QT_NO_FILEDIALOG <a href="qstring.html">QString</a> fn = QFileDialog::getOpenFileName( QString::null, "*.map", this ); if ( !fn.<a href="qstring.html#c62623">isEmpty</a>() ) { <a href="qfile.html">QFile</a> f(fn); if (f.<a href="qfile.html#255995">open</a>(IO_ReadOnly)) { if (<a href="qtextcodec.html#f4b1ee">QTextCodec::loadCharmap</a>(&f)) { <a href=#1>rebuildCodecList</a>(); } else { <a href="qmessagebox.html#63edba">QMessageBox::warning</a>(0,"Charmap error", "The file did not contain a valid charmap.\n\n" "A charmap file should look like this:\n" " <code_set_name> thename\n" " <escape_char> /\n" " % alias thealias\n" " CHARMAP\n" " <tokenname> /x12 <U3456>\n" " <tokenname> /xAB/x12 <U0023>\n" " ...\n" " END CHARMAP\n" ); } } }#endif}bool <a name="9"></a>Editor::saveAs( const QString& fileName, int code ){ <a href="qfile.html">QFile</a> f( fileName ); if ( no_writing || !f.<a href="qfile.html#255995">open</a>( IO_WriteOnly ) ) { <a href="qmessagebox.html#63edba">QMessageBox::warning</a>(this,"I/O Error", <a href="qstring.html">QString</a>("The file could not be opened.\n\n") +fileName); return FALSE; } <a href="qtextstream.html">QTextStream</a> t(&f); if ( code >= Codec ) t.<a href="qtextstream.html#db3d00">setCodec</a>( codecList->at(code-Codec) ); else if ( code == Uni ) t.<a href="qtextstream.html#2c5ab2">setEncoding</a>( QTextStream::Unicode ); else if ( code == MBug ) t.<a href="qtextstream.html#2c5ab2">setEncoding</a>( QTextStream::UnicodeReverse ); else if ( code == Lat1 ) t.<a href="qtextstream.html#2c5ab2">setEncoding</a>( QTextStream::Latin1 ); t << e->text(); f.<a href="qfile.html#64e640">close</a>(); <a href="qwidget.html#d6a291">setCaption</a>( fileName ); changed = FALSE; return TRUE;}void <a name="10"></a>Editor::print(){#ifndef QT_NO_PRINTER if ( printer.setup(this) ) { // opens printer dialog printer.setFullPage(TRUE); // we'll set our own margins <a href="qpainter.html">QPainter</a> p; p.<a href="qpainter.html#02ed5d">begin</a>( &printer ); // paint on printer p.<a href="qpainter.html#998df2">setFont</a>( e->font() ); <a href="qfontmetrics.html">QFontMetrics</a> fm = p.<a href="qpainter.html#73b2e5">fontMetrics</a>(); <a href="qpaintdevicemetrics.html">QPaintDeviceMetrics</a> metrics( &printer ); // need width/height // of printer surface const int MARGIN = metrics.<a href="qpaintdevicemetrics.html#a15e9e">logicalDpiX</a>() / 2; // half-inch margin int yPos = MARGIN; // y position for each line for( int i = 0 ; i < e->numLines() ; i++ ) { if ( printer.aborted() ) break; if ( yPos + fm.<a href="qfontmetrics.html#e6e380">lineSpacing</a>() > metrics.<a href="qpaintdevicemetrics.html#d4fedb">height</a>() - MARGIN ) { // no more room on this page if ( !printer.newPage() ) // start new page break; // some error yPos = MARGIN; // back to top of page } p.<a href="qpainter.html#0f088f">drawText</a>( MARGIN, yPos, metrics.<a href="qpaintdevicemetrics.html#95eb7b">width</a>() - 2*MARGIN, fm.<a href="qfontmetrics.html#e6e380">lineSpacing</a>(), ExpandTabs, e->textLine( i ) ); yPos += fm.<a href="qfontmetrics.html#e6e380">lineSpacing</a>(); } p.<a href="qpainter.html#365784">end</a>(); // send job to printer }#endif}void <a name="11"></a>Editor::resizeEvent( <a href="qresizeevent.html">QResizeEvent</a> * ){ if ( e && m ) e->setGeometry( 0, m->height(), <a href="qwidget.html#2edab1">width</a>(), <a href="qwidget.html#e3c588">height</a>() - m->height() );}void <a name="12"></a>Editor::closeEvent( <a href="qcloseevent.html">QCloseEvent</a> *event ){ event-><a href="qcloseevent.html#7cf66f">accept</a>(); if ( changed ) { // the text has been changed switch ( <a href="qmessagebox.html#63edba">QMessageBox::warning</a>( this, "Qwerty", "Save changes to Document?", <a href="qobject.html#2418a9">tr</a>("&Yes"), <a href="qobject.html#2418a9">tr</a>("&No"), <a href="qobject.html#2418a9">tr</a>("Cancel"), 0, 2) ) { case 0: // yes if ( save() ) event-><a href="qcloseevent.html#7cf66f">accept</a>(); else event-><a href="qcloseevent.html#ee1736">ignore</a>(); break; case 1: // no event-><a href="qcloseevent.html#7cf66f">accept</a>(); break; default: // cancel event-><a href="qcloseevent.html#ee1736">ignore</a>(); break; } }}void <a name="13"></a>Editor::toUpper(){ e->setText(e->text().upper());}void <a name="14"></a>Editor::toLower(){ e->setText(e->text().lower());}void <a name="15"></a>Editor::textChanged(){ changed = TRUE;}</pre> <hr> Main:<pre>/****************************************************************************** $Id: qt/examples/qwerty/main.cpp 2.3.8 edited 2004-05-12 $**** Copyright (C) 1992-2000 Trolltech AS. All rights reserved.**** This file is part of an example program for Qt. This example** program may be used, distributed and modified without limitation.*******************************************************************************/#include <<a name="qapplication.h"></a><a href="qapplication-h.html">qapplication.h</a>>#include "qwerty.h"int main( int argc, char **argv ){ <a name="QApplication"></a><a href="qapplication.html">QApplication</a> a( argc, argv ); bool small = qApp->desktop()->size().width() < 450 || qApp->desktop()->size().height() < 450; int i; for ( i= argc <= 1 ? 0 : 1; i<argc; i++ ) { Editor *e = new Editor; e-><a name="setCaption"></a><a href="qwidget.html#d6a291">setCaption</a>("Qt Example - QWERTY"); if ( i > 0 ) e->load( argv[i] ); if ( small ) { e-><a name="showMaximized"></a><a href="qwidget.html#b6e000">showMaximized</a>(); } else { e-><a name="resize"></a><a href="qwidget.html#8fcbbe">resize</a>( 400, 400 ); e-><a name="show"></a><a href="qwidget.html#200ee5">show</a>(); } } a.<a name="connect"></a><a href="qobject.html#fbde73">connect</a>( &a, SIGNAL(lastWindowClosed()), &a, SLOT(quit()) ); return a.<a name="exec"></a><a href="qapplication.html#84c7bf">exec</a>();}</pre><p><address><hr><div align="center"><table width="100%" cellspacing="0" border="0"><tr><td>Copyright
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -