C++ GUI Qt4编程(13)-6.2preferencedialog
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的更多相关文章
- C++ GUI Qt4编程(10)-3.4spreadsheet
1. C++ GUI Qt4编程第三章,增加spreadsheet. 2. spreadsheet.h /**/ #ifndef SPREADSHEET_H #define SPREADSHEET_H ...
- C++ GUI Qt4编程(09)-3.3spreadsheet-toolbar
1. C++ GUI Qt4编程第三章,增加工具栏.状态栏和快捷键. 2. mainwindow.h /**/ #ifndef MAINWINDOW_H #define MAINWINDOW_H #i ...
- C++ GUI Qt4编程(08)-3.2spreadsheet-resource
1. C++ GUI Qt4编程第三章,图片使用资源机制法. 2. 步骤: 2-1. 在resource文件夹下,新建images文件,存放图片. 2-2. 新建spreadsheet.qrc文件,并 ...
- C++ GUI Qt4编程(07)-3.1menu
1. C++ GUI Qt4编程第三章,添加menu菜单. 2. mainwindow.h #ifndef MAINWINDOW_H #define MAINWINDOW_H #include < ...
- C++ GUI Qt4编程(03)-1.3layout
1. 根据C++ GUI Qt4编程(第二版)整理2. 系统:centos7: Qt版本:5.5.13. 程序:layout.cpp #include <QApplication> #i ...
- C++ GUI Qt4编程(02)-1.2quit
1. 根据C++ GUI Qt4编程(第二版)整理2. 系统:centos7: Qt版本:5.5.13. 程序:quit.cpp #include <QApplication> #inc ...
- C++ GUI Qt4编程(01)-1.1Hello Qt
1. 根据C++ GUI Qt4编程(第二版)整理2. 系统:centos7: Qt版本:5.5.13. 程序:hello.cpp #include <QApplication> #in ...
- C++ GUI Qt4编程-创建自定义窗口部件
C++ GUI Qt4编程-创建自定义窗口部件 Qtqt4 通过Qt窗口部件进行子类化或者直接对QWidget进行子类化,就可以创建自定义窗口部件,下面示范两种方式,并且也会说明如何把自定义窗口部 ...
- C++ GUI Qt4 编程 (第二版)
[加拿大]JasminBlanchette [英]MarkSummerfield . 电子工业 2008. 前几天的问题多是因为版本不兼容的问题. QT本身Q4 Q5就有版本问题,然后集成到VS08 ...
随机推荐
- Python3 网络爬虫开发实战学习弱点书签
1. urllib.robotparse模块对robot.txt文件的解析,can_fetch()方法和parse()方法. Page121 2. lxml.etree模块自动补全Html代码,Htm ...
- dynamic和匿名类和var的混合使用 没提示照样点
using System;using System.Collections;using System.Collections.Generic;using System.Linq;using Syste ...
- ESP8266文档阅读ESP8266 SDK 入门指南
ESP8266 SDK 入门指南 1.概述 1.2.ESP8266 HDK 1.3.ESP8266 SDK 1.4.ESP8266 FW 1.5.ESP8266 工具集 2.1.开发板方案 3.软件 ...
- wpf附加属性理解
WPF附加属性 http://www.cnblogs.com/tianyou/archive/2012/12/27/2835670.html WPF属性(二)附加属性 http://blog.csdn ...
- 随便写个bat存档
@echo off @COLOR @echo ------------切换Hosts环境--------------- :Again @set /p choice="切换模式:A:应用环境, ...
- spring深入了解心得
spring 主要核心组件 :Core.上下文(Context) .实体(Bean): spring 主要由两大特色:控制反转(IOC).面向对象(AOP): spring中Core主要用于组建Bea ...
- 「POJ 2699」The Maximum Number of Strong Kings
题目链接 戳我 \(Describe\) 一场联赛可以表示成一个完全图,点表示参赛选手,任意两点u, v之间有且仅有一条有向边\((u, v)\)或\((v, u)\),表示\(u\)打败\(v\)或 ...
- 51 nod 1350 斐波那契表示
每一个正整数都可以表示为若干个斐波那契数的和,一个整数可能存在多种不同的表示方法,例如:14 = 13 + 1 = 8 + 5 + 1,其中13 + 1是最短的表示(只用了2个斐波那契数).定义F(n ...
- Django 学习:为窗体加上防机器人的验证机制(验证码功能)
这里我们使用 django-simple-captcha 模块,官方介绍如下:https://github.com/mbi/django-simple-captcha 一键安装: pip instal ...
- 消息中间件ActiveMQ、RabbitMQ、RocketMQ、ZeroMQ、Kafka如何选型?
最近要为公司的消息队列中间件进行选型,市面上相关的开源技术又非常多,如ActiveMQ.RabbitMQ.ZeroMQ.Kafka,还有阿里巴巴的RocketMQ等. 这么多技术,如何进行选型呢? 首 ...