gpt4 book ai didi

qt - 如何在 QT 中设计具有自定义形状的按钮

转载 作者:行者123 更新时间:2023-12-04 12:48:53 25 4
gpt4 key购买 nike

我试图在 Qt 中设计一系列带有箭头形状的自定义按钮,但我找不到方法。我需要一些类似于您可以在下一张图片中看到的按钮,以便管理我的程序的工作流程并引导用户完成它。
workflow guide buttons
图片的文字应为“Step 1”、“Step 2”和“Step 3”。抱歉我的错误。
欢迎任何帮助或建议。
谢谢你的帮助。

最佳答案

头文件:

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

cpp文件:

我用按钮颜色填充颜色列表。
colors.append(Qt::red);
colors.append(Qt::blue);
colors.append(Qt::yellow);

并调用 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)));
}
}

结果是

you can look result

关于qt - 如何在 QT 中设计具有自定义形状的按钮,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29796256/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com