Qt5开发学习之调色板与各种工具类(五)
来源:互联网 发布:淘宝的图片尺寸是多少 编辑:程序博客网 时间:2024/06/07 22:59
工具盒类
QToolBox
类称为称为工具盒类,QToolBox
提供一种列状的层叠窗体。QToolButton
提供了一种快速访问命令或选择项的按钮,通常在工具条中使用。抽屉效果是软件界面中一种常用形式,可以以一种直观的方式在有限大小的界面上扩展出更多的功能。
使用QToolBox
类实现一个类似QQ聊天界面的抽屉效:
myqq.h:
#include <QToolBox>#include <QToolButton>class MyQQ : public QToolBox{ Q_OBJECTpublic: MyQQ(QWidget *parent = 0); ~MyQQ();private: QToolButton *toolBtn1_1; QToolButton *toolBtn1_2; QToolButton *toolBtn1_3; QToolButton *toolBtn1_4; QToolButton *toolBtn1_5; QToolButton *toolBtn2_1; QToolButton *toolBtn2_2; QToolButton *toolBtn3_1; QToolButton *toolBtn3_2;};
myqq.cpp:
#include "myqq.h"#include <QGroupBox>#include <QVBoxLayout>MyQQ::MyQQ(QWidget *parent) : QToolBox(parent){ setWindowTitle(tr("My QQ")); this->resize(200, 800); toolBtn1_1 = new QToolButton; toolBtn1_1->setText(tr("11")); // 加载显示头像的图片,位置在构建之后产生的Debug文件目录下 toolBtn1_1->setIcon(QPixmap("11.jpg")); // 设定按钮显示大小为图片大小 toolBtn1_1->setIconSize(QPixmap("11.jpg").size()); // 设置图片自动弹起 toolBtn1_1->setAutoRaise(true); // 设置头像图片旁边显示名字 toolBtn1_1->setToolButtonStyle(Qt::ToolButtonTextBesideIcon); toolBtn1_2 = new QToolButton; toolBtn1_2->setText(tr("12")); toolBtn1_2->setIcon(QPixmap("12.jpg")); toolBtn1_2->setIconSize(QPixmap("12.jpg").size()); toolBtn1_2->setAutoRaise(true); toolBtn1_2->setToolButtonStyle(Qt::ToolButtonTextBesideIcon); toolBtn1_3 = new QToolButton; toolBtn1_3->setText(tr("13")); toolBtn1_3->setIcon(QPixmap("13.jpg")); toolBtn1_3->setIconSize(QPixmap("13.jpg").size()); toolBtn1_3->setAutoRaise(true); toolBtn1_3->setToolButtonStyle(Qt::ToolButtonTextBesideIcon); toolBtn1_4 = new QToolButton; toolBtn1_4->setText(tr("14")); toolBtn1_4->setIcon(QPixmap("14.jpg")); toolBtn1_4->setIconSize(QPixmap("14.jpg").size()); toolBtn1_4->setAutoRaise(true); toolBtn1_4->setToolButtonStyle(Qt::ToolButtonTextBesideIcon); toolBtn1_5 = new QToolButton; toolBtn1_5->setText(tr("15")); toolBtn1_5->setIcon(QPixmap("15.jpg")); toolBtn1_5->setIconSize(QPixmap("15.jpg").size()); toolBtn1_5->setAutoRaise(true); toolBtn1_5->setToolButtonStyle(Qt::ToolButtonTextBesideIcon); // 设置一个QGroupBox实例,对应一个抽屉 QGroupBox *groupBox1 = new QGroupBox; // 设置抽屉内的布局 QVBoxLayout *layout1 = new QVBoxLayout(groupBox1); layout1->setMargin(10); // 设置布局中各窗体显示的位置 layout1->setAlignment(Qt::AlignHCenter); // 布局中插入头像控件 layout1->addWidget(toolBtn1_1); layout1->addWidget(toolBtn1_2); layout1->addWidget(toolBtn1_3); layout1->addWidget(toolBtn1_4); layout1->addWidget(toolBtn1_5); // 插入一个占位符,使得所有按钮能够向上对齐 layout1->addStretch(); toolBtn2_1 = new QToolButton; toolBtn2_1->setText(tr("21")); toolBtn2_1->setIcon(QPixmap("21.jpg")); toolBtn2_1->setIconSize(QPixmap("21.jpg").size()); toolBtn2_1->setAutoRaise(true); toolBtn2_1->setToolButtonStyle(Qt::ToolButtonTextBesideIcon); toolBtn2_2 = new QToolButton; toolBtn2_2->setText(tr("22")); toolBtn2_2->setIcon(QPixmap("22.jpg")); toolBtn2_2->setIconSize(QPixmap("22.jpg").size()); toolBtn2_2->setAutoRaise(true); toolBtn2_2->setToolButtonStyle(Qt::ToolButtonTextBesideIcon); QGroupBox *groupBox2 = new QGroupBox; QVBoxLayout *layout2 = new QVBoxLayout(groupBox2); layout2->setMargin(10); layout2->setAlignment(Qt::AlignHCenter); layout2->addWidget(toolBtn2_1); layout2->addWidget(toolBtn2_2); toolBtn3_1 = new QToolButton; toolBtn3_1->setText(tr("31")); toolBtn3_1->setIcon(QPixmap("31.jpg")); toolBtn3_1->setIconSize(QPixmap("31.jpg").size()); toolBtn3_1->setAutoRaise(true); toolBtn3_1->setToolButtonStyle(Qt::ToolButtonTextBesideIcon); toolBtn3_2 = new QToolButton; toolBtn3_2->setText(tr("32")); toolBtn3_2->setIcon(QPixmap("32.jpg")); toolBtn3_2->setIconSize(QPixmap("32.jpg").size()); toolBtn3_2->setAutoRaise(true); toolBtn3_2->setToolButtonStyle(Qt::ToolButtonTextBesideIcon); QGroupBox *groupBox3 = new QGroupBox; QVBoxLayout *layout3 = new QVBoxLayout(groupBox3); layout3->setMargin(10); layout3->setAlignment(Qt::AlignHCenter); layout3->addWidget(toolBtn3_1); layout3->addWidget(toolBtn3_2); this->addItem((QWidget*)groupBox1, tr("我的好友")); this->addItem((QWidget*)groupBox2, tr("陌生人")); this->addItem((QWidget*)groupBox3, tr("黑名单"));}MyQQ::~MyQQ(){}
进度条
进度条用于告诉用户当前任务的进展情况,进度条对话框的使用有两种方式:模态方式和非模态方式。模态方式比较简单,使用QApplication::processEvents()
使事件保持正常进行状态;非模态方式则需要通过QTime
实现定时设置进度条的值。
Qt提供了两种进度条的显示方式:一种是QProgressBar
提供了横向或纵向显示进度控件表示方式,用来描述任务的完成情况。另一种是QProgressDialog
提供了一种慢速过程的进度对话框表示方式。标准进度条一般包括一个进度显示条,一个取消按钮以及一个标签。
实例使用一个对话框来完成两种进度条的显示方式:
Porgress.h:
#include <QDialog>#include <QProgressBar>#include <QProgressDialog>#include <QLabel>#include <QPushButton>#include <QLineEdit>#include <QComboBox>#include <QGridLayout>class Progress : public QDialog{ Q_OBJECTpublic: Progress(QWidget *parent = 0); ~Progress();private: QLabel *fileNumLabel; QLineEdit *fileNumEdit; QLabel *typeLabel; QComboBox *typeComboBox; QProgressBar *progressBar; QPushButton *startButton; QGridLayout *mainLayout;private slots: void startProgress();};
progress.cpp:
#include "progress.h"Progress::Progress(QWidget *parent) : QDialog(parent){ // 构造函数完成界面各个控件的创建 setWindowTitle(tr("ProgressBar")); // 设置字体 QFont font("ZYSong18030", 12); setFont(font); fileNumLabel = new QLabel(tr("file's number :")); fileNumEdit = new QLineEdit; fileNumEdit->setText(tr("100000")); typeLabel = new QLabel(tr("select type :")); progressBar = new QProgressBar; // 选择类型下拉框 typeComboBox = new QComboBox; typeComboBox->addItem(tr("ProgressBar")); typeComboBox->addItem(tr("ProgressDialog")); startButton = new QPushButton(tr("start")); mainLayout = new QGridLayout(this); mainLayout->addWidget(fileNumLabel, 0, 0); mainLayout->addWidget(fileNumEdit, 0, 1); mainLayout->addWidget(typeLabel, 1, 0); mainLayout->addWidget(typeComboBox, 1, 1); mainLayout->addWidget(progressBar, 2, 0, 1, 2); mainLayout->addWidget(startButton, 3, 1); mainLayout->setMargin(15); mainLayout->setSpacing(10); connect(startButton, &QPushButton::clicked, this, &Progress::startProgress);}Progress::~Progress(){}void Progress::startProgress(){ bool ok; // 获取输入的file number int num = fileNumEdit->text().toInt(&ok); // 获取下拉框选择的项目 if (typeComboBox->currentIndex() == 0) { // 设置进度条的步进范围是0到num progressBar->setRange(0, num); for (int i = 1; i < num + 1; ++i) { // 进度条向前递增 progressBar->setValue(i); } } else if (typeComboBox->currentIndex() == 1) { // 创建一个进度条模态对话框 QProgressDialog *progressDialog = new QProgressDialog(this); QFont font("ZYSong18030", 12); progressDialog->setFont(font); // 设置成模态对话框 progressDialog->setWindowModality(Qt::WindowModal); // 设置对话框出现需等待的时间,默认为4 progressDialog->setMinimumDuration(5); progressDialog->setWindowTitle(tr("please wait")); progressDialog->setLabelText(tr("copying......")); progressDialog->setCancelButtonText(tr("CANCEL")); progressDialog->setRange(0, num); for (int i = 1; i < num + 1; ++i) { progressDialog->setValue(i); // 点击取消按钮 if (progressDialog->wasCanceled()) { return; } } }}
调色板QPalette类
Qt提供QPalette
类专门用于管理对话框的外观显示。QPalette
类相当于对话框活控件的调色板,控制着窗口与控件的所有颜色信息,所有窗体或控件都包含一个QPalette
对象,在显示时按照它的QPalette
对象中各部分状态下的颜色显示进行绘制。 QPalette
有两个基本概念,一个是ColorGroup
另一个是ColorRole
。ColorGroup
有三种不同的状态:QPalette::Active
获得焦点的状态;QPalette::Inactivate
未获得焦点的状态;QPalette::Disable
不可用的状态。ColorRole
指的是颜色主题,即窗体中不同部位的颜色的分类。最常使用的函数是QPalette::setColor
函数对颜色进行设置的同时,还区分状态。QPalette::setBrush
通过画刷的设置对显示进行更改,即可以使用图片改变主题而非单一的颜色。
下面的代码使用QPalette
类实现改变不同控件颜色的方法:
palette.h:
#include <QDialog>#include <QLabel>#include <QComboBox>#include <QTextEdit>#include <QLineEdit>#include <QPalette>#include <QGridLayout>#include <QVBoxLayout>#include <QHBoxLayout>#include <QFrame>#include <QStringList>#include <QPushButton>class Palette : public QDialog{ Q_OBJECTpublic: Palette(QWidget *parent = 0); ~Palette(); // 设置左侧控制面板 void createCtrlFrame(); // 设置右侧显示面板 void createContnetFrame(); // 完成颜色下拉框中插入颜色的工作 void fillColorList(QComboBox *);private slots: void showWindow(); void showWindowText(); void showButton(); void showButtonText(); void showBase();private: // 左侧颜色选择面板 QFrame *ctrlFrame; QLabel *windowLabel; QComboBox *windowComboBox; QLabel *windowTextLabel; QComboBox *windowTextComboBox; QLabel *buttonLabel; QComboBox *buttonComboBox; QLabel *buttonTextLabel; QComboBox *buttonTextComboBox; QLabel *baseLabel; QComboBox *baseComboBox; // 右侧具体显示面板 QFrame *contentFrame; QLabel *label1; QComboBox *comboBox; QLabel *label2; QLineEdit *lineEdit; QTextEdit *textEdit; QPushButton *okBtn; QPushButton *cancelBtn;};
palette.cpp:
#include "palette.h"Palette::Palette(QWidget *parent) : QDialog(parent){ setWindowTitle(tr("paltte")); // 在构造函数中对界面进行构建 createCtrlFrame(); createContnetFrame(); QHBoxLayout *mainLayout = new QHBoxLayout(this); mainLayout->addWidget(ctrlFrame); mainLayout->addWidget(contentFrame);}Palette::~Palette(){}void Palette::createCtrlFrame(){ // 控制面板的显示 ctrlFrame = new QFrame; windowLabel = new QLabel(tr("QPaltte::Window :")); windowComboBox = new QComboBox; fillColorList(windowComboBox); connect(windowComboBox, SIGNAL(activated(int)), this, SLOT(showWindow())); windowTextLabel = new QLabel(tr("QPaltte::WindowText :")); windowTextComboBox = new QComboBox; fillColorList(windowTextComboBox); //connect(windowTextComboBox, &QComboBox::activated, this, &Palette::showWindowText); connect(windowTextComboBox, SIGNAL(activated(int)), this, SLOT(showWindowText())); buttonLabel = new QLabel(tr("QPaltte::Button :")); buttonComboBox = new QComboBox; fillColorList(buttonComboBox); //connect(buttonComboBox, &QComboBox::activated, this, &Palette::showButton); connect(buttonComboBox, SIGNAL(activated(int)), this, SLOT(showButton())); buttonTextLabel = new QLabel(tr("QPaltte::ButtonText :")); buttonTextComboBox = new QComboBox; fillColorList(buttonTextComboBox); //connect(buttonTextComboBox, &QComboBox::activated, this, &Palette::showButtonText); connect(buttonTextComboBox, SIGNAL(activated(int)), this, SLOT(showButtonText())); baseLabel = new QLabel(tr("QPaltte::Base :")); baseComboBox = new QComboBox; fillColorList(baseComboBox); //connect(baseComboBox, &QComboBox::activated, this, &Palette::showBase); connect(baseComboBox, SIGNAL(activated(int)), this, SLOT(showBase())); QGridLayout *mainLayout = new QGridLayout(ctrlFrame); mainLayout->setSpacing(15); mainLayout->addWidget(windowLabel, 0, 0); mainLayout->addWidget(windowComboBox, 0, 1); mainLayout->addWidget(windowTextLabel, 1, 0); mainLayout->addWidget(windowTextComboBox, 1, 1); mainLayout->addWidget(buttonLabel, 2, 0); mainLayout->addWidget(buttonComboBox, 2, 1); mainLayout->addWidget(buttonTextLabel, 3, 0); mainLayout->addWidget(buttonTextComboBox, 3, 1); mainLayout->addWidget(baseLabel, 4, 0); mainLayout->addWidget(baseComboBox, 4, 1);}void Palette::createContnetFrame(){ // 显示面板的显示 contentFrame = new QFrame; label1 = new QLabel(tr("plz input a num")); comboBox = new QComboBox; label2 = new QLabel(tr("plz input the str")); lineEdit = new QLineEdit; textEdit = new QTextEdit; QGridLayout *TopLayout = new QGridLayout; TopLayout->addWidget(label1, 0, 0); TopLayout->addWidget(comboBox, 0, 1); TopLayout->addWidget(label2, 1, 0); TopLayout->addWidget(lineEdit, 1, 1); TopLayout->addWidget(textEdit, 2, 0, 1, 2); okBtn = new QPushButton(tr("OK")); cancelBtn = new QPushButton(tr("CANCEL")); QHBoxLayout *bottomLayout = new QHBoxLayout; bottomLayout->addStretch(1); bottomLayout->addWidget(okBtn); bottomLayout->addWidget(cancelBtn); QVBoxLayout *mainLayout = new QVBoxLayout(contentFrame); mainLayout->addLayout(TopLayout); mainLayout->addLayout(bottomLayout);}void Palette::fillColorList(QComboBox *c){ // 获得Qt所有知道名称的颜色列表 QStringList colorList = QColor::colorNames(); // 为循环遍历做准备 QString color; // 对颜色名列表进行遍历 foreach (color, colorList) { // 新建一个QPixmap对象作为显示颜色的图标 QPixmap pix(QSize(70, 20)); // 填充颜色 pix.fill(QColor(color)); // 在下拉框中插入颜色图标 c->addItem(QIcon(pix), NULL); c->setIconSize(QSize(70, 20)); // 设置下拉框尺寸 c->setSizeAdjustPolicy(QComboBox::AdjustToContents); }}// 用于响应背景颜色的选择void Palette::showWindow(){ // 获取下拉框中选择的颜色 QStringList colorList = QColor::colorNames(); QColor color = QColor(colorList[windowComboBox->currentIndex()]); // 获取背景的QPalette对象 QPalette p = contentFrame->palette(); // 设置颜色为选中的颜色 p.setColor(QPalette::Window, color); // 把修改后的调色板信息更新到背景板上 contentFrame->setPalette(p); contentFrame->update();}// 用于响应文字的颜色,就是对前景色设置void Palette::showWindowText(){ // 获取下拉框中选择的颜色 QStringList colorList = QColor::colorNames(); QColor color = QColor(colorList[windowComboBox->currentIndex()]); // 获取文字的QPalette对象 QPalette p = contentFrame->palette(); // 设置颜色为选中的颜色 p.setColor(QPalette::WindowText, color); // 把修改后的调色板信息更新到背景板上 contentFrame->setPalette(p);}// 对按钮背景色的选择void Palette::showButton(){ // 获取下拉框中选择的颜色 QStringList colorList = QColor::colorNames(); QColor color = QColor(colorList[windowComboBox->currentIndex()]); // 获取按钮的QPalette对象 QPalette p = contentFrame->palette(); // 设置颜色为选中的颜色 p.setColor(QPalette::Button, color); // 把修改后的调色板信息更新到背景板上 contentFrame->setPalette(p); contentFrame->update();}// 对按钮上文字的选择void Palette::showButtonText(){ // 获取下拉框中选择的颜色 QStringList colorList = QColor::colorNames(); QColor color = QColor(colorList[windowComboBox->currentIndex()]); // 获取按钮上文字的QPalette对象 QPalette p = contentFrame->palette(); // 设置颜色为选中的颜色 p.setColor(QPalette::ButtonText, color); // 把修改后的调色板信息更新到背景板上 contentFrame->setPalette(p);}// 文本框背景色的选择void Palette::showBase(){ // 获取下拉框中选择的颜色 QStringList colorList = QColor::colorNames(); QColor color = QColor(colorList[windowComboBox->currentIndex()]); // 获取按钮上文字的QPalette对象 QPalette p = contentFrame->palette(); // 设置颜色为选中的颜色 p.setColor(QPalette::Base, color); // 把修改后的调色板信息更新到背景板上 contentFrame->setPalette(p);}
QTime类
QTime
的currentTime()
函数用于获取当前的系统时间,QTime
的toString()
函数用于将获取到的时间转换为字符串类型。为了便于显示,toString()
函数的参数需要指定转换后时间的显示格式,也可以直接利用Qt::DataFormat
作为参数指定显示的时间格式,如Qt::TextDate,Qt::ISODate,Qt::LocalDate
。 QLCDNumber
类重定义了鼠标按下事件和鼠标移动事件。
实例实现一个在桌面上显示的可以拖拽的电子时钟。
Clock.h:基类选择QDIalog,在头文件中将继承关系改为QLCDNumber
#include <QDialog>#include <QLCDNumber>#include <QPalette>#include <QTimer>#include <QTime>#include <QMouseEvent>class Clock : public QLCDNumber{ Q_OBJECTpublic: Clock(QWidget *parent = 0); ~Clock(); void mouseMoveEvent(QMouseEvent *); void mousePressEvent(QMouseEvent *);private slots: // 显示时间槽函数 void showTime();private: // 用于实现时钟里“:”的闪烁 bool showColon; // 记录鼠标移动的位置 QPoint dragPosition;};
Clock.cpp:
#include "clock.h"Clock::Clock(QWidget *parent) : QLCDNumber(parent){ // 构造函数完成时钟的显示,并连接槽 this->resize(150, 60); // 给时钟设置背景色 QPalette p = palette(); p.setColor(QPalette::Window, Qt::blue); setPalette(p); // 设置窗体为无边框窗体 setWindowFlags(Qt::FramelessWindowHint); // 设置窗体的透明度 setWindowOpacity(0.5); // 新建一个定时器对象 QTimer *timer = new QTimer(this); connect(timer, &QTimer::timeout, this, &Clock::showTime); // 以1000毫秒为周期启动定时器 timer->start(1000); showTime(); showColon = true; // 初始化}Clock::~Clock(){}void Clock::mouseMoveEvent(QMouseEvent *event){ // buttons() 返回鼠标的状态 if (event->buttons() & Qt::LeftButton) { move(event->globalPos() - dragPosition); event->accept(); }}void Clock::mousePressEvent(QMouseEvent *event){ // 如果鼠标点下的是左键 if (event->button() == Qt::LeftButton) { // 记录位置为鼠标相对于窗体左上角的相对位置 dragPosition = event->globalPos() - frameGeometry().topLeft(); event->accept(); } else if (event->button() == Qt::RightButton) { close(); }}void Clock::showTime(){ QTime time = QTime::currentTime(); QString text = time.toString("hh:mm"); if (showColon) { text[2] = ':'; // 交替显示‘:’ showColon = false; } else { text[2] = ' '; showColon = true; } // 将时间更新到窗体中 display(text);}
- Qt5开发学习之调色板与各种工具类(五)
- Qt5开发学习之Qt工具类与简单算法(二)
- Qt5开发学习之图形与SVG图像(八)
- Qt5开发学习之网络与通信(十二)
- Qt5开发学习之事件(十三)
- Qt5与QML(五)
- 【Qt5开发及实例】11、QPalette调色板使用
- Qt5开发学习之主窗体(六)
- Qt5开发学习之Qt概述(一)
- Qt5开发学习之布局管理(三)
- Qt5开发学习之Qt基本对话框(四)
- Qt5开发学习之文本编辑功能(七)
- Qt5开发学习之图形视图框架(九)
- Qt5开发学习之模型/视图结构(十)
- Qt5开发学习之文件及磁盘操作(十一)
- Qt5开发之各种按钮控件详细讲解
- 嵌入式开发之Qt学习篇——1、Qt5.1.0安装与配置
- 游戏开发新手入门之调色板和像素
- mysql存储过程
- T-SQL语句数据类型
- 0803JS
- VS快捷键总结
- FSM有限状态机学习
- Qt5开发学习之调色板与各种工具类(五)
- HDU 4734(数位DP)
- HDU 2838 Cow Sorting(树状数组求逆序数)
- LoadRunner与性能测试
- 2-1:JavaScript基础语法,流程控制等
- Android输入法弹出是控件被顶上去问题
- RN中native-echarts组件加入节点点击事件并传值
- 酒客项目日志20170803
- 1047. 编程团体赛(20)