qt 自定义工具栏
项目中,用到了自定义工具栏。
如下
mainwindow中:
_markToolBar = new MarkToolBar(this);
this->addToolBar(_markToolBar);
这个类中,可借鉴的也就初始化按钮和按钮触发。
头文件
1 #ifndef MARKTOOLBAR_H 2 #define MARKTOOLBAR_H 3 4 #include <QToolBar> 5 6 class QActionGroup; 7 8 class MarkToolBar : public QToolBar 9 { 10 Q_OBJECT 11 public: 12 explicit MarkToolBar(MarkType markType, QWidget *parent = 0, int nMessureType = -1); 13 explicit MarkToolBar(QWidget *parent = 0); 14 ~MarkToolBar(); 15 16 //增加Action 17 void addMarkType(const QString &iconPath, const QString &typeName, MarkType type); 18 19 signals: 20 void sigMarkActive(int, QString, bool bOther); 21 void removeDataSig(int type,const QString& key); 22 void notifyTreeSig(TDObject*object, const QString&key = QString()); 23 void SigCloseProperWgInMainwindow(); 24 void SigCloseMeasurationWidget();//关闭测量窗口 25 public slots: 26 //完成状态 27 void finishedMark(); 28 void SlotCloseMarkPropertyWg();//右键菜单属性时,关闭绘制时出现的属性窗口 29 30 private slots: 31 void slotActionTriggered(QAction *action); 32 33 private: 34 void init(); 35 36 37 private: 38 QActionGroup* _markGroup; 39 QMap<MarkType, QString> _nameMap; 40 }; 41 #endif // MARKTOOLBAR_H
源文件:
1 #include "marktoolbar.h" 2 #include "mainwindow.h" 3 4 5 MarkToolBar::MarkToolBar(MarkType markType, QWidget *parent, int nMessureType) 6 : QToolBar(parent) 7 , _markGroup(NULL) 8 { 9 init(); 10 } 11 12 MarkToolBar::MarkToolBar(QWidget *parent) 13 : QToolBar(parent) 14 , _markGroup(NULL) 15 { 16 init(); 17 } 18 19 20 MarkToolBar::~MarkToolBar() 21 { 22 23 } 24 25 void MarkToolBar::slotActionTriggered(QAction *action) 26 { 27 28 } 29 30 void MarkToolBar::addMarkType(const QString &iconPath, const QString &typeName, MarkType type) 31 { 32 QAction *action = new QAction(QIcon(iconPath), typeName, this); 33 addAction(action); 34 action->setCheckable(true); 35 action->setStatusTip(typeName); 36 action->setData((int)type); 37 _markGroup->addAction(action); 38 } 39 40 void MarkToolBar::finishedMark() 41 { 42 43 } 44 45 void MarkToolBar::init() 46 { 47 this->setToolButtonStyle(Qt::ToolButtonIconOnly); 48 49 //标记类型 50 _markGroup = new QActionGroup(this); 51 _markGroup->setExclusive(true); 52 53 addMarkType(PathHelper::getFileAbsolutePath("texture/point.png"), tr("point"), MARK_POINT); 54 addMarkType(PathHelper::getFileAbsolutePath("texture/line.png"), tr("line"), MARK_LINE); 55 addMarkType(PathHelper::getFileAbsolutePath("texture/polygon.png"), tr("polygon"), MARK_POLYGON); 56 addMarkType(PathHelper::getFileAbsolutePath("texture/text.png"), tr("text"), MARK_TEXT); 57 addMarkType(PathHelper::getFileAbsolutePath("texture/toolbarCircle.png"), tr("circle"), MARK_CIRCLE); 58 addMarkType(PathHelper::getFileAbsolutePath("texture/toolbarRing.png"), tr("ring"), MARK_RING); 59 addMarkType(PathHelper::getFileAbsolutePath("texture/toolbarArc.png"), tr("arc"), MARK_ARC); 60 addMarkType(PathHelper::getFileAbsolutePath("texture/toolbarFan.png"), tr("fan"), MARK_FAN); 61 addMarkType(PathHelper::getFileAbsolutePath("texture/toolbarEllipse.png"), tr("ellipse"), MARK_ELLIPSE); 62 addMarkType(PathHelper::getFileAbsolutePath("texture/toolbarRectangle.png"), tr("rectangle"), MARK_RECTANGLE); 63 64 //信号槽 65 connect(_markGroup, SIGNAL(triggered(QAction*)), this, SLOT(slotActionTriggered(QAction*))); 66 67 _nameMap.clear(); 68 _nameMap[MARK_POINT] = tr("NewPointMark"); 69 _nameMap[MARK_LINE] = tr("NewLineMark"); 70 _nameMap[MARK_POLYGON] = tr("NewPolygonMark"); 71 _nameMap[MARK_TEXT] = tr("NewTextMark"); 72 _nameMap[MARK_MODEL] = tr("NewModelMark"); 73 _nameMap[MARK_PARTICLE] = tr("NewParticlMark"); 74 _nameMap[MARK_CIRCLE] = tr("NewCircleMark"); 75 _nameMap[MARK_RING] = tr("NewRingMark"); 76 _nameMap[MARK_ARC] = tr("NewArcMark"); 77 _nameMap[MARK_FAN] = tr("NewFanMark"); 78 _nameMap[MARK_ELLIPSE] = tr("NewEllipseMark"); 79 _nameMap[MARK_RECTANGLE] = tr("NewRectangleMark"); 80 _nameMap[MARK_SCUTCHEON] = tr("NewScutcheonMark"); 81 _nameMap[MARK_BILLBOARD] = tr("NewBillBoardMark"); 82 }