码迷,mamicode.com
首页 > 其他好文 > 详细

Qt系列04 布局管理

时间:2018-05-20 15:21:51      阅读:171      评论:0      收藏:0      [点我收藏+]

标签:.exe   tle   dia   cal   结果   float   ble   set   技术   

本节介绍Qt常用布局管理,QSplitter类、QDockWidget类和QStackedWidget类。

QSplitter类

#include "widget.h"
#include <QApplication>
#include <QSplitter>
#include <QLineEdit>

int main(int argc, char *argv[]) {
  QApplication a(argc, argv);
  QSplitter *splitterMain = new QSplitter(Qt::Horizontal, 0);

  QSplitter *splitterLeft = new QSplitter(Qt::Vertical, splitterMain);
  QLineEdit *lineEditRight = new QLineEdit("Right LineEdit", splitterMain);

  QSplitter *splitterUp = new QSplitter(Qt::Horizontal, splitterLeft);
  QSplitter *splitterDown = new QSplitter(Qt::Horizontal, splitterLeft);

  QLineEdit *lineEditUp = new QLineEdit("Up LineEdit", splitterUp);
  QLineEdit *lineEditDown = new QLineEdit("Down LineEdit", splitterDown);

  splitterMain->show();

  //    Widget w;
  //    w.show();

  return a.exec();
}

编译,运行结果

技术分享图片

 QDockWidget类

#include "dockwindow.h"
#include <QDockWidget>
#include <QTextEdit>

DockWindow::DockWindow(QWidget *parent) : QMainWindow(parent) {
  setWindowTitle(tr("Dock Window"));
  QTextEdit *te = new QTextEdit(this);
  te->setText(tr("Main Text Edit"));
  te->setAlignment(Qt::AlignCenter);
  setCentralWidget(te);

  QDockWidget *dock1 = new QDockWidget(tr("Dock Widget 1"), this);
  dock1->setFeatures(QDockWidget::DockWidgetMovable);
  dock1->setAllowedAreas(Qt::LeftDockWidgetArea | Qt::RightDockWidgetArea);
  QTextEdit *te1 = new QTextEdit();
  te1->setText(tr("Text Edit 1, This Text Edit Widget is Movable!"));
  dock1->setWidget(te1);
  addDockWidget(Qt::RightDockWidgetArea, dock1);

  QDockWidget *dock2 = new QDockWidget(tr("Dock Widget 2"), this);
  dock2->setFeatures(QDockWidget::DockWidgetClosable |
                     QDockWidget::DockWidgetFloatable);
  QTextEdit *te2 = new QTextEdit();
  te2->setText(
      tr("Text Edit 2, This Text Edit Widget is Closable and Floatable!"));
  dock2->setWidget(te2);
  addDockWidget(Qt::RightDockWidgetArea, dock2);

  QDockWidget *dock3 = new QDockWidget(tr("Dock Widget 3"), this);
  dock3->setFeatures(QDockWidget::AllDockWidgetFeatures);
  QTextEdit *te3 = new QTextEdit();
  te3->setText(tr("Text Edit 3, ALL Dock Widget Features!"));
  dock3->setWidget(te3);
  addDockWidget(Qt::RightDockWidgetArea, dock3);
}

编译,运行

技术分享图片

QStackedWidget类

#include "stackdlg.h"
#include <QDialog>
#include <QLabel>
#include <QLayout>
#include <QListWidget>
#include <QStackedWidget>
#include <QWidget>

StackDlg::StackDlg(QWidget *parent) : QDialog(parent) {
  setWindowTitle(tr("StackedWidget"));
  list = new QListWidget(this);
  list->insertItem(0, tr("Window 1"));
  list->insertItem(1, tr("Window 2"));
  list->insertItem(2, tr("Window 3"));

  label1 = new QLabel(tr("Window Test 1"));
  label2 = new QLabel(tr("Window Test 2"));
  label3 = new QLabel(tr("Window Test 3"));

  stack = new QStackedWidget(this);
  stack->addWidget(label1);
  stack->addWidget(label2);
  stack->addWidget(label3);

  QHBoxLayout *mainLayout = new QHBoxLayout(this);
  mainLayout->setMargin(5);
  mainLayout->setSpacing(5);
  mainLayout->addWidget(list);
  mainLayout->addWidget(stack, 0, Qt::AlignHCenter);
  mainLayout->setStretchFactor(list, 1);
  mainLayout->setStretchFactor(stack, 3);

  connect(list, SIGNAL(currentRowChanged(int)), stack,
          SLOT(setCurrentIndex(int)));
}

编译,运行结果

技术分享图片

 

Qt系列04 布局管理

标签:.exe   tle   dia   cal   结果   float   ble   set   技术   

原文地址:https://www.cnblogs.com/tangjicheng/p/9062277.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!