欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

QT实现工具箱QButtonGroup

程序员文章站 2022-05-01 17:53:00
工具箱有两种实现方式,一是运用工具栏QToolBar,二是运用QButtonGroup,在按钮组内创建多个QPushButton,每个按钮对应不同的槽函数。 本节的“...

工具箱有两种实现方式,一是运用工具栏QToolBar,二是运用QButtonGroup,在按钮组内创建多个QPushButton,每个按钮对应不同的槽函数。

本节的“工具箱”窗口是QDockWidget类型,通过在停靠窗口上添加控件来实现工具箱功能。

QT实现工具箱QButtonGroup

代码

在头文件中添加

private:
    int drawType;                               // 绘制类型
    QButtonGroup *toolButtonGroup;              // 按钮组
    void ToolWin();                             // 工具箱窗口

private slots :
    void toolButtonClicked(int id);             // 绘图槽函数ButtonGroup

在CPP文件的构造函数中添加

mainWindow::mainWindow(QWidget *parent)
    : QMainWindow(parent)
{
    ui.setupUi(this);   
    ......
    ToolWin();      // 工具箱窗口
}


void mainWindow::ToolWin()                              // 工具箱窗口
{
    //dock_Tool = new QDockWidget(tr("工具箱"), this);             // 工具栏
    //dock_Tool->setFeatures(QDockWidget::DockWidgetClosable);

    QPushButton *button_pen = new QPushButton(QIcon("../Image/toolbox/Painbrush.png"), tr(""), this);
    button_pen->setFixedSize(35, 35);
    button_pen->setToolTip(tr("钢笔"));
    button_pen->setObjectName("customButton");
    QPushButton *button_line = new QPushButton(QIcon("../Image/toolbox/Line.png"), tr(""), this);
    button_line->setFixedSize(35, 35);
    button_line->setToolTip(tr("线条"));
    button_line->setObjectName("customButton");
    QPushButton *button_circle = new QPushButton(QIcon("../Image/toolbox/Circle.png"), tr(""), this);
    button_circle->setFixedSize(35, 35);
    button_circle->setToolTip(tr("圆形"));
    button_circle->setObjectName("customButton");
    QPushButton *button_ellipse = new QPushButton(QIcon("../Image/toolbox/Elipse.png"), tr(""), this);
    button_ellipse->setFixedSize(35, 35);
    button_ellipse->setToolTip(tr("椭圆"));
    button_ellipse->setObjectName("customButton");
    QPushButton *button_triangle = new QPushButton(QIcon("../Image/toolbox/Triangle.png"), tr(""), this);
    button_triangle->setFixedSize(35, 35);
    button_triangle->setToolTip(tr("三角形"));
    button_triangle->setObjectName("customButton");
    QPushButton *button_rhombus = new QPushButton(QIcon("../Image/toolbox/Rhombus.png"), tr(""), this);
    button_rhombus->setFixedSize(35, 35);
    button_rhombus->setToolTip(tr("菱形"));
    button_rhombus->setObjectName("customButton");
    QPushButton *button_rect = new QPushButton(QIcon("../Image/toolbox/Rectangle.png"), tr(""), this);
    button_rect->setFixedSize(35, 35);
    button_rect->setToolTip(tr("长方形"));
    button_rect->setObjectName("customButton");
    QPushButton *button_square = new QPushButton(QIcon("../Image/toolbox/Square.png"), tr(""), this);
    button_square->setFixedSize(35, 35);
    button_square->setToolTip(tr("正方形"));
    button_square->setObjectName("customButton");
    QPushButton *button_hexagon = new QPushButton(QIcon("../Image/toolbox/Hexagon.png"), tr(""), this);
    button_hexagon->setFixedSize(35, 35);
    button_hexagon->setToolTip(tr("六边形"));
    button_hexagon->setObjectName("customButton");

    QGridLayout *toolLayout = new QGridLayout();
    toolLayout->setAlignment(Qt::AlignTop);
    toolLayout->addWidget(button_pen, 0, 0);
    toolLayout->addWidget(button_line, 0, 1);
    toolLayout->addWidget(button_ellipse, 1, 1);
    toolLayout->addWidget(button_circle, 1, 0);
    toolLayout->addWidget(button_triangle, 2, 0);
    toolLayout->addWidget(button_rhombus, 2, 1);
    toolLayout->addWidget(button_rect, 3, 0);
    toolLayout->addWidget(button_square, 3, 1);
    toolLayout->addWidget(button_hexagon, 4, 0);

    QWidget *toolWidget = new QWidget(dock_Tool);
    toolWidget->setLayout(toolLayout);
    dock_Tool->setWidget(toolWidget);


    // 按钮组 QButtonGroup
    toolButtonGroup = new QButtonGroup();
    toolButtonGroup->addButton(button_pen, 1);              // 自动分配的id为复数,从-2开始,
    toolButtonGroup->addButton(button_line, 2);             // 最好自己指定id,使用正数以避免冲突。
    toolButtonGroup->addButton(button_ellipse, 3);
    toolButtonGroup->addButton(button_circle, 4);
    toolButtonGroup->addButton(button_triangle, 5);
    toolButtonGroup->addButton(button_rhombus, 6);
    toolButtonGroup->addButton(button_rect, 7);
    toolButtonGroup->addButton(button_square, 8);
    toolButtonGroup->addButton(button_hexagon, 9);
    connect(toolButtonGroup, SIGNAL(buttonClicked(int)), this, SLOT(toolButtonClicked(int)));       // 按钮组的槽函数
}


// 绘图槽函数ButtonGroup
void mainWindow::toolButtonClicked(int id)  
{
    QList buttons = toolButtonGroup->buttons();
    foreach(QAbstractButton *button, buttons)
    {
        if (toolButtonGroup->button(id) != button)  // 按钮组内其它按钮设置为不可用
            button->setChecked(false);
        else if (drawType == id)                    // 如果绘制类型与前次相同,则退出绘制模式
            drawType = 0;
        else
            drawType = id;
    }

    switch (drawType)
    {
    case 0:
        QMessageBox::information(this, tr("绘制"), tr("退出绘制模式!"));    // 后续将改为绘制功能函数
        break;              
    case 1:
        QMessageBox::information(this, tr("绘制"), tr("钢笔!"));    break;
    case 2:
        QMessageBox::information(this, tr("绘制"), tr("线条!"));    break;
    case 3:
        QMessageBox::information(this, tr("绘制"), tr("椭圆形!"));   break;
    case 4:
        QMessageBox::information(this, tr("绘制"), tr("圆形!"));    break;
    case 5:
        QMessageBox::information(this, tr("绘制"), tr("三角形!"));   break;
    case 6:
        QMessageBox::information(this, tr("绘制"), tr("菱形!"));    break;
    case 7:
        QMessageBox::information(this, tr("绘制"), tr("长方形!"));   break;
    case 8:
        QMessageBox::information(this, tr("绘制"), tr("正方形!"));   break;
    case 9:
        QMessageBox::information(this, tr("绘制"), tr("六边形!"));   break;
    default:
        break;
    }
}
QT实现工具箱QButtonGroup
QT实现工具箱QButtonGroup
QT实现工具箱QButtonGroup