qt:按钮的常见操作(简单方向键项目)

1.圆形按钮

首先,设置圆形按钮,首先要将setGeometry(x位置,y位置,长,宽)中的长和宽设置为相等,再使用一下模板

   q2->setStyleSheet("QPushButton {"
                     "    background-color: black;"
                     "    color: white;"
                     "    border-radius: 25px;"
                     "    border: none;"
                     "}");

将border_radius设置为长或宽的一半,就可以得到一个圆形按钮了

 

 

2.图片按钮

   cq->setIcon(icon);
   cq->setIconSize(QSize(100,50));

那么问题来了,如何在图片上显示字呢?

最好的方法是在图片上直接p,如果使用样式表就需要绝对路径,一般不会使用绝对路径 

3.按钮快捷键

   方法

up->setShortcut(QKeySequence("w"));

up是按钮,而且w的位置也可以是ctrl+w等组合键

在使用这钟快捷键方式后,不点击小兔子,点键盘上的wasd,就能控制猫的移动了

4. 重复触发

键盘:默认支持

鼠标:setAutoReapeat,setAutoReapeatDelay(触发延迟),setAutoReapeatInterval(触发间隔)

  up->setAutoRepeat(true);
    down->setAutoRepeat(true);
     left->setAutoRepeat(true);
      right->setAutoRepeat(true);

现在按住兔子,猫就可以一直向上了

5.选择按钮

QRaioButton,属性:checkable:能否被选中,true表示可以被选中,false表示不可以,checked,表示是否已经被选中

对应槽函数,clicked是点击时,pressed是鼠标按下时,released是鼠标释放时,toggled是checked属性改时(最适合)

所有按钮默认排他,QButtonGroup可以进行分组,组内排他,QCheckbox可以复选

应用:模拟菜单(选择标题可以将按钮和标签放入一个显示组里面管理,具体可以看代码)

注意,QButtonGroup默认排他,在对复选按钮进行管理时需要setExclusive函数取消排他性,要设置标签的话可以在旁边搞一个不可选中的黑底白字按钮,或进行显示控制(之后会讲)

代码

pro

QT       += core gui
QMAKE_PROJECT_DEPTH = 0

greaterThan(QT_MAJOR_VERSION, 4): QT += widgets

CONFIG += c++17

# You can make your code fail to compile if it uses deprecated APIs.
# In order to do so, uncomment the following line.
#DEFINES += QT_DISABLE_DEPRECATED_BEFORE=0x060000    # disables all the APIs deprecated before Qt 6.0.0

SOURCES += \
    main.cpp \
    mainwindow.cpp

HEADERS += \
    mainwindow.h

FORMS += \
    mainwindow.ui

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

 .h

#ifndef MAINWINDOW_H
#define MAINWINDOW_H

#include <QMainWindow>
#include<QPushButton>
#include<QRadioButton>
#include<QCheckBox>
#include<QButtonGroup>
#include<QGroupBox>
QT_BEGIN_NAMESPACE
namespace Ui {
class MainWindow;
}
QT_END_NAMESPACE

class MainWindow : public QMainWindow
{
    Q_OBJECT

public:
    MainWindow(QWidget *parent = nullptr);

    ~MainWindow();
public slots:
    void getup();
    void getdown();
    void getleft();
    void getright();
private:
    Ui::MainWindow *ui;
    QPushButton*example;
    QPushButton*up;
    QPushButton*down;
    QPushButton*left;
    QPushButton*right;
    QRadioButton*q1;
    QRadioButton*q2;
    QCheckBox*q3;
    QCheckBox*q4;
    QCheckBox*q5;
   QButtonGroup*g1;
    QButtonGroup*g2;
};
#endif // MAINWINDOW_H

main

#include "mainwindow.h"

#include <QApplication>

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

.cpp

#include "mainwindow.h"
#include "ui_mainwindow.h"
#include<QLabel>
#include<QVBoxLayout>
MainWindow::MainWindow(QWidget *parent)
    : QMainWindow(parent)
    , ui(new Ui::MainWindow)
{
    ui->setupUi(this);
    example=new QPushButton(this);

    up=new QPushButton(this);
    down=new QPushButton(this);
    left=new QPushButton(this);
    right=new QPushButton(this);
    example->setGeometry(100,100,50,50);
     up->setGeometry(375,200,100,100);
     down->setGeometry(375,400,100,100);
     left->setGeometry(275,300,100,100);
     right->setGeometry(475,300,100,100);
    QIcon cat("D:/project/cat.jpg");
    example->setIcon(cat);
    example->setIconSize(QSize(55,75));
     QIcon upsign("D:/project/p1.jpg");
    up->setIcon(upsign);
     up->setIconSize(QSize(100,100));
    down->setIcon(upsign);
    down->setIconSize(QSize(100,100));
    left->setIcon(upsign);
    left->setIconSize(QSize(100,100));
    right->setIcon(upsign);
    right->setIconSize(QSize(100,100));
    up->setShortcut(QKeySequence("w"));
     down->setShortcut(QKeySequence("s"));
     left->setShortcut(QKeySequence("a"));
      right->setShortcut(QKeySequence("d"));
     up->setAutoRepeat(true);
    down->setAutoRepeat(true);
     left->setAutoRepeat(true);
      right->setAutoRepeat(true);
    connect(up,&QPushButton::clicked,this,&MainWindow::getup);
    connect(down,&QPushButton::clicked,this,&MainWindow::getdown);
    connect(left,&QPushButton::clicked,this,&MainWindow::getleft);
    connect(right,&QPushButton::clicked,this,&MainWindow::getright);
    q1=new QRadioButton("麦辣鸡腿",this);
    q1->setGeometry(600,100,100,100);
    q2=new QRadioButton("双层吉士",this);
    q2->setGeometry(500,100,100,100);
    q3=new QCheckBox("蛋挞",this);
    q3->setGeometry(100,200,100,100);
    q4=new QCheckBox("可乐",this);
    q4->setGeometry(100,300,100,100);
    q5=new QCheckBox("雪碧",this);
    q5->setGeometry(100,400,100,100);
    g1=new QButtonGroup(this);
    g2=new QButtonGroup(this);
    g1->addButton(q1);
    g1->addButton(q2);
    g2->setExclusive(false);
    g2->addButton(q3);
    g2->addButton(q4);
   g2->addButton(q5);
    //QLabel*l=new QLabel("汉堡",this);
  //  l->setGeometry(400,100,100,100);
  //  QVBoxLayout *layout = new QVBoxLayout(this);
   // layout->addWidget(l);
   // layout->addWidget(q1);
   // layout->addWidget(q2);


}
void MainWindow::getup()
{
    QPoint mypos = example->pos();
  mypos.setY(mypos.y() -5);
    example->move(mypos);
}void MainWindow::getdown()
{
    QPoint mypos = example->pos();
    mypos.setY(mypos.y() +5);
    example->move(mypos);
}
void MainWindow::getleft()
{
    QPoint mypos = example->pos();
    mypos.setX(mypos.x() - 5);
    example->move(mypos);
}
void MainWindow::getright()
{
    QPoint mypos = example->pos();
    mypos.setX(mypos.x() + 5);
    example->move(mypos);
}
MainWindow::~MainWindow()
{
    delete ui;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值