Qt5.9中工具栏类QToolBar的用法

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

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

widget.h

#ifndef WIDGET_H
#define WIDGET_H

#include 
#include 
#include 
#include 
#include 
#include 
#include 

class Widget : public QWidget
{
    Q_OBJECT

public:
    Widget(QWidget *parent = 0);
    void createView();
    void addControls();
    void craeteToolBar();
    void craeteMenu();

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 

Widget::Widget(QWidget *parent)
    : QWidget(parent)
{
    createView();
}

void Widget::createView()
{
    mainLayout = new QVBoxLayout;
    this->setLayout(mainLayout);

    addControls();                  //添加按钮控件
    craeteToolBar();                //创建工具栏
    craeteMenu();                   //创建按钮栏

    mainLayout->setMargin(0);
    mainLayout->setSpacing(0);
}

void Widget::addControls()
{
    //添加控件
    m_PushButton = new QPushButton("test");
    mainLayout->addWidget(m_PushButton);
}

void Widget::craeteToolBar()
{
    //创建工具栏
    m_ToolBar = new QToolBar;
    m_ToolBarActionGroup = new QActionGroup(this);
    QList 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::craeteMenu()
{
    //创建菜单栏
    m_Menu = new QMenu;
    m_MenuActionGroup = new QActionGroup(this);
    QList 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()<text()==%1").arg(m_ActionMenu1->text());
    }
    else if (action->text().compare(m_ActionMenu2->text())==0)
    {
        qDebug()<text()==%1").arg(m_ActionMenu2->text());
    }
}

void Widget::On_ClickedToolBarActionGroup(QAction *action)
{
    if(action->text().compare(m_ActionToolBar1->text())==0)
    {
        qDebug()<text()==%1").arg(m_ActionToolBar1->text());
    }
    else if (action->text().compare(m_ActionToolBar2->text())==0)
    {
        qDebug()<text()==%1").arg(m_ActionToolBar2->text());
    }
}

 

main.cpp

 

#include "widget.h"
#include 

int main(int argc, char *argv[])
{
    QApplication a(argc, argv);
    Widget w;
    w.resize(960,640);
    w.show();

    return a.exec();
}

 

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

Qt5.9中工具栏类QToolBar的用法_第1张图片

 

总结:

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

 

 

参考内容:

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

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

你可能感兴趣的:(嵌入式积累,Qt基础知识,Qt工程应用,Qt5.9,工具栏,QToolBar,QAction,QMenu)