qerths.blogg.se

Imageviewer
Imageviewer






imageviewer

Imageviewer code#

However, when we run the code and look at the menu, the "Exit" is not in the top menu. instead of directly double-clicking on the top menu area:ĮxitAct = new QAction(tr("E&xit "), this) "), this) įor the Exit menu, we open up the "Edit action" pop-up either by clicking the icon(New) or by right-click on the item -> select New. Later, after the image is loaded it will be reset as enabled:Īgain, the equivalent code should look like this: The translation, tr(), is set enabled by default.įor the Print menu, we need to set it disabled (unchecked) initially. The equivalent code in C++ would be like this: Note that we can get the "Edit action" window either by double click the item in the Action Editor window or right mouse click on the item, and then select "Edit." Let's change the object name to openAct, and make a shortcut (we should type in real key combination, not typing in each character) as shown in the picture below: Notice that at the bottom, in the Action Editor, a new action called actionOpen has been created. We need to press Enter to make a real change in the menu. Type in "File" at the top menu, and "Open." under the "File" menu. Let's open up Designer by double-clicking the imageviewer.ui to put menu bar and actions. In addition we may have the Help menu, and About to provide the users with information about the Image Viewer example Fit to Window - Stretch the image to occupy the entire window.

imageviewer

Normal Size - Show the image at its original size.Once an image is loaded, the View menu allows the users to: The File menu gives the user the possibility to: With the Image Viewer application, the users can view an image of their choice. The setCentralWidget(ui->scrollArea) make the scrollArea as a central widget of MainWindow.

imageviewer

If we omitted to set the imageLabel's scaledContents property, zooming in would enlarge the QLabel, but leave the pixmap at its original size, exposing the QLabel's background. Here, we ensure that the label will scale its contents to fill all available space, to enable the image to scale properly when zooming. When enabled and the label shows a pixmap, it will scale the pixmap to fill the available space. The setScaledContents(bool) decides whether the label will scale its contents to fill all available space. Otherwise, the default size policy (preferred) will make scroll bars appear when the scroll area becomes smaller than the label's minimum size hint. We set imageLabel's size policy to ignored, making the users able to scale the image to whatever size they want when the Fit to Window option is turned on. The size policy describes how the item should grow horizontally and vertically when arranged in a layout. The setSizePolicy() sets the size policy to policy. The setBackgroundRole() method let us use a color role for the background, which means one of the predefined color of the style applied to the widget. ScrollArea->setBackgroundRole(QPalette::Dark) ImageLabel->setSizePolicy(QSizePolicy::Ignored, QSizePolicy::Ignored) ImageLabel->setBackgroundRole(QPalette::Base) ImageViewer::ImageViewer(QWidget *parent) : This time, we set up the QScrollArea and the QLable using code but not Designer. More work on the Scroll area and the Label If the child widget exceeds the size of the frame, QScrollArea automatically provides scroll bars. QScrollArea provides a scrolling view around another widget. QLabel is typically used for displaying text, but it can also display an image. We want to put Scroll Area into the Main Window: Let's open up Designer by double-clicking the imageviewer.ui. Also, in the header, we declared a pointer to the Ui::ImageViewer as a private member, ui. Note that we have a namespace UI, and our ImageViewer is under the UI scope. GreaterThan(QT_MAJOR_VERSION, 4): QT += widgetsįrom the highlight line, we have an incident of the ImageViewer class.Įxplicit ImageViewer(QWidget *parent = 0)

imageviewer

Let's open up imageviewer.pro, add a line to support print: Applications->Qt Gui Application->Choose.








Imageviewer