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. 效果

最新文章

  1. 使用 Code Snippet 简化 Coding
  2. Javascript核心概述 - 深入了解javascript
  3. delphi xe3的helper语法 good
  4. Google Developing for Android 学习总结
  5. bzoj1061--线性规划
  6. 利用有限自动机(finite automata)进行模式匹配
  7. Domain Driven Design
  8. Eclipse+Spring+SpringMVC+Maven+Mybatis+MySQL+Tomcat项目搭建
  9. php使用rc4加密算法
  10. dotnet core 3.0 linux 部署小贴士
  11. 判断PDF文件是否相同(通过二进制流判断)
  12. jq解决a连接锚点平滑过渡
  13. Linux:Day8(上) 文件系统挂载和管理工具
  14. python 打开文件对话框 filedialog tkinter GUI 编程
  15. Fabric Dev开发调试模式的搭建过程
  16. 信号处理函数的返回sigsetjmp/siglongjmp
  17. Markdown 版本演进
  18. ibatis中:org.springframework.jdbc.UncategorizedSQLException:异常
  19. Python开发基础-Day25-28FTP项目(待补充)
  20. iOS 进阶---推送通知之本地通知

热门文章

  1. Python基础 之 变量、用户交互、if条件语句、while循环语句、编码、逻辑运算
  2. ssh试卷
  3. Hadoop深入学习:MapTask详解
  4. 【Head First Java 读书笔记】(六)认识Java API
  5. 通过vb.net 和NPOI实现对excel的读操作
  6. delphi 创建DLL文件 及其调用和注意事项
  7. attachEvent 与 addEventListener 的监听
  8. C# -- 泛型(2)
  9. webform Response的一些成员
  10. Web标准及网站的可用性、可访问性