当前位置:网站首页>Basic layout -qhboxlayout class, qvboxlayout class, qgridlayout class

Basic layout -qhboxlayout class, qvboxlayout class, qgridlayout class

2022-06-25 01:58:00 Full stack programmer webmaster

Hello everyone , I meet you again , I'm your friend, Quan Jun .

(1) newly build Qt Widget Application, Project name UserInfo, Base class QDialog, Cancel creating interface ; (2) open dialog.h The header file , Declare the controls in the dialog box in the header file , Add code

#ifndef DIALOG_H
#define DIALOG_H

#include <QDialog>
// Add header file 
#include <QLabel>
#include <QLineEdit>
#include <QComboBox>
#include <QTextEdit>
#include <QGridLayout>
class Dialog : public QDialog
{
    Q_OBJECT

public:
    Dialog(QWidget *parent = 0);
    ~Dialog();
private:// Add the following code 
    // left 
    QLabel *UserNameLabel;
    QLabel *NameLabel;
    QLabel *SexLabel;
    QLabel *DepartmentLabel;
    QLabel *AgeLabel;    
    QLabel *OtherLabel;
    QLineEdit *NameLineEdit;
    QLineEdit *UserNameLineEdit;
    QComboBox *SexComboBox;
    QTextEdit *DepartmentTextEdit;
    QLineEdit *AgeLineEdit;
    QGridLayout *LeftLayout;
    // On the right side 
    QLabel *HeadLabel;// Upper right corner 
    QLabel *HeadIconLabel;
    QPushButton *UpdateHeadBtn;
    QHBoxLayout *TopRightLayout;
    QLabel *IntroductionLabel;
    QTextEdit *IntroductionTextEdit;
    QVBoxLayout *RightLayout;
    // Bottom 
    QPushButton *OkBtn;
    QPushButton *CancelBtn;
    QHBoxLayout *ButtomLayout;
};

#endif // DIALOG_H

(2) open dialog.cpp file , In the class Dialog Add the following code to the constructor of :

#include "dialog.h"
// Add header file 
#include <QLabel>
#include <QLineEdit>
#include <QComboBox>
#include <QPushButton>
#include <QFrame>
#include <QGridLayout>
#include <QPixmap>
#include <QHBoxLayout>
Dialog::Dialog(QWidget *parent)
    : QDialog(parent)
{
    setWindowTitle(tr("UserInfo"));
    /*********** left *********/
    UserNameLabel=new QLabel(tr(" user name : "));
    UserNameLineEdit=new QLineEdit;
    NameLabel=new QLabel(tr(" full name :"));
    NameLineEdit=new QLineEdit;
    SexLabel=new QLabel(tr(" Gender :"));
    SexComboBox=new QComboBox;
    SexComboBox->addItem(tr(" Woman "));
    SexComboBox->addItem(tr(" male "));
    DepartmentLabel=new QLabel(tr(" department :"));
    DepartmentTextEdit=new QTextEdit;
    AgeLabel=new QLabel(tr(" remarks :"));
    AgeLineEdit=new QLineEdit;
    OtherLabel=new QLabel(tr(" remarks :"));
    OtherLabel->setFrameStyle(QFrame::Panel|QFrame::Sunken);
    // Set the style of the control ,setFrameStyle() yes QFrame Methods , Parameter with or | To set the panel style of the control , By shape (QFrame::Shape) And shadows (QFrame::shadow) Two cooperative decisions . There are six shapes , Namely NoFrame,Panel,Box,HLine,VLine,WinPanel; There are three kinds of shadows ,Plain,Raised,Sunken.
    LeftLayout=new QGridLayout();// Left layout , Because this layout manager is not the main layout manager , So there is no need to specify the parent window 
    // Add the controls that need layout to the layout 
    LeftLayout->addWidget(UserNameLabel,0,0);// user name 
    LeftLayout->addWidget(UserNameLineEdit,0,1);
    LeftLayout->addWidget(NameLabel,1,0);// full name 
    LeftLayout->addWidget(NameLineEdit,1,1);
    LeftLayout->addWidget(SexLabel,2,0);// Gender 
    LeftLayout->addWidget(SexComboBox,2,1);
    LeftLayout->addWidget(DepartmentLabel,3,0);// department 
    LeftLayout->addWidget(DepartmentTextEdit,3,1);
    LeftLayout->addWidget(AgeLabel,4,0);// Age 
    LeftLayout->addWidget(AgeLineEdit,4,1);
    LeftLayout->addWidget(OtherLabel,5,0,1,2);// other 
    LeftLayout->setColumnStretch(0,1);
    LeftLayout->setColumnStretch(1,3);
    // Set the proportion of space occupied by two columns , This example is set as 1:3, Even if the dialog size changes , The width ratio between the two columns remains unchanged 
    /********** On the right side ***********/
    HeadLabel =new QLabel(tr(" Head portrait :"));// Upper right corner 
    HeadIconLabel=new QLabel;
    QPixmap icon("312.pgn");
    HeadIconLabel->setPixmap(icon);
    HeadIconLabel->resize(icon.width(),icon.height());
    UpdateHeadBtn=new QPushButton(tr(" to update "));
    // Complete the layout of the upper right avatar selection area 
    TopRightLayout=new QHBoxLayout();
    TopRightLayout->setSpacing(20);// Set the spacing between controls to 20
    TopRightLayout->addWidget(HeadLabel);
    TopRightLayout->addWidget(HeadIconLabel);
    TopRightLayout->addWidget(UpdateHeadBtn);
    IntroductionLabel=new QLabel(tr(" Personal instructions :"));// Lower right corner 
    IntroductionTextEdit=new QTextEdit;
    // Complete the layout on the right 
    RightLayout=new QVBoxLayout();
    RightLayout->setMargin(10);
    RightLayout->addLayout(TopRightLayout);
    RightLayout->addWidget(IntroductionLabel);
    RightLayout->addWidget(IntroductionTextEdit);
    /******* Bottom ********/
    OkBtn=new QPushButton(tr(" determine "));
    CancelBtn=new QPushButton(tr(" Cancel "));
    // Complete the layout of the two buttons below 
    ButtomLayout=new QHBoxLayout();
    ButtomLayout->addStretch();
    // Insert a placeholder before the button , Align the two buttons to the right , And when the size of the entire dialog box changes , Make sure that the size of the button does not change .
    ButtomLayout->addWidget(OkBtn);
    ButtomLayout->addWidget(CancelBtn);
    /************/
    QGridLayout *mainLayout=new QGridLayout(this);
    // Realize the main layout , Specify the parent window this, You can also call this->setLayout(mainLayout) Realization 
    mainLayout->setMargin(15);// Set the margin of the dialog box to 15
    mainLayout->setSpacing(10);
    mainLayout->addLayout(LeftLayout,0,0);
    mainLayout->addLayout(RightLayout,0,1);
    mainLayout->addLayout(ButtomLayout,1,0,1,2);
    mainLayout->setSizeConstraint(QLayout::SetFixedSize);
    // Set optimization explicit , Even if the control presses its sizeHint() The size of is explicit , And the user cannot change the size of the dialog box .
}

Dialog::~Dialog()
{

}

Publisher : Full stack programmer stack length , Reprint please indicate the source :https://javaforall.cn/151802.html Link to the original text :https://javaforall.cn

原网站

版权声明
本文为[Full stack programmer webmaster]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/176/202206242153198922.html