In the post http://www.cnblogs.com/hancq/p/5817108.html, the use of an empty QT project to create an interface with a menu bar and a toolbar is described.
Here, use a simple picture viewer project to familiarize yourself with the image display and basic operations of QT.
The project is divided into two parts:
(1) To achieve the opening, closing, center display, previous/Next switch of the picture
(2) To achieve the image amplification, reduction, left-hand, right-handed, save as the operation
The QT class that needs to be used:
Qfiledialog qimage Qpixmap Qfileinfo
Create an interface with a menu bar and a toolbar using an empty QT project Reference blog http://www.cnblogs.com/hancq/p/5817108.html
1. Page layout
Add menu bars and toolbars to your actual needs, such as:
The toolbar icons are: Open picture, close picture, Previous, Next, left, right, zoom in, zoom out
This project is a small demo, mainly familiar with the image processing of related operations, basic classes and familiar with the operation can add complex functions.
2. Realize the opening and closing of pictures
Steps to open and display the file:
(1) using the Qfiledislog function to select a picture file, get the path name of the file;
(2) using the Qimage class to load files, generate image objects;
(3) Use the Setpixmap function of the Qlabel class to display the picture on the interface.
The reference code is as follows:
1 voidImageviewer::openactiontriggered (void)2 {3filename = Qfiledialog::getopenfilename ( This, TR ("Select Image:"),4 "D:\\documents\\pictures", TR ("Images (*.png *.bmp *.jpg *.gif)"));5 if(Filename.isempty ()) {6 return ;7 }8 9 qimage image;Ten if(!image.load (filename)) { OneQmessagebox::information ( This, TR ("Error"), TR ("Open File Error")); A return ; - } - theQpixmap Pixmap =qpixmap::fromimage (image); -ImageSize =pixmap.size (); - -Imagelabel->Setpixmap (pixmap); +Imagelabel->Resize (imageSize); - //qdebug () << "filname:" << filename; + ASetwindowtitle (qfileinfo (filename). filename () + TR ("-Imageviewer")); at}
When the picture is closed, simply empty the label:
1 void Imageviewer::closeactiontriggered (void)2{3 imagelabel-> Clear (); 4 Imagelabel->resize (qsize ()); 5 Setwindowtitle (tr ("imageviewer")); 6 }
The function that the picture opens, closes, as the Qaction the slot function to trigger can:
1 Connect (openaction, SIGNAL (triggered (boolThis, SLOT (openactiontriggered ())); 2 Connect (closeaction, SIGNAL (triggered (boolThis, SLOT (closeactiontriggered ()));
Tip: When the picture opens, you can change the interface name to the file name + title, and remove the file name when you close it for user convenience.
3, achieve the picture center display, the page adjusts automatically
By doing this, you can already create a label and display the picture in the label.
However, there are a variety of problems, such as: label in the resize part size beyond the size of the page display is not complete, the picture can not be centered, and so on.
The following actions need to be done to resolve the above problem:
(1) using the Setpixmap function of the Qlabel class to display the picture on the interface;
(2) Create a qscrollarea part and load the label into the Scrollarea;
(3) Set the Scrollarea part as the center alignment, no border;
(4) Set the layout of the page as the grid layout, and increase the Scrollarea part to 0, 0 points.
Reference code:
1 voidImageshow::setimageshowwidget (void)2 {3 /*Label Show Image*/4Imagelabel =NewQlabel ();5 6Qscrollarea *imagescrollarea =NewQscrollarea ();7Imagescrollarea->setalignment (qt::aligncenter);8Imagescrollarea->Setframeshape (qframe::noframe);9Imagescrollarea->Setwidget (Imagelabel);Ten OneQgridlayout *mainlayout =Newqgridlayout (); AMainlayout->addwidget (Imagescrollarea,0,0); -Centralwidget->setlayout (mainlayout); -}
When a picture is opened, it is automatically centered, and when the size of the picture exceeds the center window size, the Scrollarea part appears with a slider, showing that the fit zooms in and out, and the display works better.
The effect is as follows:
4, to achieve the previous/next switch
When you open a file, you get a list of the files to the current directory, which is saved to the File Information link table.
When you need to switch on and off the open file, simply poll the following table in the current list and get the picture displayed.
1 voidImageviewer::getimginfolist (Qfileinfolist &imginfolist)2 {3 imginfolist.clear ();4 5Qdir dir =qfileinfo (filename). Absolutepath ();6Qfileinfolist infolist =dir.entryinfolist (qdir::files);7 //qdebug () << "GET:" << infolist.count () << dir;8 9 qfileinfo info;Ten for(inti =0; I < Infolist.count (); i++) { Oneinfo =infolist.at (i); A //qdebug () << i << Info.absolutepath (); -QString suffix =Info.suffix (); - the if(Suffix = ="jpg"|| suffix = ="BMP"|| suffix = ="PNG") { - Imginfolist.append (info); - //qdebug () << "getimginfolist:" << i << info.absolutepath () << info.suffix (); - } + } - +Qfileinfo Curimageinfo =qfileinfo (filename); A for(intj =0; J < Imginfolist.count (); J + +) { atinfo =imginfolist.at (j); - if(Info.filename () = =Curimageinfo.filename ()) { -index =J; - //qdebug () << "Curimage index:" << index; - } - } in}
Add the previous/Next button on the menu bar and toolbar to implement the qaction behavior trigger.
Implementation steps:
(1) Use the qfileinfolist linked list to save the information of all the picture files under the path where the file is already open;
(2) record the following table of the current document, the previous/next time trigger, the subscript to increase or decrease;
(3) Get the file path and filename;
(4) Reconstruct the image object, load the picture file and display it.
Reference code:
1 voidImageviewer::lastactiontriggered (void)2 {3 //getimginfolist (imginfolist);4 5index = index-1;6 intCount =Imginfolist.count ();7 //qdebug () << "left Count:" << count << "index:" << index;8 if(Index <0) {9Index = count-1;Ten } One A filename.clear (); - filename.append (path); -FileName + ="/"; theFileName + =imginfolist.at (Index). FileName (); - //qdebug () << "filname:" << filename; - - qimage image; + if(!image.load (filename)) { -Qmessagebox::information ( This, TR ("Error"), TR ("Open File Error")); + return ; A } at -Qpixmap Pixmap =qpixmap::fromimage (image); -ImageSize =pixmap.size (); - -Imagelabel->Setpixmap (pixmap); -Imagelabel->Resize (imageSize); in -Setwindowtitle (qfileinfo (filename). filename () + TR ("-Imageviewer")); to } + - voidImageviewer::nextactiontriggered (void) the { * //getimginfolist (imginfolist); $ Panax Notoginsengindex = index +1; - intCount =Imginfolist.count (); the //qdebug () << "right count:" << count << "index:" << index; + if(Index = =count) { Aindex =0; the } + - filename.clear (); $ filename.append (path); $FileName + ="/"; -FileName + =imginfolist.at (Index). FileName (); - //qdebug () << "filname:" << filename; the - qimage image;Wuyi if(!image.load (filename)) { theQmessagebox::information ( This, TR ("Error"), TR ("Open File Error")); - return ; Wu } - AboutQpixmap Pixmap =qpixmap::fromimage (image); $ImageSize =pixmap.size (); - -Imagelabel->Setpixmap (pixmap); -Imagelabel->Resize (imageSize); A +Setwindowtitle (qfileinfo (filename). filename () + TR ("-Imageviewer")); the}
Places to be aware of:
(1) The picture closes, the previous one, the next action is after the picture opens successfully to be able to operate, therefore the late button causes the program to crash, needs to add enables the operation.
(2) The creation of the picture object here does not use the new operation, the reason is the need to manually delete the trouble; directly using local variables, the compiler will automatically release resources.
(3) This is created using an empty QT template, without using the system's UI files, and requires manual initialization of the menu bar, toolbars, and center windows.
This article for the original blog, reproduced please indicate the author and source, Update link: http://www.cnblogs.com/hancq/p/6016400.html
QT Project Combat 2: Simple Picture Viewer (1)