1. 主要介绍了QStackedLayout、QListWidget、QDialogButtonBox的简单用法。
2. QStackedLayout:
  要使某个特定的子窗口部件可见,可以用setCurrentIndex()函数调用页号来实现。
  使用indexOf()可以获取子窗口部件的页号。
3. 创建此对话框的简单步骤:
  1) 建立一个QListWidget和一个QStackedWidget,并添加到QGridLayout布局中。
  2) 在QListWidget中用addItem(),添加新的项目(或者说是新列表?)。
  3) 在QStackedWidget中,新建QWisget类的分页,并用自窗口部件和布局来填充分页。
  4) 在QGridLayout中添加QDialogButtonBox按钮。
  5) 将列表窗口部件的currentRowChanged(int)信号和分组窗口部件的setCurrentIndex(int)
     槽连接起来。实现单击列表,在分组窗口中显示对应的信息。
  6) 把列表框的currentRow属性值设为0。
4. 使用QTabWidget会比此方法更简单。
5. preferencedialog.cpp

 /**/
#include "preferencedialog.h" #include <QListWidget>
#include <QStackedLayout>
#include <QGridLayout>
#include <QLabel>
#include <QVBoxLayout>
#include <QPushButton>
#include <QDialogButtonBox> PreferenceDialog::PreferenceDialog(QWidget *parent)
: QWidget(parent)
{
createAppearancePage();
createWebBrowserPage();
createMailAndNewsPage();
createAdvancedPage(); buttonBox = new QDialogButtonBox(QDialogButtonBox::Ok |
QDialogButtonBox::Cancel); listWidget = new QListWidget;
listWidget->addItem(tr("Appearance"));
listWidget->addItem(tr("Web Browse"));
listWidget->addItem(tr("Mail & News"));
listWidget->addItem(tr("Advanced")); stackedLayout = new QStackedLayout;
stackedLayout->addWidget(appearancePage);
stackedLayout->addWidget(webBrowserPage);
stackedLayout->addWidget(mailAndNewsPage);
stackedLayout->addWidget(advancedPage); connect(listWidget, SIGNAL(currentRowChanged(int)),
stackedLayout, SLOT(setCurrentIndex(int))); QGridLayout *mainLayout = new QGridLayout;
mainLayout->addWidget(listWidget, , );
mainLayout->addLayout(stackedLayout, , );
mainLayout->addWidget(buttonBox, , , , ); setLayout(mainLayout); listWidget->setCurrentRow(); /*设置起始页*/
} void PreferenceDialog::createAppearancePage()
{
appearancePage = new QWidget; QLabel *appearanceLabel = new QLabel(tr("appearance"));
QVBoxLayout *layout = new QVBoxLayout;
layout->addWidget(appearanceLabel); appearancePage->setLayout(layout);
} void PreferenceDialog::createWebBrowserPage()
{
webBrowserPage = new QWidget; QPushButton *webBrowserButton = new QPushButton(tr("web browser"));
QVBoxLayout *layout = new QVBoxLayout;
layout->addWidget(webBrowserButton); webBrowserPage->setLayout(layout);
} void PreferenceDialog::createMailAndNewsPage()
{
mailAndNewsPage = new QWidget; QLabel *mailAndNewsLabel = new QLabel(tr("mail & news"));
QVBoxLayout *layout = new QVBoxLayout;
layout->addWidget(mailAndNewsLabel); mailAndNewsPage->setLayout(layout);
} void PreferenceDialog::createAdvancedPage()
{
advancedPage = new QWidget; QPushButton *advancedButton = new QPushButton(tr("advanced"));
QVBoxLayout *layout = new QVBoxLayout;
layout->addWidget(advancedButton); advancedPage->setLayout(layout);
}

6. preferencedialog.h

 /**/
#ifndef PREFERENCEDIALOG_H
#define PREFERENCEDIALOG_H #include <QWidget> class QListWidget;
class QStackedLayout;
class QDialogButtonBox; class PreferenceDialog : public QWidget
{
Q_OBJECT public:
PreferenceDialog(QWidget *parent = ); private:
QListWidget *listWidget;
QStackedLayout *stackedLayout;
QWidget *appearancePage;
QWidget *webBrowserPage;
QWidget *mailAndNewsPage;
QWidget *advancedPage;
QDialogButtonBox *buttonBox; void createAppearancePage();
void createWebBrowserPage();
void createMailAndNewsPage();
void createAdvancedPage();
}; #endif /*PREFERENCEDIALOG_H*/

7. main.cpp

 /**/
#include <QApplication>
#include "preferencedialog.h" int main(int argc, char *argv[])
{
QApplication app(argc, argv); PreferenceDialog dialog;
dialog.show(); return app.exec();
}

8. 效果

C++ GUI Qt4编程(13)-6.2preferencedialog的更多相关文章

  1. C++ GUI Qt4编程(10)-3.4spreadsheet

    1. C++ GUI Qt4编程第三章,增加spreadsheet. 2. spreadsheet.h /**/ #ifndef SPREADSHEET_H #define SPREADSHEET_H ...

  2. C++ GUI Qt4编程(09)-3.3spreadsheet-toolbar

    1. C++ GUI Qt4编程第三章,增加工具栏.状态栏和快捷键. 2. mainwindow.h /**/ #ifndef MAINWINDOW_H #define MAINWINDOW_H #i ...

  3. C++ GUI Qt4编程(08)-3.2spreadsheet-resource

    1. C++ GUI Qt4编程第三章,图片使用资源机制法. 2. 步骤: 2-1. 在resource文件夹下,新建images文件,存放图片. 2-2. 新建spreadsheet.qrc文件,并 ...

  4. C++ GUI Qt4编程(07)-3.1menu

    1. C++ GUI Qt4编程第三章,添加menu菜单. 2. mainwindow.h #ifndef MAINWINDOW_H #define MAINWINDOW_H #include < ...

  5. C++ GUI Qt4编程(03)-1.3layout

    1. 根据C++ GUI Qt4编程(第二版)整理2. 系统:centos7:  Qt版本:5.5.13. 程序:layout.cpp #include <QApplication> #i ...

  6. C++ GUI Qt4编程(02)-1.2quit

    1. 根据C++ GUI Qt4编程(第二版)整理2. 系统:centos7:  Qt版本:5.5.13. 程序:quit.cpp #include <QApplication> #inc ...

  7. C++ GUI Qt4编程(01)-1.1Hello Qt

    1. 根据C++ GUI Qt4编程(第二版)整理2. 系统:centos7:  Qt版本:5.5.13. 程序:hello.cpp #include <QApplication> #in ...

  8. C++ GUI Qt4编程-创建自定义窗口部件

    C++ GUI Qt4编程-创建自定义窗口部件   Qtqt4 通过Qt窗口部件进行子类化或者直接对QWidget进行子类化,就可以创建自定义窗口部件,下面示范两种方式,并且也会说明如何把自定义窗口部 ...

  9. C++ GUI Qt4 编程 (第二版)

    [加拿大]JasminBlanchette [英]MarkSummerfield . 电子工业 2008. 前几天的问题多是因为版本不兼容的问题. QT本身Q4 Q5就有版本问题,然后集成到VS08 ...

随机推荐

  1. HTML中meta标签的作用与使用

    META标签用来描述一个HTML网页文档的属性 META标签可分为两大部分:HTTP-EQUIV和NAME变量. HTTP实例 HTML代码实例中有一项内容是 <meta http-equiv= ...

  2. Java基础语法(二)<运算符>

    运算符: 下面的都是相关的练习: 1.键盘录入一个三位整数数,请分别获取该三位数上每一位的数值 import java.util.Scanner; public class Test02 { publ ...

  3. Java String对象面试题分析

  4. (转)使用Jquery+EasyUI 进行框架项目开发案例讲解之四---组织机构管理源码分享

    原文地址:http://www.cnblogs.com/huyong/p/3404647.html 在上三篇文章  <使用Jquery+EasyUI进行框架项目开发案例讲解之一---员工管理源码 ...

  5. 转:[web]javascript 增加表單的input

    利用javascript增加form的input 這是js的部份 //用來區分不同input的name var element_count = 0; function add_element(obj) ...

  6. [bash] 显示配色

    #/bin/bash for STYLE in 0 1 2 3 4 5 6 7; do for FG in 30 31 32 33 34 35 36 37; do for BG in 40 41 42 ...

  7. ASP.NET MVC Controller 编程所涉及到的常用属性成员

    Controller (System.Web.Mvc.Controller) 1.获取路由中的各个值 Request.RequestContext.RouteData.Values["id& ...

  8. linux 改变系统时间

    date  查看系统时间 date -s 04/05/16  日期设置成2016年4月5日 date -s 15:03:32  日期设置成2016年4月5日15:03:32 上述两步可以直接写成这样一 ...

  9. java 文件硬盘存取 练习

    读写文件操作 对字符流文件读写 1 写文件 FileOutputStream 节点类 负责写字节 OutputStreamWriter 转化类  负责字节到字符转换 BufferedWriter 装饰 ...

  10. B - N皇后问题

    原文链接 一天课下,张老板研究起了国际象棋,渴望完美的他更改了棋盘的大小,在N*N的方格棋盘放置了N个皇后,希望它们不相互攻击(即任意2个皇后不允许处在同一排,同一列,也不允许处在与棋盘边框成45角的 ...