Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add-new: 连续平行四边形按钮 #49

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Binary file added ui/button_parallelogram/bk.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
43 changes: 43 additions & 0 deletions ui/button_parallelogram/button_parallelogram.pro
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
#-------------------------------------------------
#
# Project created by QtCreator 2024-07-18T14:25:58
#
#-------------------------------------------------

QT += core gui

greaterThan(QT_MAJOR_VERSION, 4): QT += widgets

TARGET = button_parallelogram
TEMPLATE = app

# The following define makes your compiler emit warnings if you use
# any feature of Qt which has been marked as deprecated (the exact warnings
# depend on your compiler). Please consult the documentation of the
# deprecated API in order to know how to port your code away from it.
DEFINES += QT_DEPRECATED_WARNINGS

# You can also make your code fail to compile if you use deprecated APIs.
# In order to do so, uncomment the following line.
# You can also select to disable deprecated APIs only up to a certain version of Qt.
#DEFINES += QT_DISABLE_DEPRECATED_BEFORE=0x060000 # disables all the APIs deprecated before Qt 6.0.0

CONFIG += c++11

SOURCES += \
main.cpp \
widget.cpp

HEADERS += \
widget.h

FORMS += \
widget.ui

# Default rules for deployment.
qnx: target.path = /tmp/$${TARGET}/bin
else: unix:!android: target.path = /opt/$${TARGET}/bin
!isEmpty(target.path): INSTALLS += target

RESOURCES += \
image.qrc
323 changes: 323 additions & 0 deletions ui/button_parallelogram/button_parallelogram.pro.user

Large diffs are not rendered by default.

5 changes: 5 additions & 0 deletions ui/button_parallelogram/image.qrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<RCC>
<qresource prefix="/">
<file>bk.png</file>
</qresource>
</RCC>
11 changes: 11 additions & 0 deletions ui/button_parallelogram/main.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
#include "widget.h"
#include <QApplication>

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

return a.exec();
}
75 changes: 75 additions & 0 deletions ui/button_parallelogram/ui_widget.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
/********************************************************************************
** Form generated from reading UI file 'widget.ui'
**
** Created by: Qt User Interface Compiler version 5.9.8
**
** WARNING! All changes made in this file will be lost when recompiling UI file!
********************************************************************************/

#ifndef UI_WIDGET_H
#define UI_WIDGET_H

#include <QtCore/QVariant>
#include <QtWidgets/QAction>
#include <QtWidgets/QApplication>
#include <QtWidgets/QButtonGroup>
#include <QtWidgets/QHeaderView>
#include <QtWidgets/QPushButton>
#include <QtWidgets/QWidget>

QT_BEGIN_NAMESPACE

class Ui_Widget
{
public:
QPushButton *pushButton;
QPushButton *pushButton2;
QPushButton *pushButton3;

void setupUi(QWidget *Widget)
{
if (Widget->objectName().isEmpty())
Widget->setObjectName(QStringLiteral("Widget"));
Widget->resize(1015, 612);
pushButton = new QPushButton(Widget);
pushButton->setObjectName(QStringLiteral("pushButton"));
pushButton->setGeometry(QRect(70, 70, 200, 80));
QSizePolicy sizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);
sizePolicy.setHorizontalStretch(0);
sizePolicy.setVerticalStretch(0);
sizePolicy.setHeightForWidth(pushButton->sizePolicy().hasHeightForWidth());
pushButton->setSizePolicy(sizePolicy);
pushButton->setStyleSheet(QStringLiteral("transform:skewX(-30deg);"));
pushButton2 = new QPushButton(Widget);
pushButton2->setObjectName(QStringLiteral("pushButton2"));
pushButton2->setGeometry(QRect(270, 70, 200, 80));
sizePolicy.setHeightForWidth(pushButton2->sizePolicy().hasHeightForWidth());
pushButton2->setSizePolicy(sizePolicy);
pushButton3 = new QPushButton(Widget);
pushButton3->setObjectName(QStringLiteral("pushButton3"));
pushButton3->setGeometry(QRect(470, 70, 200, 80));
sizePolicy.setHeightForWidth(pushButton3->sizePolicy().hasHeightForWidth());
pushButton3->setSizePolicy(sizePolicy);

retranslateUi(Widget);

QMetaObject::connectSlotsByName(Widget);
} // setupUi

void retranslateUi(QWidget *Widget)
{
Widget->setWindowTitle(QApplication::translate("Widget", "Widget", Q_NULLPTR));
pushButton->setText(QApplication::translate("Widget", "PushButton", Q_NULLPTR));
pushButton2->setText(QApplication::translate("Widget", "PushButton", Q_NULLPTR));
pushButton3->setText(QApplication::translate("Widget", "PushButton", Q_NULLPTR));
} // retranslateUi

};

namespace Ui {
class Widget: public Ui_Widget {};
} // namespace Ui

QT_END_NAMESPACE

#endif // UI_WIDGET_H
46 changes: 46 additions & 0 deletions ui/button_parallelogram/widget.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
#include "widget.h"
#include "ui_widget.h"
#include <QPixmap>
#include <QBitmap>
#include <qmath.h>

Widget::Widget(QWidget *parent) :
QWidget(parent),
ui(new Ui::Widget)
{
ui->setupUi(this);
}

Widget::~Widget()
{
delete ui;
}

void Widget::showEvent(QShowEvent *event)
{
ui->pushButton->setStyleSheet("background-color: red;");
ui->pushButton2->setStyleSheet("background-color: green;");
ui->pushButton3->setStyleSheet("background-color: blue;");

ui->pushButton->setText("button1");
ui->pushButton2->setText("button2");
ui->pushButton3->setText("button3");

QPixmap maskUp(":/bk.png");
// 获取按钮的大小
QRect buttonSize = ui->pushButton->geometry();

// 计算倾斜角度来获取偏移量
int buttonHeight = buttonSize.height();
double angleInRadians = qDegreesToRadians(8.0); // 假设倾斜角度为8度
int horizontalOffset = buttonHeight * qTan(angleInRadians);

// 将图片缩放到按钮大小
QPixmap scaledMask = maskUp.scaledToWidth(buttonSize.width());
ui->pushButton->setMask(scaledMask.mask());
ui->pushButton2->setMask(scaledMask.mask());
ui->pushButton3->setMask(scaledMask.mask());
// 主动布局,不使用固有布局
ui->pushButton2->move( ui->pushButton->pos().x() + buttonSize.width() - horizontalOffset,ui->pushButton->pos().y());
ui->pushButton3->move( ui->pushButton2->pos().x() + buttonSize.width() - horizontalOffset,ui->pushButton2->pos().y());
}
24 changes: 24 additions & 0 deletions ui/button_parallelogram/widget.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
#ifndef WIDGET_H
#define WIDGET_H

#include <QWidget>

namespace Ui {
class Widget;
}

class Widget : public QWidget
{
Q_OBJECT

public:
explicit Widget(QWidget *parent = nullptr);
~Widget();
protected:
void showEvent(QShowEvent *event) override; // 声明 showEvent 函数

private:
Ui::Widget *ui;
};

#endif // WIDGET_H
80 changes: 80 additions & 0 deletions ui/button_parallelogram/widget.ui
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
<?xml version="1.0" encoding="UTF-8"?>
<ui version="4.0">
<class>Widget</class>
<widget class="QWidget" name="Widget">
<property name="geometry">
<rect>
<x>0</x>
<y>0</y>
<width>1015</width>
<height>612</height>
</rect>
</property>
<property name="windowTitle">
<string>Widget</string>
</property>
<widget class="QPushButton" name="pushButton">
<property name="geometry">
<rect>
<x>70</x>
<y>70</y>
<width>200</width>
<height>80</height>
</rect>
</property>
<property name="sizePolicy">
<sizepolicy hsizetype="Expanding" vsizetype="Expanding">
<horstretch>0</horstretch>
<verstretch>0</verstretch>
</sizepolicy>
</property>
<property name="styleSheet">
<string notr="true">transform:skewX(-30deg);</string>
</property>
<property name="text">
<string>PushButton</string>
</property>
</widget>
<widget class="QPushButton" name="pushButton2">
<property name="geometry">
<rect>
<x>270</x>
<y>70</y>
<width>200</width>
<height>80</height>
</rect>
</property>
<property name="sizePolicy">
<sizepolicy hsizetype="Expanding" vsizetype="Expanding">
<horstretch>0</horstretch>
<verstretch>0</verstretch>
</sizepolicy>
</property>
<property name="text">
<string>PushButton</string>
</property>
</widget>
<widget class="QPushButton" name="pushButton3">
<property name="geometry">
<rect>
<x>470</x>
<y>70</y>
<width>200</width>
<height>80</height>
</rect>
</property>
<property name="sizePolicy">
<sizepolicy hsizetype="Expanding" vsizetype="Expanding">
<horstretch>0</horstretch>
<verstretch>0</verstretch>
</sizepolicy>
</property>
<property name="text">
<string>PushButton</string>
</property>
</widget>
</widget>
<layoutdefault spacing="6" margin="11"/>
<resources/>
<connections/>
</ui>