解释:
1、封装内容:最小化、最大化、还原、关闭 四个按钮状态只显示部分
2、使用 enum 枚举状态
函数定义:
enum ButtonType { MIN_CLOSE_BUTTON = 0, //最小化及关闭按钮 MIN_MAX_CLOSE_BUTTON, //最小化、最大化及关闭按钮 CLOSE_BUTTON, //仅有关闭按钮 COUNT_ //总数 }; class TitleBar { Q_OBJECT public://对外暴露的接口 TitleBar(ButtonType btnType); private://内部实现 void init(); void setTitleButton(); ButtonType _btnType; }
源代码:
TitleBar::TitleBar(ButtonType btnType) :_btnType(btnType) { ui.setupUi(this); init(); } void TitleBar::setTitleButton() { switch (_btnType) { case MIN_CLOSE_BUTTON: { ui.ButtonRestore->setVisible(false); ui.ButtonMax->setVisible(false); break; } case MIN_MAX_CLOSE_BUTTON: { ui.ButtonRestore->setVisible(false); break; } case CLOSE_BUTTON: { ui.ButtonRestore->setVisible(false); ui.ButtonMax->setVisible(false); ui.ButtonMin->setVisible(false); break; } default: break; } }
使用测试:
#include "TitleBar.h" int main(int argc, char *argv[]) { QApplication a(argc, argv); TitleBar t(MIN_MAX_CLOSE_BUTTON); t.show(); return a.exec(); }

992

被折叠的 条评论
为什么被折叠?



