工具栏类QToolBar与QActionGroup、Slot结合的用法

原文:https://blog.csdn.net/naibozhuan3744/article/details/80855818

         https://blog.csdn.net/naibozhuan3744/article/details/82192298

本文主要总结菜单栏类QMenu和工具栏类QToolBar的用法,具体的用法,将通过一个实例进行讲解。

1.1新建一个widget工程,不要勾选ui界面设计,然后分别在widget.h,widget.cpp,main.cpp分别添加如下代码。

widget.h

#ifndef WIDGET_H
#define WIDGET_H
 
#include <QWidget>
#include <QVBoxLayout>
#include <QHBoxLayout>
#include <QMenu>
#include <QActionGroup>
#include <QPushButton>
#include <QToolBar>
 
class Widget : public QWidget
{
    Q_OBJECT
 
public:
    Widget(QWidget *parent = 0);
    void createView();
    void addControls();
    void createToolBar();
    void createMenu();
 
private slots:
    void On_ClickedMenuActionGroup(QAction *action);
    void On_ClickedToolBarActionGroup(QAction *action);
 
private:
    QVBoxLayout *mainLayout;
//    QVBoxLayout *mainWidgetLayout;
 
//    QWidget *mainWidget;
 
    QPushButton *m_PushButton;
    QMenu *m_Menu;
    QToolBar *m_ToolBar;
    QActionGroup *m_MenuActionGroup;
    QActionGroup *m_ToolBarActionGroup;
    QAction *m_ActionMenu1;
    QAction *m_ActionMenu2;
    QAction *m_ActionToolBar1;
    QAction *m_ActionToolBar2;
};
 
#endif // WIDGET_H

widget.cpp

#include "widget.h"
#include <QDebug>
 
Widget::Widget(QWidget *parent)
    : QWidget(parent)
{
    createView();
}
 
void Widget::createView()
{
    mainLayout = new QVBoxLayout;
    this->setLayout(mainLayout);
 
    addControls();                  //添加按钮控件
    createToolBar();                //创建工具栏
    createMenu();                   //创建按钮栏
 
    mainLayout->setMargin(0);
    mainLayout->setSpacing(0);
}
 
void Widget::addControls()
{
    //添加控件
    m_PushButton = new QPushButton("test");
    mainLayout->addWidget(m_PushButton);
}
 
void Widget::createToolBar()
{
    //创建工具栏
    m_ToolBar = new QToolBar;
    m_ToolBarActionGroup = new QActionGroup(this);
    QList<QAction*> m_ToolBarList;
    
    m_ActionToolBar1 = new QAction(QStringLiteral("ToolBar1"));
    m_ActionToolBar2 = new QAction(QStringLiteral("ToolBar2"));
    m_ActionToolBar1->setCheckable(true);
    m_ActionToolBar2->setCheckable(true);
    m_ToolBarList.append(m_ActionToolBar1);
    m_ToolBarList.append(m_ActionToolBar2);
    m_ToolBarActionGroup->addAction(m_ActionToolBar1);
    m_ToolBarActionGroup->addAction(m_ActionToolBar2);    
    
    m_ToolBar->addActions(m_ToolBarList);
    m_ToolBar->setStyleSheet("background-color:rgb(200,40,43);color:rgb(204,204,204)");
    mainLayout->addWidget(m_ToolBar);
    
    connect(m_ToolBarActionGroup,SIGNAL(triggered(QAction*)),this,SLOT(On_ClickedToolBarActionGroup(QAction *)));
}
 
void Widget::createMenu()
{
    //创建菜单栏
    m_Menu = new QMenu;
    m_MenuActionGroup = new QActionGroup(this);
    QList<QAction*> m_MenuList;
    m_ActionMenu1 = new QAction(QStringLiteral("Menu1"));
    m_ActionMenu2 = new QAction(QStringLiteral("Menu2"));
    m_ActionMenu1->setCheckable(true);
    m_ActionMenu2->setCheckable(true);
    m_MenuList.append(m_ActionMenu1);
    m_MenuList.append(m_ActionMenu2);
    m_MenuActionGroup->addAction(m_ActionMenu1);
    m_MenuActionGroup->addAction(m_ActionMenu2);
    connect(m_MenuActionGroup,SIGNAL(triggered(QAction*)),this,SLOT(On_ClickedMenuActionGroup(QAction *)));
    m_Menu->addActions(m_MenuList);
    mainLayout->addWidget(m_Menu);
}
 
void Widget::On_ClickedMenuActionGroup(QAction *action)
{
    if(action->text().compare(m_ActionMenu1->text())==0)
    {
        qDebug()<<tr("m_ActionMenu1->text()==%1").arg(m_ActionMenu1->text());
    }
    else if (action->text().compare(m_ActionMenu2->text())==0)
    {
        qDebug()<<tr("m_ActionMenu2->text()==%1").arg(m_ActionMenu2->text());
    }
}
 
void Widget::On_ClickedToolBarActionGroup(QAction *action)
{
    if(action->text().compare(m_ActionToolBar1->text())==0)
    {
        qDebug()<<tr("m_ActionToolBar1->text()==%1").arg(m_ActionToolBar1->text());
    }
    else if (action->text().compare(m_ActionToolBar2->text())==0)
    {
        qDebug()<<tr("m_ActionToolBar2->text()==%1").arg(m_ActionToolBar2->text());
    }
}

main.cpp

#include "widget.h"
#include <QApplication>
 
int main(int argc, char *argv[])
{
    QApplication a(argc, argv);
    Widget w;
    w.resize(960,640);
    w.show();
 
    return a.exec();
}

1.2程序构建运行后,结果如下图所示:

总结:

构建界面的工具栏,主要用到QToolBar类,先用函数QToolBar::addAction(QAtion*)或者QToolBar::addActions(QList<QAtion*>)将对应的QAction工具挂载进来。然后将该QToolBar挂载到QWidget上(跟普通的QPushButton一样挂载)。

参考内容:

http://www.cnblogs.com/findumars/p/4951636.html(重点参考)

http://www.cnblogs.com/grandyang/p/4663277.html(QActionGroup用法)

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值