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()";
}