6.2、Qt::QPushButtonTest操作

前言:

本文操作均为在vs2015+QT5.9.5版本中执行

本文重要部分

//控件设置操作
//快捷键设置操作
//按下单个按键触发Pressed()事件
ui.ShortButton->setShortcut(tr(“x”));
//按下组合键触发Pressed()事件,后面设置的快捷键会覆盖前面的;
ui.ShortButton->setShortcut(tr(“Alt+x”));
//按下组合键后在按下按键a触发Pressed()事件,
ui.ShortButton->setShortcut(tr(“Alt+x,a”));

//控件样式设置
//样式设置
ui.StyleButton_2->setStyleSheet(QString::fromUtf8(“background-color: rgb(255, 14, 50);\n”
“color: rgb(255, 255, 255);\n”
“font: 16pt “\351\273\221\344\275\223”;”));

//样式图片设置
ui.StyleButton->setStyleSheet(QStringLiteral(“background-image: url(:/gif/tree.gif);”));
ui.StyleButton->setFlat(true);//设置为平面,其实就是减小点击后浮动。

//样式设置+圆角边框 border-radius: 20px;
ui.RadiusButton->setStyleSheet(QLatin1String(
“QPushButton{\n”
“color: rgb(255,0,0);\n”
" font: 16pt “\351\273\221\344\275\223”;"
“}\n”
“QPushButton::hover{\n”/设置选中样式/
“background-color: qlineargradient(spread:reflect, x1:1, y1:0.482955, x2:1, y2:0, stop:0.164773 rgba(0, 255, 255, 255), stop:1 rgba(255, 255, 255, 255));\n”
" border-radius:10px;\n"
“}\n”
“QPushButton::!hover{\n”/设置未选中样式/
“background-color: qlineargradient(spread:reflect, x1:1, y1:0.494, x2:1, y2:0, stop:0.238636 rgba(255, 255, 0, 255), stop:1 rgba(255, 255, 255, 255));\n”
" border-radius:10px;\n"
“}”));

头文件:textpushbutton.h

#pragma once

#include <QtWidgets/QWidget>
#include "ui_textpushbutton.h"

class TextpushButton : public QWidget
{
	Q_OBJECT

public:
	TextpushButton(QWidget *parent = Q_NULLPTR);
	public slots:
	void click();
	void click2(bool estimate);
	void Pressed();
	void Released();
private:
	Ui::TextpushButtonClass ui;
};

UI文件:ui_textpushbutton.h

/********************************************************************************
** Form generated from reading UI file 'textpushbutton.ui'
**
** Created by: Qt User Interface Compiler version 5.9.5
**
** WARNING! All changes made in this file will be lost when recompiling UI file!
********************************************************************************/

#ifndef UI_TEXTPUSHBUTTON_H
#define UI_TEXTPUSHBUTTON_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_TextpushButtonClass
{
public:
    QPushButton *ShortButton;
    QPushButton *StyleButton;
    QPushButton *pushButton;
    QPushButton *pushButton_3;
    QPushButton *StyleButton_2;
    QPushButton *pushButton_2;
    QPushButton *RadiusButton;

    void setupUi(QWidget *TextpushButtonClass)
    {
        if (TextpushButtonClass->objectName().isEmpty())
            TextpushButtonClass->setObjectName(QStringLiteral("TextpushButtonClass"));
        TextpushButtonClass->resize(1053, 713);
        ShortButton = new QPushButton(TextpushButtonClass);
        ShortButton->setObjectName(QStringLiteral("ShortButton"));
        ShortButton->setGeometry(QRect(270, 360, 241, 131));
        StyleButton = new QPushButton(TextpushButtonClass);
        StyleButton->setObjectName(QStringLiteral("StyleButton"));
        StyleButton->setGeometry(QRect(510, 50, 361, 201));
        StyleButton->setStyleSheet(QStringLiteral(""));
        StyleButton->setFlat(false);
        pushButton = new QPushButton(TextpushButtonClass);
        pushButton->setObjectName(QStringLiteral("pushButton"));
        pushButton->setGeometry(QRect(20, 210, 201, 121));
        pushButton_3 = new QPushButton(TextpushButtonClass);
        pushButton_3->setObjectName(QStringLiteral("pushButton_3"));
        pushButton_3->setGeometry(QRect(20, 360, 201, 131));
        pushButton_3->setCheckable(false);
        StyleButton_2 = new QPushButton(TextpushButtonClass);
        StyleButton_2->setObjectName(QStringLiteral("StyleButton_2"));
        StyleButton_2->setGeometry(QRect(20, 80, 201, 121));
        StyleButton_2->setStyleSheet(QStringLiteral(""));
        pushButton_2 = new QPushButton(TextpushButtonClass);
        pushButton_2->setObjectName(QStringLiteral("pushButton_2"));
        pushButton_2->setGeometry(QRect(270, 210, 241, 121));
        pushButton_2->setCheckable(true);
        RadiusButton = new QPushButton(TextpushButtonClass);
        RadiusButton->setObjectName(QStringLiteral("RadiusButton"));
        RadiusButton->setGeometry(QRect(550, 410, 261, 101));
        RadiusButton->setStyleSheet(QStringLiteral(""));

        retranslateUi(TextpushButtonClass);

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

    void retranslateUi(QWidget *TextpushButtonClass)
    {
        TextpushButtonClass->setWindowTitle(QApplication::translate("TextpushButtonClass", "TextpushButton", Q_NULLPTR));
        ShortButton->setText(QApplication::translate("TextpushButtonClass", "PushButton", Q_NULLPTR));
        StyleButton->setText(QApplication::translate("TextpushButtonClass", "PushButton", Q_NULLPTR));
        pushButton->setText(QApplication::translate("TextpushButtonClass", "\346\226\207\346\234\254\346\265\213\350\257\225\357\274\232\344\277\256\346\224\271\345\211\215", Q_NULLPTR));
        pushButton_3->setText(QApplication::translate("TextpushButtonClass", "\346\243\200\346\265\213\346\230\257\345\220\246\346\214\211\344\270\213", Q_NULLPTR));
        StyleButton_2->setText(QApplication::translate("TextpushButtonClass", "\346\240\267\345\274\217\350\256\276\347\275\256", Q_NULLPTR));
        pushButton_2->setText(QApplication::translate("TextpushButtonClass", "button(bool)\345\210\244\346\226\255\344\272\213\344\273\266", Q_NULLPTR));
        RadiusButton->setText(QApplication::translate("TextpushButtonClass", "\345\234\206\350\247\222\346\214\211\351\222\256", Q_NULLPTR));
    } // retranslateUi

};

namespace Ui {
    class TextpushButtonClass: public Ui_TextpushButtonClass {};
} // namespace Ui

QT_END_NAMESPACE

#endif // UI_TEXTPUSHBUTTON_H

主函数:main.cpp

#include "textpushbutton.h"
#include <QtWidgets/QApplication>

int main(int argc, char *argv[])
{
	QApplication a(argc, argv);
	TextpushButton w;
	w.show();
	return a.exec();
}

实现文件:textpushbutton.cpp

#include "textpushbutton.h"
#include <QDebug>
TextpushButton::TextpushButton(QWidget *parent)
	: QWidget(parent)
{

	ui.setupUi(this);
	//信号与槽关联
	//clicked()点击事件
	connect(ui.pushButton, SIGNAL(clicked()), this, SLOT(click()));
	//clicked(bool)判断是否为真和假,需要打开QAbstractButton中的checkable开关要打开
	connect(ui.pushButton_2, SIGNAL(clicked(bool)), this, SLOT(click2(bool)));
	//pressed()点击长按触发事件
	connect(ui.pushButton_3, SIGNAL(pressed()), this, SLOT(Pressed()));
	//released()点击松开触发事件
	connect(ui.pushButton_3, SIGNAL(released()), this, SLOT(Released()));
	//快捷键操作,按下快捷键按钮触发Pressed()事件
	connect(ui.ShortButton, SIGNAL(clicked()), this, SLOT(Pressed()));
	//样式表操作测试
	connect(ui.StyleButton_2, SIGNAL(clicked()), this, SLOT(Pressed()));
	connect(ui.StyleButton, SIGNAL(clicked()), this, SLOT(Pressed()));
	connect(ui.RadiusButton, SIGNAL(clicked()), this, SLOT(Pressed()));

	//控件设置操作
	/***************************************************************/
	//快捷键设置操作
	//按下单个按键触发Pressed()事件
	ui.ShortButton->setShortcut(tr("x"));
	//按下组合键触发Pressed()事件,后面设置的快捷键会覆盖前面的;
	ui.ShortButton->setShortcut(tr("Alt+x"));
	//按下组合键后在按下按键a触发Pressed()事件,
	ui.ShortButton->setShortcut(tr("Alt+x,a"));
	/***************************************************************/
	//控件样式设置
	/*************************************************************************************************/
	//样式设置
	ui.StyleButton_2->setStyleSheet(QString::fromUtf8("background-color: rgb(255, 14, 50);\n"
		"color: rgb(255, 255, 255);\n"
		"font: 16pt \"\351\273\221\344\275\223\";"));
	//样式图片设置
	ui.StyleButton->setStyleSheet(QStringLiteral("background-image: url(:/gif/tree.gif);"));
	ui.StyleButton->setFlat(true);//设置为平面,其实就是减小点击后浮动。

	//样式设置+圆角边框 border-radius: 20px;
	ui.RadiusButton->setStyleSheet(QLatin1String(
		"QPushButton{\n"
		"color: rgb(255,0,0);\n"
		"   font: 16pt \"\351\273\221\344\275\223\";"
		"}\n"
		"QPushButton::hover{\n"/*设置选中样式*/
		"background-color: qlineargradient(spread:reflect, x1:1, y1:0.482955, x2:1, y2:0, stop:0.164773 rgba(0, 255, 255, 255), stop:1 rgba(255, 255, 255, 255));\n"
		"    border-radius:10px;\n"
		"}\n"
		"QPushButton::!hover{\n"/*设置未选中样式*/
		"background-color: qlineargradient(spread:reflect, x1:1, y1:0.494, x2:1, y2:0, stop:0.238636 rgba(255, 255, 0, 255), stop:1 rgba(255, 255, 255, 255));\n"
		"    border-radius:10px;\n"
		"}"));
	/*************************************************************************************************/
}
void TextpushButton::click()
{
	ui.pushButton->setText(QString::fromLocal8Bit("文本测试:修改后"));
}
void TextpushButton::click2(bool estimate)
{
	if (estimate == true)
	{
		ui.pushButton_2->setText("true");
		qDebug() << "true";
	}
	else if (estimate == false)
	{
		ui.pushButton_2->setText("false");
		qDebug() << "false";
	}
}
void TextpushButton::Pressed()
{
	qDebug() << "Pressed()";
}
void TextpushButton::Released()
{
	qDebug() << "Released()";
}

ui界面布局样式

在这里插入图片描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

柳一航

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值