最近在学习QT编程,在百度贴吧中看到有人问关于窗口布局的问题 结合最近学到的东西 用代码实现了下该功能
http://tieba.baidu.com/p/2473655082
QWidget* widget = new QWidget;
widget->setFixedSize(350,300);
widget->setWindowTitle("UnicomMini");
//设置支持中文
QTextCodec *codec = QTextCodec::codecForName("System");
QTextCodec::setCodecForLocale(codec);
QTextCodec::setCodecForTr(codec);
QTextCodec::setCodecForCStrings(codec);
QLabel* srceenAreaLabel = new QLabel(QObject::tr("屏幕区"), widget);
QGridLayout* centerBtnLayout = new QGridLayout;
// centerBtnLayout->addStretch();
QPushButton* chongzhiBtn = new QPushButton(QObject::tr("充值缴费"), widget);
QPushButton* yuechaxunBtn = new QPushButton(QObject::tr("余额查询"), widget);
QPushButton* zhangdanchaxunBtn = new QPushButton(QObject::tr("账单查询"), widget);
QPushButton* detailchaxunBtn = new QPushButton(QObject::tr("详细查询"), widget);
QPushButton* buycardBtn = new QPushButton(QObject::tr("买电子卡"), widget);
QPushButton* syssettingBtn = new QPushButton(QObject::tr("系统设置"), widget);
centerBtnLayout->addWidget(chongzhiBtn,0,0,1,1);
centerBtnLayout->addWidget(yuechaxunBtn,0,1,1,1);
centerBtnLayout->addWidget(zhangdanchaxunBtn,1,0,1,1);
centerBtnLayout->addWidget(detailchaxunBtn,1,1,1,1);
centerBtnLayout->addWidget(buycardBtn,2,0,1,1);
centerBtnLayout->addWidget(syssettingBtn,2,1,1,1);
centerBtnLayout->setContentsMargins(40,1, 40, 0); // 设置下距离边框的距离
QLabel* keyAreaLabel = new QLabel(QObject::tr("键盘区"), widget);
QHBoxLayout* hKeyAreaLayout = new QHBoxLayout;
QStringList btnsTextList;
btnsTextList<<"0"<<"1"<<"2"<<"3"<<"4"<<"5"<<"6"<<"7"<<"8"<<"9"<<"<-";
for( int i= 0; i< 11; i++)
{
QPushButton* btn = new QPushButton(btnsTextList.at(i), widget );
btn->setFixedSize(25,25);
hKeyAreaLayout->addWidget(btn);
}
QGridLayout* direction = new QGridLayout;
QPushButton* topBtn = new QPushButton(QObject::tr("上"),widget);
QPushButton* leftBtn = new QPushButton(QObject::tr("左"),widget);
QPushButton* downBtn = new QPushButton(QObject::tr("下"),widget);
QPushButton* rightBtn = new QPushButton(QObject::tr("右"),widget);
direction->addWidget(topBtn, 0,1, 1,1);
direction->addWidget(leftBtn, 1,0, 1,1);
direction->addWidget(downBtn, 1,1, 1,1);
direction->addWidget(rightBtn, 1,2, 1,1);
QVBoxLayout* mainLayout = new QVBoxLayout;
mainLayout->addWidget(srceenAreaLabel);
mainLayout->addLayout(centerBtnLayout);
mainLayout->addWidget(keyAreaLabel);
mainLayout->addLayout(hKeyAreaLayout);
mainLayout->setSpacing(10);
mainLayout->addLayout(direction);
widget->setLayout(mainLayout);
widget->show();