Qt 5 main form composition

Qt 5 main form composition

Insert image description here

1. Menu Bar
A menu is a list of commands. In order to achieve consistency in commands such as menus, toolbar buttons, and keyboard shortcuts, Qt uses actions to represent these commands. Qt's menu is a list composed of a series of QAction action objects, and the menu bar is a panel containing the menu. It is located at the top of the main window and below the title bar of the main window. A main window can have at most one menu bar.
2. Status bar
The status bar usually displays some status information of the GUI application and is located at the bottom of the main window. Users can add and use Qt widgets on the status bar. A main window can have at most one status bar.

3. Toolbar
A toolbar is a panel arranged by a series of button-like actions. It usually consists of some frequently used commands (actions). The toolbar is located below the menu bar and above the status bar, and can be docked in the top, bottom, left, and right directions of the main window. A main window can contain multiple toolbars.
4. Anchor widget
Anchor widget is used as a container to contain other widgets to implement certain functions. For example, Qt Designer's property editor, object monitor, etc. are all implemented by anchor components containing other Qt widgets. It is located inside the toolbar area and can float freely on the main window as a window, or can be docked in the upper, lower, left, and right directions of the main window like a toolbar. A main window can contain multiple anchor widgets.
5. Center widget
The center widget is located inside the anchor widget area and in the center of the main window. A main window has only one central widget.

menumenu

QToolBar

Pay attention to the role of the action button here.

QPrintDialog and QPrinter

 QPrinter printer;				//新建一个QPrinter对象
        QPrintDialog printDialog(&printer,this);			//(a)
        if(printDialog.exec())					//(b)
        {
    
    
           //获得QTextEdit对象的文档
            QTextDocument *doc =showWidget->text->document();
            doc->print(&printer);					//打印
        }

Avatar enlargement and rotation QMatrix

if(img.isNull())			//有效性判断
            return;
        QMatrix martix;			//声明一个QMatrix类的实例
        martix.scale(2,2);			//(a)
        img = img.transformed(martix);
        //重新设置显示图形
        showWidget->imageLabel->setPixmap(QPixmap::fromImage(img));

generic-cardcontainer-ui

mirror

void ImgProcessor::ShowMirrorVertical()
{
    
    
    if(img.isNull())
        return;
    img=img.mirrored(false,true);
    showWidget->imageLabel->setPixmap(QPixmap::fromImage(img));
}
void ImgProcessor::ShowMirrorHorizontal()
{
    
    
    if(img.isNull())
        return;
    img=img.mirrored(true,false);
    showWidget->imageLabel->setPixmap(QPixmap::fromImage(img));
}

Text Editor

Insert image description here

Font settings

void MainWindow::showFontComboBox(const QString &fontName)
{
    
    
    QTextCharFormat fmt;		 //创建一个QTextCharFormat对象
        fmt.setFontFamily(fontName); //选择的字体名称设置给QTextCharFormat对象
        mergeFormat(fmt);
}

void MainWindow::mergeFormat(const QTextCharFormat format)
{
    
    
    QTextCursor cursor =showWidget->text->textCursor();
    //获得编辑框中的光标
        if(!cursor.hasSelection())							//(a)
            cursor.select(QTextCursor::WordUnderCursor);
        cursor.mergeCharFormat(format);						//(b)
        showWidget->text->mergeCurrentCharFormat(format);	//(c)

}

Font size setting

void MainWindow::updateFontSize(const QString &fontSize)
{
    
    
    QTextCharFormat fmt;
    fmt.setFontPointSize(fontSize.toFloat());
    showWidget->text->mergeCurrentCharFormat(fmt);

}

Bold, shoes, underline

QTextCharFormat fmt;
    fmt.setFontWeight(boldBtn->isChecked()?QFont::Bold:QFont:: Normal);
fmt.setFontItalic(italicBtn->isChecked());
fmt.setFontUnderline(underlineBtn->isChecked());
    showWidget->text->mergeCurrentCharFormat(fmt);

Selection of color

void MainWindow::showColorBtn()
{
    
    
    QColor color = QColorDialog::getColor();
    if(color.isValid())
    {
    
    
        QTextCharFormat fmt;
        fmt.setForeground(color);
        showWidget->text->mergeCurrentCharFormat(fmt);
    }

}

Qt 5 typesetting functions

text alignment

void MainWindow::showAlignment(QAction *act)
{
    
    
    if(act==leftAction)
            showWidget->text->setAlignment(Qt::AlignLeft);
        if(act==rightAction)
            showWidget->text->setAlignment(Qt::AlignRight);
        if(act==centerAction)
            showWidget->text->setAlignment(Qt::AlignCenter);
        if(act==justifyAction)
            showWidget->text->setAlignment(Qt::AlignJustify);

}

sort

Insert image description here

Guess you like

Origin blog.csdn.net/weixin_43328357/article/details/112342484