📄 showimg-main-cpp.html
字号:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN"><html><head><meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1"><title>Qt Toolkit - showimg/main.cpp example file</title><style type="text/css"><!--h3.fn,span.fn { margin-left: 1cm; text-indent: -1cm; }a:link { color: #004faf; text-decoration: none }a:visited { color: #672967; text-decoration: none }body { background: white; color: black; }--></style></head><body bgcolor="#ffffff"><table width="100%"><tr><td><a href="index.html"><img width="100" height="100" src="qtlogo.png"alt="Home" border="0"><img width="100"height="100" src="face.png" alt="Home" border="0"></a><td valign="top"><div align="right"><img src="dochead.png" width="472" height="27"><br><a href="classes.html"><b>Classes</b></a>- <a href="annotated.html">Annotated</a>- <a href="hierarchy.html">Tree</a>- <a href="functions.html">Functions</a>- <a href="index.html">Home</a>- <a href="topicals.html"><b>Structure</b> <font face="Arial,Helvetica,Geneva,Swiss,SunSans-Regular" align="center" size=32>Qte</font></a></div></table><h1 align=center>Show Image</h1><br clear="all"> This example reads and displays an image in any supported image format (GIF, BMP, PPM, XMP etc.) <hr> Header file: <pre>/****************************************************************************** $Id: qt/examples/showimg/showimg.h 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.*******************************************************************************/#ifndef SHOWIMG_H#define SHOWIMG_H#include <<a href="qwidget-h.html">qwidget.h</a>>#include <<a href="qimage-h.html">qimage.h</a>>class QLabel;class QMenuBar;class QPopupMenu;class ImageViewer : public QWidget{ Q_OBJECTpublic: ImageViewer( <a href="qwidget.html">QWidget</a> *parent=0, const char *name=0, int wFlags=0 ); ~ImageViewer(); bool loadImage( const char *fileName );protected: void paintEvent( <a href="qpaintevent.html">QPaintEvent</a> * ); void resizeEvent( <a href="qresizeevent.html">QResizeEvent</a> * ); void mousePressEvent( <a href="qmouseevent.html">QMouseEvent</a> * ); void mouseReleaseEvent( <a href="qmouseevent.html">QMouseEvent</a> * ); void mouseMoveEvent( <a href="qmouseevent.html">QMouseEvent</a> * );private: void scale(); int conversion_flags; bool smooth() const; bool unscaled() const; bool useColorContext() const; int alloc_context; bool convertEvent( <a href="qmouseevent.html">QMouseEvent</a>* e, int& x, int& y ); const char* filename; <a href="qimage.html">QImage</a> image; // the loaded image <a href="qpixmap.html">QPixmap</a> pm; // the converted pixmap <a href="qpixmap.html">QPixmap</a> pmScaled; // the scaled pixmap <a href="qmenubar.html">QMenuBar</a> *menubar; <a href="qpopupmenu.html">QPopupMenu</a> *file; <a href="qpopupmenu.html">QPopupMenu</a> *saveimage; <a href="qpopupmenu.html">QPopupMenu</a> *savepixmap; <a href="qpopupmenu.html">QPopupMenu</a> *edit; <a href="qpopupmenu.html">QPopupMenu</a> *options; <a href="qwidget.html">QWidget</a> *helpmsg; <a href="qlabel.html">QLabel</a> *status; int si, sp, ac, co, mo, fd, bd, // Menu item ids td, ta, ba, fa, au, ad, dd, ns, us, ss, cc, t1, t8, t32; void updateStatus(); void setMenuItemFlags(); bool reconvertImage(); int pickx, picky; int clickx, clicky; bool may_be_other; static ImageViewer* other; void setImage(const QImage& newimage);private slots: void to1Bit(); void to8Bit(); void to32Bit(); void toBitDepth(int); void copy(); void paste(); void hFlip(); void vFlip(); void rot180(); void editText(); void newWindow(); void openFile(); void saveImage(int); void savePixmap(int); void giveHelp(); void doOption(int); void copyFrom(ImageViewer*);};#endif // SHOWIMG_H</pre> <hr> Implementation: <pre>/****************************************************************************** $Id: qt/examples/showimg/showimg.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 "showimg.h"#include "imagetexteditor.h"#include <<a href="qmenubar-h.html">qmenubar.h</a>>#include <<a href="qfiledialog-h.html">qfiledialog.h</a>>#include <<a href="qmessagebox-h.html">qmessagebox.h</a>>#include <<a href="qpopupmenu-h.html">qpopupmenu.h</a>>#include <<a href="qlabel-h.html">qlabel.h</a>>#include <<a href="qpainter-h.html">qpainter.h</a>>#include <qkeycode.h>#include <<a href="qapplication-h.html">qapplication.h</a>>#include <<a href="qclipboard-h.html">qclipboard.h</a>>/* In the constructor, we just pass the standard parameters on to <a href="qwidget.html">QWidget</a>. The menu uses a single slot to simplify the process of adding more items to the options menu.*/ImageViewer::ImageViewer( <a href="qwidget.html">QWidget</a> *parent, const char *name, int wFlags ) : <a href="qwidget.html">QWidget</a>( parent, name, wFlags ), conversion_flags( PreferDither ), filename( 0 ), helpmsg( 0 ){ pickx = -1; picky = -1; clickx = -1; clicky = -1; alloc_context = 0; menubar = new <a href="qmenubar.html">QMenuBar</a>(this); menubar->setSeparator( QMenuBar::InWindowsStyle ); <a href="qstrlist.html">QStrList</a> fmt = QImage::outputFormats(); saveimage = new <a href="qpopupmenu.html">QPopupMenu</a>(); savepixmap = new <a href="qpopupmenu.html">QPopupMenu</a>(); for (const char* f = fmt.<a href="qlist.html#e625f9">first</a>(); f; f = fmt.<a href="qlist.html#f95ddd">next</a>()) { saveimage->insertItem( f ); savepixmap->insertItem( f ); } <a href="qobject.html#fbde73">connect</a>( saveimage, SIGNAL(activated(int)), this, SLOT(<a href=#372>saveImage</a>(int)) ); <a href="qobject.html#fbde73">connect</a>( savepixmap, SIGNAL(activated(int)), this, SLOT(<a href=#373>savePixmap</a>(int)) ); file = new <a href="qpopupmenu.html">QPopupMenu</a>(); menubar->insertItem( "&File", file ); file->insertItem( "&New window", this, SLOT(<a href=#374>newWindow</a>()), CTRL+Key_N ); file->insertItem( "&Open...", this, SLOT(<a href=#375>openFile</a>()), CTRL+Key_O ); si = file->insertItem( "Save image", saveimage ); sp = file->insertItem( "Save pixmap", savepixmap ); file->insertSeparator(); file->insertItem( "E&xit", qApp, SLOT(quit()), CTRL+Key_Q ); edit = new <a href="qpopupmenu.html">QPopupMenu</a>(); menubar->insertItem( "&Edit", edit ); edit->insertItem("&Copy", this, SLOT(<a href=#394>copy</a>()), CTRL+Key_C); edit->insertItem("&Paste", this, SLOT(<a href=#395>paste</a>()), CTRL+Key_V); edit->insertSeparator(); edit->insertItem("&Horizontal flip", this, SLOT(<a href=#391>hFlip</a>()), ALT+Key_H); edit->insertItem("&Vertical flip", this, SLOT(<a href=#392>vFlip</a>()), ALT+Key_V); edit->insertItem("&Rotate 180", this, SLOT(<a href=#393>rot180</a>()), ALT+Key_R); edit->insertSeparator(); edit->insertItem("&Text...", this, SLOT(<a href=#397>editText</a>())); edit->insertSeparator(); t1 = edit->insertItem( "Convert to &1 bit", this, SLOT(<a href=#398>to1Bit</a>()) ); t8 = edit->insertItem( "Convert to &8 bit", this, SLOT(<a href=#399>to8Bit</a>()) ); t32 = edit->insertItem( "Convert to &32 bit", this, SLOT(<a href=#400>to32Bit</a>()) ); options = new <a href="qpopupmenu.html">QPopupMenu</a>(); menubar->insertItem( "&Options", options ); ac = options->insertItem( "AutoColor" ); co = options->insertItem( "ColorOnly" ); mo = options->insertItem( "MonoOnly" ); options->insertSeparator(); fd = options->insertItem( "DiffuseDither" ); bd = options->insertItem( "OrderedDither" ); td = options->insertItem( "ThresholdDither" ); options->insertSeparator(); ta = options->insertItem( "ThresholdAlphaDither" ); ba = options->insertItem( "OrderedAlphaDither" ); fa = options->insertItem( "DiffuseAlphaDither" ); options->insertSeparator(); ad = options->insertItem( "PreferDither" ); dd = options->insertItem( "AvoidDither" ); options->insertSeparator(); ns = options->insertItem( "No scaling" ); us = options->insertItem( "Normal scaling" ); ss = options->insertItem( "Smooth scaling" ); options->insertSeparator(); cc = options->insertItem( "Use color context" ); if ( <a href="qapplication.html#fdf8ca">QApplication::colorSpec</a>() == QApplication::ManyColor ) options->setItemEnabled( cc, FALSE ); options->setCheckable( TRUE ); <a href=#370>setMenuItemFlags</a>(); menubar->insertSeparator(); <a href="qpopupmenu.html">QPopupMenu</a>* help = new <a href="qpopupmenu.html">QPopupMenu</a>(); menubar->insertItem( "&Help", help ); help-><a href="qmenudata.html#deddb9">insertItem</a>( "Help!", this, SLOT(<a href=#388>giveHelp</a>()), CTRL+Key_H ); <a href="qobject.html#fbde73">connect</a>( options, SIGNAL(activated(int)), this, SLOT(<a href=#369>doOption</a>(int)) ); status = new <a href="qlabel.html">QLabel</a>(this); status->setFrameStyle( QFrame::WinPanel | QFrame::Sunken ); status->setFixedHeight( <a href="qwidget.html#386569">fontMetrics</a>().height() + 4 ); <a href="qwidget.html#36406c">setMouseTracking</a>( TRUE );}ImageViewer::~ImageViewer(){ if ( alloc_context ) <a href="qcolor.html#cc0257">QColor::destroyAllocContext</a>( alloc_context ); if ( other == this ) other = 0;}/* This function modifies the conversion_flags when an options menu item is selected, then ensures all menu items are up to date, and reconverts the image if possibly necessary.*/void <a name="369"></a>ImageViewer::doOption(int item){ if ( item == cc ) { // Toggle bool newbool = !options->isItemChecked(item); options->setItemChecked(item, newbool); // And reconvert... <a href=#377>reconvertImage</a>(); <a href="qwidget.html#7569b1">repaint</a>(image.hasAlphaBuffer()); // show image in widget return; } if ( options->isItemChecked( item ) ) return; // They are all radio buttons if ( item == ns || item == us || item == ss ) { options->setItemChecked( ns, item == ns ); options->setItemChecked( us, item == us ); options->setItemChecked( ss, item == ss ); <a href=#377>reconvertImage</a>(); <a href="qwidget.html#7569b1">repaint</a>(image.hasAlphaBuffer() || unscaled() ); // show image in widget return; } int ocf = conversion_flags; if ( item == ac ) { conversion_flags = conversion_flags & ~ColorMode_Mask | AutoColor; } else if ( item == co ) { conversion_flags = conversion_flags & ~ColorMode_Mask | ColorOnly; } else if ( item == mo ) { conversion_flags = conversion_flags & ~ColorMode_Mask | MonoOnly;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -