Qt 实现基本布局管理 ---实验

来源:互联网 发布:快餐收银软件ynrj 编辑:程序博客网 时间:2024/05/22 16:54

1.新建Qt控件项目--QTgui 应用--QDialog

.h文件

#ifndef DIALOG_H#define DIALOG_H#include <QDialog>#include <QtGui/QLabel>#include <QtGui/QLineEdit>#include <QtGui/QPushButton>#include <QtGui/QComboBox>#include <QtGui/QDialog>#include <QtGui/QLayout>#include <QtGui/QTextEdit>namespace Ui {class Dialog;}class Dialog : public QDialog{    Q_OBJECT    public:   // explicit Dialog(QWidget *parent = 0);    ~Dialog();    Dialog(QWidget *parent=0,Qt::WindowFlags f=0);    QLabel *label1;    QLabel *Label2;    QLabel *Label3;    QLabel *Label4;    QLabel *Label5;    QLabel *Label6;    QLabel *Label7;    QLabel *labelOther;    QLabel *labelIcon;    QLineEdit *lineEditUser;    QLineEdit *LineEditName;    QComboBox *comboBoxSex;    QTextEdit *textEditDepartment;    QLineEdit *lineEditAge;    QTextEdit *TextEditDisc;    QPushButton *pushButtonIcon;    QPushButton *pushButtonOK;    QPushButton *pushButtonExit;private:    Ui::Dialog *ui;};#endif // DIALOG_H

.cpp

#include "dialog.h"#include "ui_dialog.h"#include<QtGui>Dialog::~Dialog(){    delete ui;} Dialog::Dialog(QWidget *parent,Qt::WindowFlags f):QDialog(parent,f){    setWindowTitle(tr("User Information"));    label1=new QLabel(tr("User Name: "));    Label2=new QLabel(tr("Name"));    Label3=new QLabel(tr("Sex"));    Label4=new QLabel(tr("Department"));    Label5=new QLabel(tr("Age"));    labelOther=new QLabel(tr("Remark"));    labelOther->setFrameStyle(QFrame::Panel|QFrame::Sunken);    lineEditUser=new QLineEdit;    LineEditName=new QLineEdit;    comboBoxSex=new QComboBox;    comboBoxSex->insertItem(0,tr("Female"));    comboBoxSex->insertItem(1,tr("Male"));    textEditDepartment=new QTextEdit();    lineEditAge=new QLineEdit;    QGridLayout *leftLayout=new QGridLayout();    int labelCol=0;    int contentCol=1;    leftLayout->addWidget(label1,0,labelCol);    leftLayout->addWidget(lineEditUser,0,contentCol);    leftLayout->addWidget(Label2,1,labelCol);    leftLayout->addWidget(LineEditName,1,contentCol);    leftLayout->addWidget(Label3,2,labelCol);    leftLayout->addWidget(comboBoxSex,2,contentCol);    leftLayout->addWidget(Label4,3,labelCol,Qt::AlignTop);    leftLayout->addWidget(textEditDepartment,3,contentCol);    leftLayout->addWidget(Label5,4,labelCol);    leftLayout->addWidget(lineEditAge,4,contentCol);    leftLayout->addWidget(labelOther,5,labelCol,1,2);    leftLayout->setColumnStretch(0,1);    leftLayout->setColumnStretch(1,3);    Label7=new QLabel(tr("Head"));    labelIcon=new QLabel();    QPixmap icon("butterfly2.png");    labelIcon->resize(icon.width(),icon.height());    labelIcon->setPixmap(icon);    pushButtonIcon=new QPushButton();    pushButtonIcon->setText(tr("Change"));    QHBoxLayout *hLayout=new QHBoxLayout;    hLayout->setSpacing(20);    hLayout->addWidget(Label7);    hLayout->addWidget(labelIcon);    hLayout->addWidget(pushButtonIcon);    Label6=new QLabel(tr("Invdividual"));    TextEditDisc=new QTextEdit();    QVBoxLayout *rightLayout=new QVBoxLayout();    rightLayout->setMargin(10);    rightLayout->addLayout(hLayout);    rightLayout->addWidget(Label6);    rightLayout->addWidget(TextEditDisc);    pushButtonOK=new QPushButton(tr("OK"));    pushButtonExit=new QPushButton(tr("cancel"));    QHBoxLayout *bottomLayout=new QHBoxLayout();    bottomLayout->addStretch();    bottomLayout->addWidget(pushButtonOK);    bottomLayout->addWidget(pushButtonExit);    QGridLayout *mainLayout=new QGridLayout(this);    mainLayout->setSpacing(15);    mainLayout->setMargin(15);    mainLayout->addLayout(leftLayout,0,0);    mainLayout->addLayout(rightLayout,0,1);    mainLayout->addLayout(bottomLayout,1,0,1,2);    mainLayout->setSizeConstraint(QLayout::SetFixedSize);}

主函数

#include <QtGui/QApplication>#include "dialog.h"int main(int argc, char *argv[]){    QApplication a(argc, argv);    Dialog w;    w.show();        return a.exec();}




 

原创粉丝点击