Qt display image.

Source: Internet
Author: User

There are many ways to display images using QT

Roughly

Label

Directly draw

Container display

 

1 --------------- display GIF image (displayed on label)

Http://archive.cnblogs.com/a/2053701/

 

To display GIF images in QT, you cannot simply add parts.

You also need to manually write the program.

Tool: QT creator

To create a new project, add a qlabel component in the designer.

For example:


Scale the qlabel to an appropriate size.

Add the following program to the CPP function:

# Include "widget. H"
# Include "ui_widget.h"
# Include <qlabel>
# Include <qmovie>

Widget: widget (qwidget * parent ):
Qwidget (parent ),
UI (new UI: widget)
{
UI-> setupui (this );
Qmovie * movie = new qmovie ("D:/project/QT/testclass/2.gif ");
UI-> label-> setmovie (movie );
Movie-> Start ();
}

Widget ::~ Widget ()
{
Delete UI;
}


For example:



Note the path name in qmovie: "D:/project/QT/testclass/2.gif". The path slash here is opposite to that in Windows. The default path is a backslash in windows.

Compile and run the GIF file.

For example:



When the document GIF image is displayed:

# Include <qtgui/qapplication>
# Include <qlabel>
# Include <qmovie>

Int main (INT argc, char * argv [])
{
Qapplication app (argc, argv );

Qlabel * label = new qlabel ();
Qmovie * movie = new qmovie ("D:/project/QT/firstqt/2.gif ");
Label-> setmovie (movie );
Movie-> Start ();
Label-> show ();

Return app.exe C ();
}

 

2 --------------------------- the image is displayed on the label ------------------

 

Put your label.png on the top of the project directory and directly
Qpixmap pixmap ("label.png ");

UI-> title_label-> setpixmap (pixmap );

UI-> title_label-> show ();

 

---

You can directly:
Label-> setpixmap (qpixmap ("./pic.jpg "));

Or:
Qimage * image = new qimage ("./pic.jpg ");
Label-> setpixmap (qpixmap: fromimage (image ));

Or change an image in the middle:
Qimage * image = new qimage ("./pic1.jpg ");
Label-> setpixmap (qpixmap: fromimage (image ));
...........
Image-> load ("./pic2.jpg ");

 

3 ---------- draw an image directly -------------------------

Voidlogindlg: paintevent (qpaintevent *)

{
QPainterpainter(this);
QPixmappix;
pix.load("D:/QT/login/login/images/delta.png");
painter.drawPixmap(0,0,100,33,pix);
//painter.drawLine(0,0,100,100);
}

 

 

 



 

4 ----------- pictures of the program at startup

 

Qapplication app (argc, argv );
Qsplashscreen * splash = new qsplashscreen;
Splash-> setpixmap (qpixmap (":/images/splash.png"); // sets the image
Splash-> show (); // display the image
Qt: Alignment topright = QT: alignright | QT: aligntop;
Splash-> showmessage (qobject: TR ("setting up the main window..."), topright, QT: White); // display information
Mainwindow mainwin;
Splash-> showmessage (qobject: TR ("Loading modules ..."),
Topright, QT: White); // display information
Loadmodules ();
Splash-> showmessage (qobject: TR ("establishing connections ..."),
Topright, QT: White); // display information
Establishconnections ();
Mainwin. Show ();
Splash-> finish (& mainwin); // The image is always displayed until the mainwin loading is complete.
Delete Splash; // release space to save memory
Return app.exe C (); from: Example
The methods for loading images for different parts are also different, for example:
Qlabel:
C/C ++ code
Qstring strpath = ImagePath. Value (day); // image path
Qpixmap pix (strpath );
Daylabel-> setpixmap (pix );

Qpushbutton:
C/C ++ code
Button-> seticon (qicon ("toolbutton.png "));
Button-> seticonsize (qsize (48, 48 ));

The seticonsize function is used to capture the display area of an image. If this function is not available, the image is scaled and placed on the image.
Use a palette to load images:
C/C ++ code
Qpalette P = palette ();
P. setbrush (qpalette: button, qbrush (qpixmap ("toolbutton.png ")));
Setpalette (P );

In addition, the buttons are transparent:
C/C ++ code
Button-> setflat (true );

You can also use the event rendering function:
C/C ++ code
Qpixmap arrayimage ("/home/image/array.png"); // image path
Qrect arrayrect (, 50, 50); // capture the image area
Qpainter painter;
Painter. drawpixmap (qpoint (100,100), arrayimage, arrayrect); // print the image

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.