如何在QT中使用自定义形状设计按钮

如何在QT中使用自定义形状设计按钮,qt,button,qt4,Qt,Button,Qt4,我试图在Qt中设计一系列箭头形状的自定义按钮,但我找不到方法。我需要一些类似按钮的东西,你可以在下一张图片中看到,以便管理我的程序的工作流程,并指导用户通过它。 图像的文本应为“步骤1”、“步骤2”和“步骤3”。对不起,弄错了。 欢迎任何帮助或建议。 感谢您的帮助。您可以使用一个小部件而不是树,并在小部件的paintEvent()函数中绘制内容(步骤1、步骤2…)。鼠标单击(或其他)事件时,您可以计算事件发生在哪个虚拟按钮上,并执行相应的操作。如果您不想自己完成所有绘制,可以使用三个正常的qpu

我试图在Qt中设计一系列箭头形状的自定义按钮,但我找不到方法。我需要一些类似按钮的东西,你可以在下一张图片中看到,以便管理我的程序的工作流程,并指导用户通过它。 图像的文本应为“步骤1”、“步骤2”和“步骤3”。对不起,弄错了。 欢迎任何帮助或建议。
感谢您的帮助。

您可以使用一个小部件而不是树,并在小部件的
paintEvent()
函数中绘制内容(步骤1、步骤2…)。鼠标单击(或其他)事件时,您可以计算事件发生在哪个虚拟按钮上,并执行相应的操作。

如果您不想自己完成所有绘制,可以使用三个正常的
qpushbutton
setIcon()
在将
flat
属性设置为true时,将自定义图形切割为三部分

可能需要在父窗口小部件中对它们进行分组,以控制它们的位置和大小,具体取决于您的布局。

头文件:

public:

void paintEvent(QPaintEvent* event); //--> drawing triangles
void createPushButtons(); 
const static int firstButtonX = 0; //--> topleft corner x value of first button 
const static int firstButtonY = 0; //--> topleft corner y value of first button 
const static int buttonWidth = 50;
const static int buttonHeight = 30;
const static int triangleWidth = 30;
QList<QColor> colors; //--> button colors
并调用
createPushButtons()创建按钮的功能

void MainWindow::createPushButtons()
{
    QWidget * wdg = new QWidget(this);//--> widget contains pushButtons
    wdg->setObjectName("buttonWidget");//--> I set object name in order to catch widget and pushButtons in PaintEvent()
    setCentralWidget(wdg);//--> I add widget to app main layout

    for(int i=0; i<colors.size(); i++)//--> Button count and color count must be same.
    {
        QPushButton *btn = new QPushButton(QString::number(i)+". button",wdg);//--> create pushButtons in parent wdg
        btn->setGeometry(firstButtonX + (buttonWidth+triangleWidth)*i, firstButtonY, buttonWidth, buttonHeight);//--> I set geometry for pushButtons 

        btn->setStyleSheet(QString("background-color: %1;border-style: outset;border-width: 0px;").arg(colors.at(i).name()));//--> I change background color and clear border
    }
}

void MainWindow::paintEvent(QPaintEvent *event)
{
    QWidget *wdg = findChild<QWidget *>("buttonWidget");//--> I catch my widget
    QList<QPushButton *> buttons = wdg->findChildren < QPushButton *>();//--> I find pushButtons

    for(int i=0;i < buttons.size(); i++)
    {
        int x = buttons.at(i)->pos().x();//--> measurements for triangle
        int y = buttons.at(i)->pos().y();
        int w = buttons.at(i)->width();
        int h = buttons.at(i)->height();

        QRect r(x+w,y,triangleWidth,h);//--> I create rectangular area between pushButtons

        QPainter painter(this);
        qreal point3X = x+w + triangleWidth;//--> triangle corners
        qreal point3Y = y + h/2 ;
        qreal point1X = x+w;
        qreal point1Y = y;
        qreal point2X = x+w;
        qreal point2Y = y+h;

        QPainterPath path;
        path.moveTo (point1X, point1Y);
        path.lineTo (point2X, point2Y);
        path.lineTo (point3X, point3Y);

        painter.setPen (Qt :: NoPen);
        if(i != buttons.size()-1)//--> I paint rectangular and triangle
        {
            painter.fillRect(r, QBrush (colors.at(i+1)));
        }
        painter.fillPath (path, QBrush (colors.at(i)));
    }
}
void主窗口::createPushButtons()
{
QWidget*wdg=新的QWidget(此);//-->小部件包含按钮
wdg->setObjectName(“buttonWidget”);//-->我设置对象名称是为了捕捉PaintEvent()中的小部件和按钮
setCentralWidget(wdg);//-->我将小部件添加到应用程序主布局中
对于(int i=0;i)按钮计数和颜色计数必须相同。
{
QPushButton*btn=新的QPushButton(QString::number(i)+“.button”,wdg);//-->在父wdg中创建按钮
btn->setGeometry(firstButtonX+(buttonWidth+triangleWidth)*i,firstButtonY,buttonWidth,buttonHeight);//-->我为按钮设置几何图形
btn->setStyleSheet(QString(“背景色:%1;边框样式:开始;边框宽度:0px;”)).arg(colors.at(i).name());//-->我更改背景色并清除边框
}
}
void主窗口::paintEvent(QPaintEvent*事件)
{
QWidget*wdg=findChild(“buttonWidget”);//-->我捕获我的小部件
QList buttons=wdg->findChildren();//-->我找到了按钮
对于(int i=0;ipos().x();//-->三角形的测量值
int y=按钮。在(i)->pos().y();
int w=按钮。在(i)->宽度();
int h=按钮。在(i)->高度();
QRect r(x+w,y,三角形宽度,h);//-->我在按钮之间创建矩形区域
油漆工(本);
qreal point3X=x+w+三角形宽度;//-->三角形角点
qreal point3Y=y+h/2;
qreal point1X=x+w;
qreal point1Y=y;
qreal point2X=x+w;
qreal point2Y=y+h;
QPainterPath路径;
path.moveTo(点1X,点1Y);
path.lineTo(点2x,点2y);
path.lineTo(点3x,点3y);
painter.setPen(Qt::NoPen);
如果(i!=buttons.size()-1)/-->我绘制矩形和三角形
{
painter.fillRect(r,QBrush(colors.at(i+1)));
}
painter.fillPath(path,QBrush(colors.at(i)));
}
}
结果是


但这仍然是准确的吗?
void MainWindow::createPushButtons()
{
    QWidget * wdg = new QWidget(this);//--> widget contains pushButtons
    wdg->setObjectName("buttonWidget");//--> I set object name in order to catch widget and pushButtons in PaintEvent()
    setCentralWidget(wdg);//--> I add widget to app main layout

    for(int i=0; i<colors.size(); i++)//--> Button count and color count must be same.
    {
        QPushButton *btn = new QPushButton(QString::number(i)+". button",wdg);//--> create pushButtons in parent wdg
        btn->setGeometry(firstButtonX + (buttonWidth+triangleWidth)*i, firstButtonY, buttonWidth, buttonHeight);//--> I set geometry for pushButtons 

        btn->setStyleSheet(QString("background-color: %1;border-style: outset;border-width: 0px;").arg(colors.at(i).name()));//--> I change background color and clear border
    }
}

void MainWindow::paintEvent(QPaintEvent *event)
{
    QWidget *wdg = findChild<QWidget *>("buttonWidget");//--> I catch my widget
    QList<QPushButton *> buttons = wdg->findChildren < QPushButton *>();//--> I find pushButtons

    for(int i=0;i < buttons.size(); i++)
    {
        int x = buttons.at(i)->pos().x();//--> measurements for triangle
        int y = buttons.at(i)->pos().y();
        int w = buttons.at(i)->width();
        int h = buttons.at(i)->height();

        QRect r(x+w,y,triangleWidth,h);//--> I create rectangular area between pushButtons

        QPainter painter(this);
        qreal point3X = x+w + triangleWidth;//--> triangle corners
        qreal point3Y = y + h/2 ;
        qreal point1X = x+w;
        qreal point1Y = y;
        qreal point2X = x+w;
        qreal point2Y = y+h;

        QPainterPath path;
        path.moveTo (point1X, point1Y);
        path.lineTo (point2X, point2Y);
        path.lineTo (point3X, point3Y);

        painter.setPen (Qt :: NoPen);
        if(i != buttons.size()-1)//--> I paint rectangular and triangle
        {
            painter.fillRect(r, QBrush (colors.at(i+1)));
        }
        painter.fillPath (path, QBrush (colors.at(i)));
    }
}